Pygame是跨平台Python模块,专为电子游戏设计,包含图像、声音。建立在SDL基础上,允许实时电子游戏研发而无需被低级语言(如机器语言和汇编语言)束缚。基于这样一个设想,所有需要的游戏功能和理念都(主要是图像方面)都完全简化为游戏逻辑本身,所有的资源结构都可以由高级语言提供。
Pygame的编程其实可以理解为循环加事件实现。
安装:
pip3 install pygame
测试:(可忽略)
python -m pygame.examples.aliens
画一个厉害的画玩玩:
生成一个最简窗口:
pygame
sys exit
screen = pygame.display.set_mode((700,450
pygame.display.set_caption(
background = pygame.image.load(
event
event.type ==
exit()
画画实现:
实现文字显示、事件监听、画大部分图形,其中显示文字时需要中文字体库支持,可直接将对应的字体拷贝到目录下,方便程序的移植,而不要固定写Windows字体库的路径,所以相对路径是最好的解决方法。
Windows文字库所在位置:C:\Windows\Fonts
pygame
sys exit
math
screen = pygame.display.set_mode((700,450
pygame.display.set_caption(
co1=pygame.Color(255,255,20,10 co2=pygame.Color(255,255 co3=pygame.Color(0,255 cotextbg=pygame.Color(0,200 cotext=pygame.Color(255,255
ftext222=pygame.freetype.Font(,20
textrect222=ftext222.render_to(screen,(620,420),,fgcolor=cotext,bgcolor=cotextbg,rotation=0,size=20
texview333,texrect333=ftext222.render(,rotation=20,size=50
rectx = pygame.draw.rect(screen,(255,255),(330,289,30,20 rectx = pygame.draw.rect(screen,(380,0)
rectx = pygame.draw.rect(screen,co2,(270,270,200,5
circlex = pygame.draw.circle(screen,(250,200),50 circlex = pygame.draw.circle(screen,(450,180),50,1
ell = pygame.draw.ellipse(screen,40,500,350),1
alrectx = pygame.draw.arc(screen,co3,(344,244,60,0 * pi,1 * pi,3
event
event.type ==
exit()
壁球小游戏基本实现及Pygame的其他内容补充:
pygame
sys exit
winWidth=700
winHeight=450
ballX=1
ballY=1
fpsx=19
timefpsx=
winRoot= winWidth= winHeight=
mod=pygame.RESIZABLE
screen =
pygame.display.set_caption(
titleiconx=pygame.image.load(
backgroundx = pygame.image.load(
ballimgx=pygame.image.load(
ballrectx=
ballrectx=
ballrectx.left<0 ballrectx.right> ballX=- ballrectx.top<0 ballrectx.bottom> ballY=-
event
event.type ==
event.type == (,event.pos,
event.type == (
event.type == (,