Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

list - How to make a new object while the program is running (Python, pygame)

I am currently making 2048 and am having trouble with creating a new block whenever a move is made.

I made a list storing all the blocks there are, within the list there objects:

class Blocks:
   ~~~~~~~ code ~~~~~~~~~

block1 = Blocks()
block2 = Blocks()
block_list = [block1, block2] 

The issue is that it works perfectly when the blocks are pre-written, but I can not make a new block while the game is already running, is there any way to do this? Thank you in advance, stay safe :)

question from:https://stackoverflow.com/questions/65894324/how-to-make-a-new-object-while-the-program-is-running-python-pygame

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't see why this would not be possible. You should simply be able append a new block to your block_list:

block_n = Blocks()
block_list.append(block_n)

You place this in your game loop where you detect when a move is made.

Where you blit I assume you go through the block_list and blit all the blocks to the screen? Also make sure to always update the screen with pygame.display.flip().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...