Python通過Pygame繪制移動的矩形實例代碼
更新時間:2018年01月03日 15:20:05 作者:明柳夢少
這篇文章主要介紹了Python通過Pygame繪制移動的矩形實例代碼,具有一定借鑒價值,需要的朋友可以參考下
Pygame是一個多用于游戲開發(fā)的模塊。
本文實例主要是在演示框里實現(xiàn)一個移動的矩形實例代碼,完整代碼如下:
#moving rectangle project
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("Drawing Rectangles")
pos_x = 300
pos_y = 250
vel_x = 2
vel_y = 1
while 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繪制移動的矩形實例代碼的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
相關文章
Python使用PyAudio制作錄音工具的實現(xiàn)代碼
這篇文章主要介紹了Python使用PyAudio制作錄音工具,音頻錄制與視頻錄制相似,也是以數(shù)據(jù)幀的方式錄制保存,這次使用強大的第三方包PyAudio和內(nèi)置的wave模塊編寫,需要的朋友可以參考下2022-04-04
Python 3.6 -win64環(huán)境安裝PIL模塊的教程
PIL功能非常強大,但API卻非常簡單易用。這篇文章主要介紹了Python 3.6 -win64環(huán)境安裝PIL模塊的教程,需要的朋友可以參考下2019-06-06

