为什么Pygame会变成黑屏

2024-05-16 10:54:51 发布

您现在位置:Python中文网/ 问答频道 /正文

好的,所以基本上屏幕出现时需要输入我的名字,当我输入时,pygame窗口打开了,但是当它打开时,它只是一个黑屏,它也没有出现错误。 有什么想法吗?:)

import time

import random`enter code here`
import pygame
from pygame import*
pygame.init()
myname=input('What is your name')
#set the window size
window= pygame.display.set_mode((800,600) ,0,24)
pygame.display.set_caption("Fruit Catch")
#game variables
myscore=0
mylives=3
mouth_x=300
fruit_x=250
fruit_y=75
fruitlist=['chicken.gif','broccoli.gif']
#prepare for screen
myfont=pygame.font.SysFont("Britannic Bold", 55)
label1=myfont.render(myname, 1, (240, 0, 0))

label3=myfont.render(str(mylives), 1, (20, 255, 0))
#grapchics
fruit=pygame.image.load('data/chicken.gif')
mouth=pygame.image.load('data/bowl.gif')
backGr=pygame.image.load('data/kfc.jpg')
#endless loop
running=True
while running:
   #code to move fruit
    if fruit_y>=460:#check if at bottom, if so prepare new fruit
      fruit_x=random.randrange(50,530,1)
      fruit_y=75
      fruit=pygame.image.load('data/'+fruitlist[random.randrange(0,2,1)])
    else:fruit_y+=5
   #check collision
    if fruit_y>=440:
           if fruit_x>=mouth_x and fruit_x<=mouth_x+110:
                              myscore+1
                              fruit_y=600 #move it off screen
            #detect key events
    for event in pygame.event.get():
                     if (event.type==pygame.KEYDOWN):
                         if (event.key==pygame.K_LEFT):
                                 mouth_x-=20
                         if (event.key==pygame.K_RIGHT):
                                 mouth_x+=20
#update screen from back first
label2=myfont.render(str(myscore), 1, (20, 255, 0))
window.blit(backGr,(0,0))
window.blit(mouth, (mouth_x,440))
window.blit(fruit,(fruit-x, fruit_y))
window.blit(label1, (174, 537))
window.blit(label2, (700, 157))
window.blit(label3, (700, 400))
pygame.display.update()

Tags: imageimporteventdataifloadrandomwindow
2条回答

使用这个正确缩进的代码。。。 请注意,除了缩进之外,您还犯了其他非常愚蠢的错误

=>;myscore+1什么也不做。
=>;window.blit(fruit,(fruit-x, fruit_y))有{},而不是{}

import time
import random
import pygame
from pygame import*
pygame.init()
myname=input('What is your name')
#set the window size
window= pygame.display.set_mode((800,600) ,0,24)
pygame.display.set_caption("Fruit Catch")
#game variables
myscore=0
mylives=3
mouth_x=300
fruit_x=250
fruit_y=75
fruitlist=['chicken.gif','b.gif']
#prepare for screen
myfont=pygame.font.SysFont("Britannic Bold", 55)
label1=myfont.render(myname, 1, (240, 0, 0))

label3=myfont.render(str(mylives), 1, (20, 255, 0))
#grapchics
fruit=pygame.image.load('chicken.gif')
mouth=pygame.image.load('a.gif')
backGr=pygame.image.load('b.gif')
#endless loop
running=True
while running:
    #code to move fruit
    if fruit_y>=460:#check if at bottom, if so prepare new fruit
        fruit_x=random.randrange(50,530,1)
        fruit_y=75
        fruit=pygame.image.load(fruitlist[random.randrange(0,2,1)])
    else:
        fruit_y+=5
    #check collision
    if fruit_y>=440:
        if fruit_x>=mouth_x and fruit_x<=mouth_x+110:
            myscore+=1
            fruit_y=600 #move it off screen
    #detect key events
    for event in pygame.event.get():
        if (event.type==pygame.KEYDOWN):
            if (event.key==pygame.K_LEFT):
                mouth_x-=20
            if (event.key==pygame.K_RIGHT):
                mouth_x+=20
    #update screen from back first
    label2=myfont.render(str(myscore), 1, (20, 255, 0))
    window.blit(backGr,(0,0))
    window.blit(mouth, (mouth_x,440))
    window.blit(fruit,(fruit_x, fruit_y))
    window.blit(label1, (174, 537))
    window.blit(label2, (700, 157))
    window.blit(label3, (700, 400))
    pygame.display.update()

从back first更新屏幕的最后一段代码,只有在whilerunning:循环结束时才会运行。所以你需要把所有这些行向前一行。在

相关问题 更多 >