[英]PyGame window not opening; no errors
我正在使用pygame
来创建射击游戏,代码对我来说看起来很完美,但我的pygame
窗口没有打开。 这是我的代码:
#Modules
import pygame as py
#initialize
py.init()
#gameloop
running = True
while running:
for event in py.event.get():
if event.type == py.QUIT:
running = False
#screen
screen = py.display.set_mode((800,600))
#title
py.display.set_caption("Shooter")
#icon
icon = py.image.load("shooting-game.png")
py.display.set_icon(icon)
#player
icon2 = py.image.load("clipart1504254.png")
playerx = 889
playery = 493
def main():
screen.blit(icon2, (playerx, playery))
#bg
screen.fill((252,0,0))
py.display.update()
main()
我没有收到任何错误,但窗口没有打开。
窗口没有显示,因为屏幕只更新一次,您需要在游戏循环中移动它和main()
函数。
python中的顺序很重要。 我将您的一些代码移到了 while 循环中,并重新排列了代码:
#Modules
import pygame as py
#initialize
py.init()
def main():
screen.blit(icon2, (playerx, playery))
#screen
screen = py.display.set_mode((800,600))
#title
py.display.set_caption("Shooter")
#icon
icon = py.image.load("shooting-game.png")
py.display.set_icon(icon)
icon2 = py.image.load("clipart1504254.png")
#player
playerx = 889
playery = 493
#gameloop
running = True
while running:
screen.fill((252,0,0))
main()
py.display.update()
for event in py.event.get():
if event.type == py.QUIT:
running = False
另外,对于像 blitting 对象这样的简单事情,我不推荐使用函数。
您的 while 循环需要在定义窗口之后进行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.