返回

python-Pygame gameloop无法重新启动

发布时间:2022-03-07 15:50:10 438
# node.js

我有一个关于重启和退出选项的问题。每当我按下“q”键后;“墙”;它会按预期退出程序,但c按钮似乎无法重新启动gameloop(),但我找不到原因。

我确实尝试在同一行中添加game_over和game_close=False,但这似乎并没有解决问题。我还尝试添加Else gameloop(),但也没有解决这个问题。

import pygame 
import time

pygame.init()

dis_width = 800
dis_height  = 600

dis=pygame.display.set_mode((dis_width,dis_height))
pygame.display.set_caption('Snake delux')

#define colors
blue=(0,0,255)
red=(255,0,0)
white = (255, 255, 255)
black = (0, 0, 0)

#starting values
x1_change = 0       
y1_change = 0
x1 = dis_width/2
y1 = dis_height/2
#game parameters
snake_speed = 144
snake_block = 10
font_style = pygame.font.SysFont(None, 30)
game_over = False
game_close = False
clock = pygame.time.Clock()

def message(msg, color):
    mesg = font_style.render(msg, True, color)
    dis.blit(mesg, [dis_width/3, dis_height/3])

def gameLoop():
    global game_over 
    global game_close 
    global x1_change
    global y1_change
    global x1
    global y1
        

    while not game_over:

        while game_close == True:
            dis.fill(black)
            message("Press Q-Quit or C-Play Again", red)
            pygame.display.update()

            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        game_over = True
                        game_close = False
                    if event.key == pygame.K_c:
                        gameLoop()


        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_over = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x1_change = -snake_block
                    y1_change = 0
                elif event.key == pygame.K_RIGHT:
                    x1_change = snake_block
                    y1_change = 0
                elif event.key == pygame.K_UP:
                    y1_change = -snake_block
                    x1_change = 0
                elif event.key == pygame.K_DOWN:
                    y1_change = snake_block
                    x1_change = 0

        if x1 >= dis_width or x1 = dis_height or y1 
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像