今天是我的第一天,pygame
我不明白为什么此代码不起作用,pygame
窗户是黑色,没有响应,并且没有图像显示
import pygame
pygame.init()
screen_width=800
screen_height=800
screen=pygame.display.set_mode([screen_width,screen_height])
screen.fill((255,255,255))
Quit=input("Press'Y' is you want to quit")
if Quit == "Y":
pygame.display.quit()
Board = pygame.image.load("TicTacToeBoard.jpg")
screen.blit(Board,(0,0))
pygame.display.flip()
所有PyGame程序都有一个事件循环。这是一个连续的循环,接受来自窗口管理器/操作环境的事件。事件是诸如鼠标移动,按钮单击和按键之类的事件。如果您的程序不接受事件,最终启动程序将认为它已停止响应,并可能提示用户终止它。
您现有的代码从控制台获取输入。如果您使用线程,可以在PyGame中完成,然后将事件发回到主循环。但是通常,将退出作为事件进行处理会更容易。在下面的代码中,我已经处理了退出QUIT
事件并按的问题Q。
import pygame
pygame.init()
screen_width=800
screen_height=800
screen=pygame.display.set_mode([screen_width,screen_height])
Board = pygame.image.load("TicTacToeBoard.jpg")
clock = pygame.time.Clock()
# Main Event Loop
exiting = False
while not exiting:
# Handle events
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
exiting = True
elif ( event.type == pygame.MOUSEBUTTONUP ):
# On mouse-click
mouse_pos = pygame.mouse.get_pos()
print( "Mouse Click at "+str( mouse_pos ) )
elif ( event.type == pygame.KEYUP ):
if ( event.key == pygame.K_q ):
# Q is quit too
exiting = True
# Paint the screen
screen.fill((255,255,255))
screen.blit(Board,(0,0))
pygame.display.flip()
# Limit frame-rate to 60 FPS
clock.tick_busy_loop(60)
此外,此代码还将帧速率限制为60 FPS。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句