pygame窗口没有响应

时间:2018-08-07 02:44:30

标签: python pygame

我是初学者,正在尝试编写游戏代码。它一直运行良好,但是每当我启动它时,它的状态就达到一品脱,并且pygame窗口打开并保持黑色5秒钟,并说没有响应。当我尝试运行较旧版本的程序时,它运行正常。有人可以帮我弄这个吗 ?我正在使用python 3.7和pygame 1.9.4 代码如下

import pygame
import time
import random

pygame.init()

display_width = 800
display_height = 600

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)


car_width = 61

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('A bit Racey')
clock = pygame.time.Clock()

roadImg = pygame.image.load("road.jpg")

rockImg = pygame.image.load('rock.png')

carImg = pygame.image.load('racecar2.png')




def score(count):
    font = pygame.font.SysFont(None, 25)
    text = font.render("score:  "+str(count), True,red)
    gameDisplay.blit(text, (0,0))
#######
def things(thingx, thingy):
    gameDisplay.blit(rockImg, (thingx,thingy))
######

def road():
    gameDisplay.blit(roadImg, (0,0))



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

def text_objects(text, font):
    textSurface = font.render(text, True, red)
    return textSurface, textSurface.get_rect()

def message_display(text):

    largeText = pygame.font.Font('freesansbold.ttf',115)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.display.update()

    time.sleep(2)

    game_loop()



def crash():
    message_display('You Crashed')

def game_loop():

    pygame.event.get() 
    x = (display_width * 0.45)
    y = (display_height * 0.8)

    x_change = 0
######
    thing_startx = random.randrange(0, display_width)
    thing_starty = -400
    thing_speed = 10
    thing_width = 80
    thing_height = 80

    score_keep = 0
######
    gameExit = False


    while not gameExit:


        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -7
                if event.key == pygame.K_RIGHT:
                    x_change = 7
                if event.key == pygame.K_UP:
                    thing_speed+=2

                    score_keep+=2
                if event.key == pygame.K_DOWN:
                    score_keep-=0.5
                    thing_speed-=1


                if event.type == pygame.KEYUP:
                    if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:

                        x_change = 0
                if thing_speed < 0:
                    thing_speed == 0.1

        x += x_change
        gameDisplay.fill(white)

    ##########
        # things(thingx, thingy, thingw, thingh, color)

        road()
        things(thing_startx, thing_starty)
        thing_starty += thing_speed

        car(x,y)
    ##########
        score(score_keep)
        if x > display_width - car_width - 25 or x < -15:
            crash()

        if thing_starty > display_height:
            thing_starty = 0 - thing_height
            thing_startx = random.randrange(0,display_width)
            score_keep+=1
            thing_speed+=0.25


        if y < thing_starty+thing_height :


            if x > thing_startx and x < thing_startx + thing_width or x+car_width > thing_startx and x+car_width < thing_startx + thing_width :

                crash()
        pygame.display.update()
        clock.tick(60)

def help():
    runGame = False
    while not runGame:

        for event in pygame.event.get():

            time.sleep(10)
            if event.type == pygame.KEYDOWN:

                if event.key == pygame.K_h:
                    smallText = pygame.sysfont.SysFont("monoton",25)
                    TextSurf, TextRect = text_objects(" the left and rigth arrow keys are your controls ", smallText)
                    TextSurf, TextRect = text_objects(" the up and down keys are your difficulty settings ", smallText)
                    gameDisplay.blit(TextSurf, TextRect)
                if event.key == pygame.K_p:
                    time.sleep(5)
                    game_loop()
                    pygame.display.update()
                    time.sleep(60)
                runGame=True




def start_screen():
    road()
    largeText = pygame.sysfont.SysFont("monoton",50)
    TextSurf, TextRect = text_objects(" welcome to A Bit Racy ", largeText)
    TextRect.center = ((display_width/2),(display_height/3))
    gameDisplay.blit(TextSurf, TextRect)

    time.sleep(20)
    help()

    pygame.display.update()

pygame.event.clear()
start_screen()
pygame.quit()
quit()

1 个答案:

答案 0 :(得分:0)

这是因为您一遍又一遍地致电time.sleep

调用time.sleep时,您的应用程序似乎已挂起,因为在此期间您无法处理事件(例如绘图,鼠标和键盘事件)。

只需删除所有time.sleep电话。在事件循环驱动的应用程序中等待永远不是正确的方法。

如果您想“暂停”您的应用程序几秒钟,最好使用pygame.time.get_ticks()之类的东西来计数直到继续前进的时间,诸如:

ticks = pygame.time.get_ticks()

while pygame.time.get_ticks() - ticks < 5000:
    for event in pygame.event.get():
        ...your event handling...

    ...your screen drawing here...

...you reach this point after 5 seconds...