为什么程序中的#continue部分不起作用?

时间:2018-07-04 15:57:49

标签: python python-3.x pygame

它应该调用events()函数并通过再次在屏幕上显示该块来重新启动整个过程,为什么它不起作用? 我尝试脱离gameover(),尝试调用pygame.display.update(),但似乎无济于事[这是我的第一个python游戏,因此在键入代码时可能有些混乱。谁能帮我解决问题?

import pygame
import time

pygame.init()
global display_height
global display_width

display_height = 800
display_width = 800
class slither(object):

    def __init__(self,top):
        self.top=top
        self.x = display_height/2
        self.y = display_width/2
        self.x_change = 0
        self.y_change = 0
        self.gameOver = False
        self.gameLoop = False
        self.FPS=30
        self.brick_size=10

        self.black = (0,0,0)
        self.white = (255,255,255)
        self.red = (255,0,0)
        self.font = pygame.font.SysFont(None, 30)
        self.clock = pygame.time.Clock()


        self.events()
        self.message('',(0,0,0))
        self.gameover()



    def create_move(self, a, b):
        pygame.draw.rect(screen, self.black , [a,b,self.brick_size,self.brick_size])

    def message(self, msg, color):
        screen_text=self.font.render(msg, True, color)
        screen.blit(screen_text, [display_height/2, display_width/2])
        pygame.display.update()

    def events(self):

        while self.gameLoop == False:
            screen.fill(self.white)
            self.create_move(self.x,self.y)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.const=True

                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_LEFT:
                        self.x_change = -self.brick_size
                        self.y_change = 0
                    elif event.key == pygame.K_RIGHT:
                        self.x_change = self.brick_size
                        self.y_change = 0
                    elif event.key == pygame.K_UP:
                        self.y_change = -self.brick_size
                        self.x_change = 0
                    elif event.key == pygame.K_DOWN:
                        self.y_change = self.brick_size
                        self.x_change = 0
            if self.x_change == -self.brick_size:
                self.x -= self.brick_size
                y=self.y
                self.create_move(self.x, y)
            elif self.x_change == self.brick_size:
                self.x += self.brick_size
                y=self.y
                self.create_move(self.x, y)
            if self.y_change == -self.brick_size:
                self.y -= self.brick_size
                x=self.x
                self.create_move(x, self.y)
            elif self.y_change == self.brick_size:
                self.y += self.brick_size
                x=self.x
                self.create_move(x, self.y)
            if self.x == display_height or self.x < 0 or self.y == display_width or self.y < 0 :
                self.gameover()

            pygame.display.update()
            self.clock.tick(30)
    def gameover(self):
        while self.gameOver == False:
            screen.fill((255, 255, 255))
            self.message('Game over, Press any to continue or W to quit ', self.black)
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_w:
                        screen.fill(self.white)
                        self.message('Game Over, You lose', self.black)
                        time.sleep(2)
                        pygame.quit()
                        quit()
                    else:
                        self.events() #continue


screen=pygame.display.set_mode((display_height,display_width))
pygame.display.set_caption('Snake')
slither(0).events()
pygame.quit()
quit()

编辑:谢谢。

1 个答案:

答案 0 :(得分:1)

这就是我要做的全部更改。 基本上,由于该块仍在屏幕之外,因此gameover()曾经被再次执行,因此当我将其重新带到屏幕上时,它就起作用了。

else: #continue
 self.x=display_height/2
 self.y=display_width/2
 self.events()