Pygame显示为黑屏

时间:2015-11-04 15:00:17

标签: python

我做了一些研究,看看我是否可以通过这种方式解决问题,但似乎没有找到解决我问题的方法。我找到了这两个:Why isn't my pygame display displaying anything?Confused at why PyGame display's a black screen。我尝试用评论中提出的建议来解决我的问题,但它没有用,或者问题的原因与我的不同。

当我运行代码时,pygame窗口显示,但只是完全黑色,但没有调用错误。

所以这里是代码(可能有点长)。

import pygame
import time
import random

pygame.init()

display_width = 700
display_height = 900

player_width = 140
player_height = 100

gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Baby shower')
clock = pygame.time.Clock()

greenkidimg = pygame.image.load('kid green.png')
bluekidimg = pygame.image.load('kid blue.png')
redkidimg = pygame.image.load('kid red.png')
catcherimg = pygame.image.load('catcher.png')
background = pygame.image.load('background.png')
#image loads

def player(x,y):
    gameDisplay.blit(catcherimg,(x,y))

def bluekid(bluex, bluey, bluew, blueh):
    gameDisplay.blit(bluekidimg, (bluex, bluey))

def redkid(redx, redy, redw, redh):
    gameDisplay.blit(redkidimg, (redx,redy))

def greenkid(greenx, greeny, greenw, greenh):
    gameDisplay.blit(greenkidimg(greenx, greeny))

def game_loop():

    x = (display_width*0.45)
    y = (display_height*0.8)

    x_change = 0

    blue_width = 66
    bluex_start = random.randrange (0, display_width-blue_width)
    bluey_start = -600
    blue_speed = 15
    blue_height = 78
    green_width = 66
    greenx_start = random.randrange (0, display_width-green_width)
    greeny_start = -600
    green_speed = 15
    green_height = 78
    red_width = 66
    redx_start =  random.randrange (0, display_width-red_width)
    redy_start = -600
    red_speed = 15
    red_heigth = 78
    #greenkid,redkid,bluekid-start,speed,h/w

    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.type == pygame.K_LEFT:
                    x_change = -20

                elif event.type == pygame.K_RIGHT:
                    x_change = 20

            if event.type == pygame.KEYUP:
                if event.type == pygame.K_LEFT or pygame.K_RIGHT:
                    x_change = 0

    x += x_change

    gameDisplay.blit(background(0,0))
    player(x,y)
    bluekid(bluex, bluey, bluew, blueh)
    redkid(redx, redy, redw, redh)
    greenkid(greenx, greeny, greenw, greenh)
    #display

    pygame.display.update()
    clock.tick(30)

game_loop()
pygame.quit()
quit()

1 个答案:

答案 0 :(得分:0)

游戏循环中的渲染逻辑只需要缩进到它内部的while循环的一部分,如下所示:

while not gameExit:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

        if event.type == pygame.KEYDOWN:
            if event.type == pygame.K_LEFT:
                x_change = -20

            elif event.type == pygame.K_RIGHT:
                x_change = 20

        if event.type == pygame.KEYUP:
            if event.type == pygame.K_LEFT or pygame.K_RIGHT:
                x_change = 0

    x += x_change

    gameDisplay.blit(background(0,0))
    player(x,y)
    bluekid(bluex, bluey, bluew, blueh)
    redkid(redx, redy, redw, redh)
    greenkid(greenx, greeny, greenw, greenh)
    pygame.display.update()
    clock.tick(30)