Pygame - window 打开但 fill() 不起作用并且未检测到任何事件
Pygame - window opens but fill() doesn't work and no events detected
我正在创建一个简单的 pygame 程序(PyGame 1.9.6 on Python 3.7),但是我的 while 循环中的一些代码似乎不起作用.当我 运行 程序时,window 打开,但屏幕没有充满黑色,当我按 "x"
时 window 也没有关闭
import pygame
# pygame setup
pygame.init()
# Open a window on the screen
width, height = 600, 600
screen = pygame.display.set_mode((width, height))
def main():
running = True
clock = pygame.time.Clock()
BLACK = (0,0,0)
while running:
clock.tick(5) # number of loops per second
print("tick")
screen.fill(BLACK)
for event in pygame.event.get():
print("event detected")
if event == pygame.QUIT:
running = False
pygame.display.update()
main()
在控制台中,按任意键或单击鼠标后,“滴答”显示正常,“检测到事件”显示。当我 运行 它时我没有收到任何错误。
如果事件 == pygame.QUIT:应该是如果 event.type == pygame.QUIT:
我通常这样使用那个事件:
if event.type == pygame.QUIT:
pygame.quit()
quit()
正如@Telan所说
if event==pygame.QUIT:
应该是
if event.type==pygame.QUIT:
但是要正确关闭 pygame,pygame.quit() 对关闭 pygame 模块很重要,这与 [=24 相反=]pygame.init()
虽然 sys.exit() 用于正确关闭主 python 程序。
from sys import exit
import pygame
if event.type == pygame.QUIT:
pygame.quit()
exit()
完整代码如下所示。享受吧!
import pygame
from sys import exit
# pygame setup
pygame.init()
# Open a window on the screen
width, height = 600, 600
screen = pygame.display.set_mode((width, height))
def main():
clock = pygame.time.Clock()
BLACK = (0, 0, 0)
while True:
clock.tick(5) # number of loops per second
print("tick")
screen.fill(BLACK)
for event in pygame.event.get():
print("event detected")
if event.type == pygame.QUIT:
pygame.quit()
exit()
pygame.display.update()
if __name__ == "__main__":
main()
我正在创建一个简单的 pygame 程序(PyGame 1.9.6 on Python 3.7),但是我的 while 循环中的一些代码似乎不起作用.当我 运行 程序时,window 打开,但屏幕没有充满黑色,当我按 "x"
时 window 也没有关闭import pygame
# pygame setup
pygame.init()
# Open a window on the screen
width, height = 600, 600
screen = pygame.display.set_mode((width, height))
def main():
running = True
clock = pygame.time.Clock()
BLACK = (0,0,0)
while running:
clock.tick(5) # number of loops per second
print("tick")
screen.fill(BLACK)
for event in pygame.event.get():
print("event detected")
if event == pygame.QUIT:
running = False
pygame.display.update()
main()
在控制台中,按任意键或单击鼠标后,“滴答”显示正常,“检测到事件”显示。当我 运行 它时我没有收到任何错误。
如果事件 == pygame.QUIT:应该是如果 event.type == pygame.QUIT:
我通常这样使用那个事件:
if event.type == pygame.QUIT:
pygame.quit()
quit()
正如@Telan所说
if event==pygame.QUIT:
应该是
if event.type==pygame.QUIT:
但是要正确关闭 pygame,pygame.quit() 对关闭 pygame 模块很重要,这与 [=24 相反=]pygame.init()
虽然 sys.exit() 用于正确关闭主 python 程序。
from sys import exit
import pygame
if event.type == pygame.QUIT:
pygame.quit()
exit()
完整代码如下所示。享受吧!
import pygame
from sys import exit
# pygame setup
pygame.init()
# Open a window on the screen
width, height = 600, 600
screen = pygame.display.set_mode((width, height))
def main():
clock = pygame.time.Clock()
BLACK = (0, 0, 0)
while True:
clock.tick(5) # number of loops per second
print("tick")
screen.fill(BLACK)
for event in pygame.event.get():
print("event detected")
if event.type == pygame.QUIT:
pygame.quit()
exit()
pygame.display.update()
if __name__ == "__main__":
main()