1、介绍与安装
(1) pygame——2D游戏开发工具包
(2)安装: pip install pygame import pygame
)3)帮助文档: https://www.pygame.org/docs/
)4) Hello World
33558 www.Sina.com/: import pygame,sys等
引入相关包:pygame.init (
3358 www.Sina.com/http://www.Sina.com/: pygame.display.set _ mode ((400,300 ) )
初始化:处理游戏事件-更新游戏状态-绘制在屏幕上
3358 www.Sina.com/: foreventinpygame.event.get () : # )结束游戏事件if event.type==quit 3360 pygame.quit ) sset
例如:
#引入相关软件包import sys,pygame#进行初始化操作pygame.init () size=width,height=320,240 speed=[ 2,2 ] black=0,0, 0# 要初始化的screen=pygame.display.set _ mode (size ) ball=pygame.image.load (intro _ ball.gif ' ) ballrect=ballrect。 t.type==pygame.quit : sys.exit ) #更新游戏状态ball rect=ball rect.move ifballrect.left0orballrect.right width 333330 在ifballrect.top0orballrect.bottom height 3360 speed屏幕上显示screen.fill(black ) screen.blit (ball,ballrect ) pygame.displabll
)1)游戏中的文字
import pygame,sys pygame.init (screen=pygame.display.set _ mode ) 500 ) ) red=pygame.color ) 255,0, 0 ) #字体font=pygame.font.get _ fonts (# print ) font ) #统一)系统使用缺省字体,#font=pygame.font.SysFont font=pygame.font.font (simhei.TTF ),40 )字符对象为surface对象text=font.render ),得到True,red ) #字符加载while ttte
用RGB表示时,可取值的范围:0-255
RGBA表示透明色: a (阿尔法) :0-255
导入系统、 pygame#初始化pygamepygame.init(#屏幕对象screen=pygame.display.set_mode ) ) 500 500 ) )加载图像ball=pygame.iii intro_ball.gif ' )红色red=pygame.color (255,20, 255 )游戏主循环while True: # (事件for event in pygame.event.get ) : if event.type==pygame.quit : pygame.quit ) sygame ) ) sygame线pygame . 200 ) #矩形pygame.draw.rect ) screen,red,) 10,20 )减去10 )画圆pygame.draw.circle(screen,red,) 100,100 )
图像加载: BG=pygame.image.load(BG.png ) ) ) ) ) ) ) )。
在屏幕上绘制: screen.blit(BG,bg.get_rect ) )
4、游戏音效
5、动画切换
6、精灵与精灵组合
精灵:精灵是一种可以在屏幕上移动的图形对象,可以将其视为与其他图形对象交互的小图像。 精灵图像可以使用pygame绘制函数绘制IDE图像,也可以绘制原始图像文件
组:用于管理组中向导的绘制和更新的容器
7、碰撞检测
两个向导之间的矩形检测: pygame.sprite.collide _ rect (sprite _ 1,sprite _2pygame.sprite.collide _ rect _ ratio ) 0.5
两个向导之间的圆形检测: pygame.sprite.collide _ circle (sprite _ 1,sprite_2) ) ) ) ) ) ) ) ) ) ) )。
两个向导之间的像素掩码检测: pygame.sprite.collide _ mask (sprite _ 1,sprite_2) ) ) ) ) ) ) ) ) ) ) ) )。
向导和向导组之间的冲突检测: pygame.sprite.spritecollideany (sprite,sprite_group,bool ) ) )。
# 1.引入相关软件包import sys,pygame# 2. pygame生成pygame.init(size=width,height=320,240 speed=[ 2,2 ] black=0 0 0# 3.屏幕对象surface screen=pygame.display.set _ mode ((320, 240 ) ) classblock ) pygame.sprite.sprite ) 360#coome )来获取# anditsxandypositiondef _ init _ (self,color,wer ) init_pos ) : # calltheparentclass (sprite ) constructor pygame.sprite._ init _ (self ) create an andfillitwithath thiscouldalsobeanimageloadedfromthedisk.self.image=pygame.surface (height ) () self.image.fill(color ) ) (fetchtherectangleobjectthathasthedimensionsoftheimage ) ) ) ) ) ) ) xan drect.y self.rect=self.image.get _ rect (实例化向导对象sprite _1=block (pygame.color (255,0,0 ),50 ) 50 )=block 90 ) # 4.游戏主循环while 1: # 在处理游戏事件foreventinpygame.event.get (3360 if event.type==pygame.quit : sys.exit ) )的屏幕上,显示向导screen.blit () sprite_1.rect ) screen.blit (sprite_2. image,sprite_2.rect ) )绘制矩形的sprite _2) print('rest ',rest )