简体   繁体   English

pygame显示的是黑屏

[英]Pygame display's a black screen

I did a bit of research to see if i could solve the issue that way, but didn't seem to find anything to solve my problem. 我做了一些研究,看我是否可以通过这种方式解决问题,但似乎没有找到解决问题的办法。 I found both of these : Why isn't my pygame display displaying anything? 我发现了这两个: 为什么我的pygame显示器不显示任何内容? and Confused at why PyGame display's a black screen . PyGame为何显示黑屏感到困惑 I tried to solve my problem with what was adviced in the comments but it didn't work, or the reason for the problem was different than mine. 我尝试使用评论中建议的内容解决问题,但没有成功,或者问题的原因与我的不同。

When i run the code the pygame window shows, but is just completely black, but no errors are called. 当我运行代码时,pygame窗口显示出来,但是完全是黑色的,但是没有错误被调用。

so here is the code (might be a bit long). 所以这是代码(可能有点长)。

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()

The rendering logic in the game loop just needs to be indented to it is a part of the while loop inside it, like this: 只需要缩进游戏循环中的渲染逻辑,使其成为其中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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM