pygame退出按钮无法正常运行

时间:2019-01-11 12:08:03

标签: python pygame

我的退出按钮在我的pygame中有效,但仅部分起作用。一旦我刚运行了游戏,它就会起作用,但是当我在游戏中被杀死并且将game_loop重置后,退出按钮只会在我按下它时重置游戏循环,并且不会关闭窗口。好像已经改变了它的功能,使其与玩家被杀死的功能相同。

我将向您展示我的整个游戏代码,以使其变得更容易。 (顺便说一下,我的游戏目前正在更改,因此某些地方可能放置在错误的位置,而别担心。)

import pygame
import time
import random
pygame.init()
pygame.font.init()
dis_width = 500
dis_height = 500
game_exit = False
hit = False
FPS = 150
record = 0
game_display = pygame.display.set_mode((dis_width, dis_height))
pygame.display.set_caption('RACRZ')


clock = pygame.time.Clock()
#COLOURS
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0 ,0)
green = (0, 255, 0)
blue = (0, 0, 255)
sky_blue = (0, 136, 239)
#VEHICLE
bg = pygame.image.load('c:\\users\\riley\\pictures\\Saved Pictures\\spaceImg_bg.png')
carImg = pygame.image.load('c:\\users\\riley\\pictures\\Saved Pictures\\space invader1.png')
car_width = 32
car_height = 33


vel = 3
#projectile
enemyImg = pygame.image.load('c:\\users\\riley\\pictures\\saved pictures\\projectile1.png')
explosionImg = pygame.image.load('c:\\users\\riley\\pictures\\saved pictures\\explosion 1.png')


def car(x, y):
    game_display.blit(carImg, (x, y))

def enemy(enemyx, enemyy):
    game_display.blit(enemyImg, (enemyx, enemyy))
def enemy2(enemyx, enemyy):
    game_display.blit(enemyImg, (enemyx, enemyy))
def enemy3(enemyx, enemyy):
    game_display.blit(enemyImg, (enemyx, enemyy))
def enemy4(enemyx, enemyy):
    game_display.blit(enemyImg, (enemyx, enemyy))
def enemy5(enemyx, enemyy):
    game_display.blit(enemyImg, (enemyx, enemyy))
def enemy6(enemyx, enemyy):
    game_display.blit(enemyImg, (enemyx, enemyy))

def explode(x, y):
    game_display.blit(explosionImg, (x, y))
#TEXT
def textobjects(text, font):
    textsurface = font.render(text, True, green)
    return textsurface, textsurface.get_rect()

#RESTART MESSAGE
def message_display(text, colour, locationX, locationY):
    largeText = pygame.font.Font('freesansbold.ttf', 50)
    text = largeText.render('HIT', True, (colour))
    game_display.blit(text, (locationX, locationY))
    pygame.display.update()
    time.sleep(2)

    game_loop()

def collision(ex, ey, ew, eh, x, y, w, h):
    if x > ex and x < ex + ew or x + w > ex and x + w < ex + ew:
        if y > ey and y < ey + eh or y + h > ey and y + h < ey + eh:
            print ('HIT')
            hit = True
            game_loop()





def score(rounds):
    font = pygame.font.SysFont(None, 35)
    text = font.render(f'ROUND {rounds}', True, green)
    game_display.blit(text, (0,0))

#GAME LOOP
def game_loop():
#beggining car location
    x = dis_width * 0.45
    y = dis_height * 0.8
#movement variables
    x_change = 0
    y_change = 0

    loop_count = 0

    enemy_startx = random.uniform(0, dis_width)
    enemy_starty = -1000


    enemy2_startx = -1000
    enemy2_starty = random.uniform(0, dis_height)

    enemy3_startx = random.uniform(0, dis_width)
    enemy3_starty = -24

    enemy4_startx = random.uniform(0, dis_width)
    enemy4_starty = 500
    #goes from top side diagonaly to bottom left
    enemy5_startx = random.uniform(0, dis_width)
    enemy5_starty = -24
    #goes from bottom side diagonaly to top right
    enemy6_startx = random.uniform(0, dis_width)
    enemy6_starty = 500

    enemy_speed = 3
    enemy_width = 24
    enemy_height = 24




#game has not been quit
    game_exit = False
#while the game is running this will happen
    while not game_exit:
        #quit button
        for event in pygame.event.get():
            #assingning keys to movement    
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change -= vel
                elif event.key == pygame.K_RIGHT:
                    x_change += vel
                elif event.key == pygame.K_DOWN:
                    y_change += vel
                elif event.key == pygame.K_UP:
                    y_change -= vel
                elif event.type == pygame.K_ESCAPE:
                    print ('escape')
                    game_exit = True
            if event.type == pygame.QUIT:
                 game_exit = True



            #stops vehicle from moving after key press
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0
                if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    y_change = 0
        #moves vehicle
        y += y_change 
        x += x_change
        #display colour
        game_display.blit(bg, (0,0))
        #spawns objects       
        if loop_count >= 10:
            enemy3_startx += enemy_speed
            enemy3_starty += enemy_speed
            enemy3(enemy3_startx, enemy3_starty)    

        if loop_count >= 15:
            enemy4_startx -= enemy_speed 
            enemy4_starty -= enemy_speed
            enemy4(enemy4_startx, enemy4_starty)

        if loop_count >= 20:
            enemy5_startx -= enemy_speed
            enemy5_starty += enemy_speed
            enemy5(enemy5_startx, enemy5_starty)

        if loop_count >= 25:
            enemy6_startx += enemy_speed
            enemy6_starty -= enemy_speed
            enemy6(enemy6_startx, enemy6_starty)

        car(x,y)
        enemy(enemy_startx, enemy_starty)
        enemy2(enemy2_startx, enemy2_starty)

        score(loop_count)

        enemy2_startx += enemy_speed
        enemy_starty += enemy_speed
        if hit == True:
            game_loop = 0
#MAKES DISPLAY BOUNDARIES
#car boundaries
        if x > dis_width - car_width:
            x = 0 
        elif x < 0:
            x = dis_width - car_width
        elif y > dis_height - car_height:
            y = 0
        elif y < 0:
            y = dis_height - car_height
#boundaries for enemy1
        if enemy_starty > dis_height:
            enemy_starty = 0 - enemy_height
            enemy_startx = random.uniform(0, dis_width - enemy_width)
            #loop_count will count the number of times enemy1 passes through the screen
            loop_count = loop_count + 1
            print(loop_count)
        # ex, ey, ew, eh, x, y, w, h
        collision(enemy_startx, enemy_starty, enemy_width, enemy_height, x, y, car_width, car_height)
        collision(enemy2_startx, enemy2_starty, enemy_width, enemy_height, x, y, car_width, car_height)
        collision(enemy3_startx, enemy3_starty, enemy_width, enemy_height, x, y, car_width, car_height)
        collision(enemy4_startx, enemy4_starty, enemy_width, enemy_height, x, y, car_width, car_height)
        collision(enemy5_startx, enemy5_starty, enemy_width, enemy_height, x, y, car_width, car_height)
        collision(enemy6_startx, enemy6_starty, enemy_width, enemy_height, x, y, car_width, car_height)
#boundaries for enemy2
        if enemy2_startx > dis_width:
            enemy2_startx = 0 - enemy_width
            enemy2_starty = random.uniform(0, dis_height - enemy_height)
#enemy3 boundaries
        if enemy3_startx > dis_width:
            enemy3_startx = random.uniform(0, dis_width)
            enemy3_starty = -24        
#enemy4 boundaries
        if enemy4_startx < 0 - enemy_width:
            enemy4_startx = random.uniform(0, dis_width)
            enemy4_starty = 500
#enemy5 boundaries
        if enemy5_startx < 0 - enemy_width:
            enemy5_startx = random.uniform(0, dis_width)
            enemy5_starty = -24
#enemy6 boundaries
        if enemy6_startx > dis_width:
            enemy6_startx = random.uniform(0, dis_width)
            enemy6_starty = 500
        #record checker/ set
        global record
        roundnum = loop_count
        if roundnum > record:
            record = roundnum


        #updates screen after movements
        pygame.display.update()
        #FPS
        clock.tick(FPS)
#loops through code
if game_exit == True:
    pygame.quit()
game_loop()
print (f'your record is {record}')

这意味着分配您可以在我的代码中找到错误。 如果您还有其他问题,请让我知道。

1 个答案:

答案 0 :(得分:0)

这里的代码有点错误。在while True:循环中尝试以下操作:

    for event in pygame.event.get():
            if event.type == QUIT:
             pygame.quit()
             sys.exit()
            pygame.display.update()

[{The pygame.display.update”位在其他位置,因此请将其删除,然后复制并粘贴到istead中。]