簡體   English   中英

pygame窗口沒有響應

[英]The pygame window isnt responding

我是一個初學者,正在嘗試編寫游戲代碼。 它一直運行良好,但是每當我啟動它時,它的狀態就達到一品脫,並且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()

這是因為您一次又一次地調用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...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM