笔记
创建一个窗口 1 2 3 4 5 6 7 8 import sys import pygame i=(1200 ,800 ) screen=pygame.display.set_mode(i)
看一下运行结果,他只显示了,标题没有显示窗口啊]

显示窗口可以用pygame.display.flip()
代码
1 2 3 4 5 6 7 8 9 10 11 import sys import pygame i=(1200 ,800 ) screen=pygame.display.set_mode(i) pygame.display.flip()
结果会很快就消失了

代码
1 2 3 4 5 6 7 8 9 10 11 12 13 import sys import pygame i=(1200 ,800 ) screen=pygame.display.set_mode(i) while True : pygame.display.flip()
结果可以看见一直都是显示
我们可以设置他的标题名字pygame.display.set_caption(标题名)
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 import sys import pygame i=(1200 ,800 ) screen=pygame.display.set_mode(i) m="abc" pygame.display.set_caption(m) while True : pygame.display.flip()
结果
然后我们发现他的窗口是关闭不了的,我们可以用sys模块进行判断关闭窗口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 import sysimport pygamei=(1200 ,800 ) screen=pygame.display.set_mode(i) m="abc" pygame.display.set_caption(m) while True : for event in pygame.event.get(): if event.type==pygame.QUIT: sys.exit() pygame.display.flip()
结果然后就可以关闭了
设置背景颜色 他默认是黑色,我们可以用screen.fill()
方法来填充颜色
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 import sysimport pygamei=(1200 ,800 ) screen=pygame.display.set_mode(i) m="abc" pygame.display.set_caption(m) bg=(230 ,230 ,230 ) while True : for event in pygame.event.get(): if event.type==pygame.QUIT: sys.exit() screen.fill(bg) pygame.display.flip()
结果
创建类 书上说的意思是如果这样写下去会代码很长,我们就可以创建一个类就可以避免这个代码太长不好管理的问题他默认是黑色,我们可以用screen.fill()
方法来填充颜色
我叫上面的代码分到两个文件里面
PingMuXianShi.py
文件里面内容,我这个文件是设置屏幕的我为了自己好认就自己改了个名字
1 2 3 4 5 6 7 8 9 class XianShi (): def __init__ (self ): self.width=1200 self.height=800 self.gb=(230 ,230 ,230 )
zhu1.py
这个是住文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 import sys import pygamefrom PingMuXianShi import XianShi def game (): xian_shi=XianShi() screen=pygame.display.set_mode((xian_shi.width,xian_shi.height)) pygame.display.set_caption("abc" ) while True : pygame.init() for event in pygame.event.get(): if event.type==pygame.QUIT: sys.exit() screen.fill(xian_shi.gb) pygame.display.flip()
构建飞船 我添加一个ship.py
文件
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 import pygame class Ship (): def __init__ (self,screen ): self.screen=screen self.ima=pygame.image.load('/home/zss/笔记/外星人入侵项目/ima/ship.bmp' ) self.rect=self.ima.get_rect() self.screen_rect=screen.get_rect() self.rect.centerx=self.screen_rect.centerx self.rect.bottom=self.screen_rect.bottom def blitme (self ): self.screen.blit(self.ima,self.rect)
运行结果
zhu1.py
里面代码添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 import sys import pygamefrom PingMuXianShi import XianShi from ship import Shipdef game (): xian_shi=XianShi() screen=pygame.display.set_mode((xian_shi.width,xian_shi.height)) pygame.display.set_caption("abc" ) ship=Ship(screen) while True : for event in pygame.event.get(): if event.type==pygame.QUIT: sys.exit() screen.fill(xian_shi.gb) ship.blitme() pygame.display.flip() game()
查看运行结果,可以看见有一个小船
重构
我们创建一个game_funcltions.py
文件用于关闭和屏幕显
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import sys import pygamedef check_events (): for event in pygame.event.get(): if event.type==pygame.QUIT: sys.exit() def update_screen (xian_shi,screen,ship ): screen.fill(xian_shi.gb) ship.blitme() pygame.display.flip()
主文件代码就变得很少了
下面是zhu1.py
文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 import sys import pygamefrom PingMuXianShi import XianShi from ship import Shipimport game_funcltions def game (): xian_shi=XianShi() screen=pygame.display.set_mode((xian_shi.width,xian_shi.height)) pygame.display.set_caption("外星人入侵" ) ship=Ship(screen) while True : game_funcltions.check_events() game_funcltions.update_screen(xian_shi,screen,ship) game()
结果
移动飞船 右移动 在python里面每一个键盘和鼠标垫事件都会pygame.event.get()
捕获到,然后我们就可以按一个建加飞船的x和y轴
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 import sys import pygamedef check_events (ship ): for event in pygame.event.get(): if event.type==pygame.QUIT: sys.exit() elif event.type==pygame.KEYDOWN: if event.key==pygame.K_RIGHT: ship.rect.centerx+=1 def update_screen (xian_shi,screen,ship ): screen.fill(xian_shi.gb) ship.blitme() pygame.display.flip()
上面添加的代码是
1 2 3 4 5 6 7 8 elif event.type==pygame.KEYDOWN: if event.key==pygame.K_RIGHT: ship.rect.centerx+=1
持续移动 上面的点一下他才运行,下面用到了两个事件,进行持续移动
KEYDOWN
如果你按键盘了他是会被执行
KEYUP
我们按键松了他是会被执行
game_funcltions.py
文件代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 import sys import pygamedef check_events (ship ): for event in pygame.event.get(): if event.type==pygame.QUIT: sys.exit() elif event.type==pygame.KEYDOWN: if event.key==pygame.K_RIGHT: ship.moving_right=True elif event.type==pygame.KEYUP: if event.key==pygame.K_RIGHT: ship.moving_right=False def update_screen (xian_shi,screen,ship ): screen.fill(xian_shi.gb) ship.blitme() pygame.display.flip()
修改的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 elif event.type==pygame.KEYDOWN: if event.key==pygame.K_RIGHT: ship.moving_right=True elif event.type==pygame.KEYUP: if event.key==pygame.K_RIGHT: ship.moving_right=False
zhu1.py
文件主代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 import sys import pygamefrom PingMuXianShi import XianShi from ship import Shipimport game_funcltions def game (): xian_shi=XianShi() screen=pygame.display.set_mode((xian_shi.width,xian_shi.height)) pygame.display.set_caption("外星人入侵" ) ship=Ship(screen) while True : game_funcltions.check_events(ship) ship.update() game_funcltions.update_screen(xian_shi,screen,ship) game()
里面添加了
运行结果

