Pygame是一個多用于游戲開發的模塊。
本文實例主要是在演示框里實現一個移動的矩形實例代碼,完整代碼如下:
#moving rectangle projectimport pygamefrom pygame.locals import *pygame.init()screen = pygame.display.set_mode((600,500))pygame.display.set_caption("Drawing Rectangles")pos_x = 300pos_y = 250vel_x = 2vel_y = 1while True: for event in pygame.event.get(): if event.type in (QUIT,KEYDOWN): pygame.quit() screen.fill((0,0,200)) # move the rectangle pos_x += vel_x pos_y += vel_y # keep rectangle on the screen if pos_x > 500 or pos_x < 0: vel_x = -vel_x if pos_y > 400 or pos_y < 0: vel_y = -vel_y # draw the rectangle color = 255,255,0 width = 0 #solid fill pos = pos_x,pos_y,100,100 pygame.draw.rect(screen,color,pos,width) pygame.display.update() 演示如下:

總結
以上就是本文關于Python通過Pygame繪制移動的矩形實例代碼的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
|
新聞熱點
疑難解答