2
遊戲開始以正確的速度運行。然而,當我按下退出鍵並返回到主菜單,然後返回遊戲時,遊戲會產生一點滯後(與當我將遊戲移到屏幕上並返回主菜單並開始遊戲時相同)。當我一遍又一遍地做這件事時,它會造成越來越多的滯後。從遊戲切換到菜單,然後創建滯後
在更多的「編碼」術語中,當我按下菜單上的「開始」時,它會調用reset(),它重置所有精靈的位置。當遊戲結束時,它將遊戲顯示在屏幕上,然後用戶按下退出鍵返回到菜單。
menuOn = 1
Rooms = []
running = True #Flags game as on
screen = None
(x1, y1) = (0, 0)
(x2, y2) = (0, -screenHeight)
allsprites = pygame.sprite.Group()
clock = pygame.time.Clock()
def placeholder1(screen):
Rooms = []
Rooms.append(Room())
global menuOn
menuOn = 0
pass
class Menu(object):
def __init__(self,screen):
self.screen = screen
self.title = startMenu
self.menu = genmenu(['START', lambda: placeholder1(screen)],['INFO', lambda: placeholder2(screen)], ['QUIT', lambda: quit()])
self.clock = pygame.time.Clock()
event = pygame.event.get()
self.menu.create(self.screen)
self.menu.choose(event)
self.main_loop()
def main_loop(self):
while menuOn == 1:
self.clock.tick(60)
events = pygame.event.get()
self.menu.choose(events)
self.screen.blit(self.title, (0, 0))
self.menu.create(self.screen)
pygame.display.flip()
class Room():
def __init__(self):
global wall, player, redFish, redfishes, greenFish, greenfishes, sharks, shark
for x in range(29):
wall = Wall()
wall.rect.topleft = (x*32,0) #top walls
walls.append(wall)
for x in range(29):
wall = Wall()
wall.rect.topleft = (x*32,screenHeight-32) #bottom walls
walls.append(wall)
for y in range(17):
wall = Wall()
wall.rect.topleft = (0, (y*32)+32) #left walls
walls.append(wall)
for y in range(17):
wall = Wall()
wall.rect.topleft = (screenWidth-32, (y*32)+32) #right walls
walls.append(wall)
reset()
def reset():
global playerpos
playerpos = [screenWidth/2, screenHeight/2]
player.rect.topleft = (playerpos[0], playerpos[1])
for greenFish in greenfishes:
greenFish.image = images["spr_greenfish"]
greenScoreList[greenfishes.index(greenFish)] = 0
greenFish.rect.topleft = random.randint(35,screenWidth-80),random.randint(35,screenHeight-80)
for redFish in redfishes:
redFish.rect.topleft = random.randint(35,screenWidth-80),random.randint(35,screenHeight-80)
sharks[0].rect.topleft = (100, 500)
sharks[1].rect.topleft = (300, 500)
sharks[2].rect.topleft = (500, 500)
sharks[3].rect.topleft = (700, 500)
while running:
clock.tick(60)
if menuOn == 1:
Menu(screen)
elif menuOn == 2:
GameOver(screen)
for event in pygame.event.get():
if event.type == QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == K_ESCAPE:
updateScoreToScreen(score)
menuOn = 1
reset()
allsprites.update()
screen.blit(ground, (0,screenHeight-100))
allsprites.draw(screen)
pygame.display.update()
聽起來好像你每次創建對象時都沒有刪除它們導致系統崩潰。更可能是所有這些牆體物體。他們每次都得到重新創建嗎? – 2015-02-05 20:26:13