左右移动 上面的他只可以右移动,像对应的还有,按左键事件K_LEFT
我们用这个事件就可以叫他向左移动
game_funcltions.py
文件代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 import sys import pygamedef check_events (ship ): for event in pygame.event.get(): if event.type==pygame.QUIT: sys.exit() elif event.type==pygame.KEYDOWN: if event.key==pygame.K_RIGHT: ship.moving_right=True elif event.key==pygame.K_LEFT: ship.moving_left=True elif event.type==pygame.KEYUP: if event.key==pygame.K_RIGHT: ship.moving_right=False elif event.key==pygame.K_LEFT: ship.moving_left=False def update_screen (xian_shi,screen,ship ): screen.fill(xian_shi.gb) ship.blitme() pygame.display.flip()
ship.py
文件增加左移动值代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 import pygame class Ship (): def __init__ (self,screen ): self.screen=screen self.ima=pygame.image.load('/home/zss/笔记/外星人入侵项目/ima/ship.bmp' ) self.rect=self.ima.get_rect() self.screen_rect=screen.get_rect() self.rect.centerx=self.screen_rect.centerx self.rect.bottom=self.screen_rect.bottom self.moving_right=False self.moving_left=False def update (self ): if self.moving_right: self.rect.centerx+=1 if self.moving_left: self.rect.centerx-=1 def blitme (self ): self.screen.blit(self.ima,self.rect)
运行结果

设置飞船的飞行速度 game_funcltions.py
文件代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 import pygame class Ship (): def __init__ (self,ai_settings,screen ): self.screen=screen self.ai_settings=ai_settings self.ima=pygame.image.load('/home/zss/笔记/外星人入侵项目/ima/ship.bmp' ) self.rect=self.ima.get_rect() self.screen_rect=screen.get_rect() self.rect.centerx=self.screen_rect.centerx self.rect.bottom=self.screen_rect.bottom self.center=float(self.rect.centerx) self.moving_right=False self.moving_left=False def update (self ): if self.moving_right and self.rect.right < self.screen_rect.right: self.rect.centerx+=self.ai_settings.ship_speed_factor if self.moving_left and self.rect.left > 0 : self.rect.centerx-=self.ai_settings.ship_speed_factor def blitme (self ): self.screen.blit(self.ima,self.rect)
上面的代码介绍
1 2 3 4 5 6 7 8 9 10 if self.moving_right and self.rect.right < self.screen_rect.right: self.rect.centerx+=self.ai_settings.ship_speed_factor if self.moving_left and self.rect.left > 0 : self.rect.centerx-=self.ai_settings.ship_speed_factor
代码1
1 if self.moving_right and self.rect.right < self.screen_rect.right:
self.moving_right
:self.moving_right在game_funcltions文件里面有键盘监听事件按键按下右键去就会吧这个属性设置成True
self.rect.right
:self.rect.right获得外部矩形位置,就是图像的位置
self.screen_rect.right
:# self.screen_rect.right记录长度,就是窗口的长度
self.screen_rect.right是1200,self.rect.right如果他向左移动到1200就是到了最右边了
代码2
self.moving_left
:self.moving_left在game_funcltions文件里面有键盘监听事件按键按下右键去就会吧这个属性设置成True
self.rect.left
:self.rect.left记录右边框还下多少距离,如果是0就错误不执行
运行结果

重构 现在发现代码已经很长了,我们可以给check_events()
函数已经很长了
game_funcltions.py
文件代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 import sys import pygamedef check_keydown_events (event,ship ): if event.key==pygame.K_RIGHT: ship.moving_right=True elif event.key==pygame.K_LEFT: ship.moving_left=True def check_keyup_events (event,ship ): if event.key==pygame.K_RIGHT: ship.moving_right=False elif event.key==pygame.K_LEFT: ship.moving_left=False def check_events (ship ): for event in pygame.event.get(): if event.type==pygame.QUIT: sys.exit() elif event.type==pygame.KEYDOWN: check_keydown_events(event,ship) elif event.type==pygame.KEYUP: check_keyup_events(event,ship) def update_screen (xian_shi,screen,ship ): screen.fill(xian_shi.gb) ship.blitme() pygame.display.flip()
射击 更新中………………….