Python 我的pygame俄罗斯方块游戏一直被卡住
我是一个初级python程序员,我正在尝试制作一个俄罗斯方块游戏。当我运行游戏时,块每秒向下移动10像素。问题是块随机停止,我的打印语句也停止打印。我没有收到错误,根据任务管理器,内存正常。我有32位的vista和celeron处理器,但我也在Windows7和i5处理器上试过,问题仍然存在,所以我迷路了 代码如下:Python 我的pygame俄罗斯方块游戏一直被卡住,python,pygame,tetris,Python,Pygame,Tetris,我是一个初级python程序员,我正在尝试制作一个俄罗斯方块游戏。当我运行游戏时,块每秒向下移动10像素。问题是块随机停止,我的打印语句也停止打印。我没有收到错误,根据任务管理器,内存正常。我有32位的vista和celeron处理器,但我也在Windows7和i5处理器上试过,问题仍然存在,所以我迷路了 代码如下: import pygame from pygame.locals import * from datetime import datetime import Blocks def
import pygame
from pygame.locals import *
from datetime import datetime
import Blocks
def Gravity(Box, rate):
# Gravity effect
Box.set_yCoor(Box.get_yCoor()+ rate)
Box.Edge.set_yCoor(Box.Edge.get_yCoor()+ rate)
return Box
def main():
# initailize all pygame modules
pygame.init()
windowWidth,windowHeight= 640,700
screen_color= (255,255,255)
# Determines how many pixels per time the box will fall
gravity= 10
# Create screen/window and change color to white
screen= pygame.display.set_mode((windowWidth,windowHeight),0,32)
screen.fill(screen_color)
# Used to determine whether to draw box and apply gravity
index =0
# Put box and border info in list
# Args: (x,y) (len, width) (color)
boxInfo= [10,10, 20,50, (0,0,0)]
borderInfo= [5,5, 30,60, (154,24,214)]
# Create instance of box and pass info
Box= Blocks.Box(boxInfo, borderInfo)
#Create an endless loop that the game will run inside of
while True:
for event in pygame.event.get():
if event.type== QUIT:
pygame.quit()
return
# Get second and millisecond time and convert to string
startTime= str(datetime.now())
# Splice time string and convert to float
startTime= float(startTime[17:23])
if index== 0:
# Pull the box down by gravity
Box= Gravity(Box, gravity)
# Move box back to the top of window
if Box.Edge.get_yCoor() > windowHeight - Box.Edge.get_boxWid():
Box.Edge.set_yCoor(5)
Box.set_yCoor(10)
# Clear screen
screen.fill(screen_color)
# Display Screen
Box.display(screen)
# Set the stop time used to determine when to call gravity and display
stopTime= startTime+ 1
index= 1
# Get second and millisecond time and convert to string
bug= str(datetime.now())
# Splice time string and convert to float
bug= bug[17:23]
print("\nDisplayed at:")
print('Start:', startTime)
print('Stop:', stopTime)
print('Bug:', bug)
elif startTime >= stopTime:
index= 0
# Get second and millisecond time and convert to string
bug= str(datetime.now())
# Splice time string and convert to float
bug= bug[17:26]
print("\nNot displaying at:")
print('Start:', startTime)
print('Stop:', stopTime)
print('Bug:', bug)
# Call main
main()
这是blocks类:
导入pygame
从pygame.locals导入*
class Box():
def __init__(self, boxInfo, edgeInfo):
# Pixel location of the box
self.__xCoor= boxInfo[0]
self.__yCoor= boxInfo[1]
# Pixel length and width of the box
self.__boxLen= boxInfo[2]
self.__boxWid= boxInfo[3]
# Pixel color is white by default
self.__color= boxInfo[4]
self.Edge= Border(edgeInfo)
# ---- ACCESSORS -- and -- MUTATORS ----
def set_xCoor(self,Coordx):
self.__xCoor= Coordx
def get_xCoor(self):
return self.__xCoor
def set_yCoor(self,Coordy):
self.__yCoor= Coordy
def get_yCoor(self):
return self.__yCoor
def set_boxLen(self,Length):
self.__boxLen= Length
def get_boxLen(self):
return self.__boxLen
def set_boxWid(self,Width):
self.__boxWid= Width
def get_boxWid(self):
return self.__boxWid
def set_color(self,color):
self.__color= color
def get_color(self):
return self.__color
# ---- METHODS ----
def boxStatCheck(self):
# Prints all the attributes in the shell for debug
print("x-Coordinate=", self.get_xCoor())
print("y-Coordinate=", self.get_yCoor())
print("Box Length=", self.get_boxLen())
print("Box Width=", self.get_boxWid())
print("Color Value=", self.get_color())
def display(self, screen):
screen.lock()
pygame.draw.rect(screen, self.Edge.get_color(), Rect((self.Edge.get_xCoor(),self.Edge.get_yCoor()),
(self.Edge.get_boxLen(),self.Edge.get_boxWid())))
pygame.draw.rect(screen, self.get_color(), Rect((self.get_xCoor(),self.get_yCoor()),
(self.get_boxLen(),self.get_boxWid())))
screen.unlock()
pygame.display.update()
class Border():
def __init__(self, listInfo):
# Pixel location of the box
self.__xCoor= listInfo[0]
self.__yCoor= listInfo[1]
# Pixel length and width of the box
self.__boxLen= listInfo[2]
self.__boxWid= listInfo[3]
# Pixel color is white by default
self.__color= listInfo[4]
# ---- ACCESSORS -- and -- MUTATORS ----
def set_xCoor(self,Coordx):
self.__xCoor= Coordx
def get_xCoor(self):
return self.__xCoor
def set_yCoor(self,Coordy):
self.__yCoor= Coordy
def get_yCoor(self):
return self.__yCoor
def set_boxLen(self,Length):
self.__boxLen= Length
def get_boxLen(self):
return self.__boxLen
def set_boxWid(self,Width):
self.__boxWid= Width
def get_boxWid(self):
return self.__boxWid
def set_color(self,color):
self.__color= color
def get_color(self):
return self.__color
# ---- METHODS ----
def boxStatCheck(self):
# Prints all the attributes in the shell for debug
print("x-Coordinate=", self.get_xCoor())
print("y-Coordinate=", self.get_yCoor())
print("Box Length=", self.get_boxLen())
print("Box Width=", self.get_boxWid())
print("Color Value=", self.get_color())
def display(self, screen):
pygame.draw.rect(screen, self.get_color(), Rect((self.get_xCoor(),self.get_yCoor()),
(self.get_boxLen(),self.get_boxWid())))
我认为您的问题在于如何使用datetime来控制何时下移块 您应该查看pygame.time模块。我建议您创建一个变量(例如wanted_fps),其中包含一个整数,表示您希望无止境循环每秒运行的频率。 然后,您的代码应该如下所示:
wanted_fps = 1 # how often I want the endless loop to run per second
fpsHandler = pygame.time.Clock() # fpsHandler created to handle fps
while(True): # our endless loop
# do something awesome
fpsHandler.tick(wanted_fps) # makes sure fps isn't higher than wanted_fps
您可以在此处阅读pygame.time的相关内容:
此外,如果您想要一个似乎没有事件处理延迟,您可以将wanted_fps设置为60,然后使用某种计数器变量(例如mycounter)来处理希望事件发生的频率。假设你在无尽的循环中做了这样的事情:
# handle events
if mycounter > 0:
mycounter -= 1
else:
# do something amazing
mycounter = 30
你每秒做60/30=2次令人惊奇的事情,但你每秒处理事件60次,因此你似乎可以立即响应事件。因为你通过向startTime添加1来设置stopTime,当startTime变为59时,stopTime将变为大于60的值。startTime然后返回到0,因为这是新的一分钟,您被卡住了。index=1,startTime永远不会大于stopTime。if或elif语句的计算结果都不是True。如果你的目标是在重新绘制盒子之前加入一个延迟,考虑一下这样做……/P>
pauseLength = 120
然后在游戏循环中
pygame.time.delay(pauseLength)
这也将允许您加快游戏速度,以增加以后的难度。尝试从命令行运行它,如果需要毫秒,则查看是否出现任何错误,而不是
startTime=str(datetime.now())
内置pygame.time.get\u ticks()
如果您的解决方案不是使用延迟函数,我会投票支持这个答案。不阻止事件处理的东西会更好。@Haz您知道一种方法不会阻止事件处理,并且仍然可以在绘制之间提供准确的时间增量吗?同样对于这样一场比赛,我不确定在平局之间我是否会处理多个事件。例如,一个块在屏幕上用一个绘图旋转两个位置。我会跟踪自砖块上次向下移动以来的帧数,并在该帧数超过设定值时更新其位置(最有可能基于所需的帧率),然后重置通过的帧数。在我玩过的大多数俄罗斯方块实现中,你可以水平移动一块砖块,即使砖块是垂直悬挂的。但在达到滴答值之前,pygame会执行延迟。是一样的。这是我的观点。据我所知,在设置任何帧速率时,都没有处理事件的方法。块向下移动是否与帧速率有关并不重要。块的垂直移动不需要人为限制帧速率。通过这样做,你限制了水平移动,潜在的音频播放,以及游戏中基于一个物体的移动而进行的任何其他事情。非常感谢!这解决了我的问题与随机停止和使用fps将更容易调整的难度。