我在pygame中制作俄罗斯方块,但只制作了1个方块(不是4个)。下面是block类:
# Creating an environment representing the grid (virtual one)
environment = numpy.zeros((21, 16), dtype=numpy.int32)
class Block(object) :
def __init__(self):
self.x, self.y, self.color, self.row, self.column = 631, 3, 0, 0, 7
self.img = next_block
self.mv_step = 32
pygame.time.set_timer(pygame.USEREVENT+1, 800)
self.is_current_block = True
def blit_and_manage(self):
MyScreen.blit(self.img, (self.x, self.y))
# this makes the first line doesn't count, because very block spawns outside the grid at first and then enters it
if self.y == 35:
self.row = 0
# handling events
for e in events:
# moving a block to the left
if (((e.type == pygame.KEYDOWN and e.key == pygame.K_q) or (e.type == pygame.KEYDOWN and e.key == pygame.K_LEFT)) and self.x != 407) and self.is_current_block and environment[self.row, self.column-1] == 0:
self.x -= 32
self.column -= 1
environment[self.row, self.column+1] = 0
# moving a block to the right
elif (((e.type == pygame.KEYDOWN and e.key == pygame.K_d) or (e.type == pygame.KEYDOWN and e.key == pygame.K_RIGHT)) and self.x != 887) and self.is_current_block and environment[self.row, self.column+1] == 0:
self.x += 32
self.column += 1
environment[self.row, self.column-1] = 0
# moving a block down
if ((e.type == pygame.USEREVENT+1) or ((e.type == pygame.KEYDOWN and (e.key == pygame.K_s or e.key == pygame.K_DOWN))) and self.is_current_block) and environment[self.row+1, self.column] == 0:
self.y += self.mv_step
if self.y > 35:
self.row += 1
environment[self.row-1, self.column] = 0
# preventing blocks to get outside of the grid when they reach the bottom boundary
if self.y >= 675:
self.y = 675
self.is_current_block = False
# modifying the block's corresponding coordinate in the numpy array (environment) to its corresponding color number
if self.y > 3:
environment[self.row, self.column] = self.color
# preventing blocks from moving down if there is another block underneath
if self.row != 20 and environment[self.row+1, self.column] != 0:
self.is_current_block = False
现在,一切都很好,但只有一个错误:如果我试图用块填充垂直线(通过滥发s按钮或向下箭头,顺便说一句,我使用zqsd而不是wasd),块将彼此重叠,直到只剩下两个空间,下一个将生成的块不会在某个位置向下移动,即使它下面的空间是空的,然后游戏将冻结并变得无响应,直到我用ctrl+c进行键盘中断。您可能还没有理解这个问题,因此这里有一个快速表示:
注:
请记住,我还没有做过一个游戏的条件,所以这个问题不能涉及到一个未完成的游戏功能或类似的东西。
别忘了,每个方块的颜色值都会在方块的y值达到35时放在环境数组中的相应位置,因为正如我前面所说的,在开始时有一条额外的线,既不会在游戏的网格中绘制,也不会包含在环境数组中。
问题只发生在块首次生成的列中。
以下是发生该问题时环境变量的外观:
[[0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 4 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 6 0 0 0 0 0 0 0 0]]
暂无答案!
目前还没有任何答案,快来回答吧!