python笔记----Pygame 游戏开发
1.游戏的原理
2.安装Pygame
pip install pygame
豆瓣源形式: pip install -i https://pypi.doubanio.com/simple/ --trusted-host pypi.doubanio.com pygame
3.Pygame 常用模块
官方网址:https://pygame.org/
例子:使用窗口模块
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode((320,240))
while True:
#添加检测事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
sysy.exit()
pygame.quit()
4.pygame 的基本使用
例子:制作一个跳跃的小球游戏
步骤:创建窗口、添加小球、移动小球、检测碰撞
import pygame
import sys
#初始化pygame
pygame.init()
size = width ,height = 640,480
color = (255,0,0)
screen = pygame.display.set_mode(size ) #设置窗口
ball= pygame.image.load(’./图片名称) #导入当前目录下的图片
ballrect = ball.get_rect() #获取图片位置
speed = [5,5] #设置移动速度初始值x轴、y轴
clock = pygame.time.Clock()
while True:
clock.tick(60) #每秒执行的次数
#添加检测事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
sysy.exit()
ballrect = ballrect.move(speed) #移动
#碰撞检测
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]
screen.flip(color)
screen.blit(ball,ballrect(位置)) #将图片显示到窗口
pygame.display.flip() #显示窗口内容
pygame.quit()
5.开发Flappy Bird 游戏
5.1.游戏介绍
5.2.游戏分析
对象:小鸟、管道
对象属性:小鸟的存活状态
对象方法:小鸟移动、管道移动
事件:计算得分、碰撞检测
5.3.实现步骤:
5.3.1.搭建主框架
5.3.2.创建小鸟类
5.3.3.创建管道类
5.3.4.计算得分
5.3.5.碰撞检测
5.4.程序编写
```python
```python
```python
```python
```python
import pygame
import sys
class Bird(object):
'''定义一个小鸟类‘’‘
def __init__(self):
'''定义初始化方法’‘’
self.birdRect = pygame.Rect(65,50,50,50)
#导入小鸟状态图片,放入列表中
self.birdState = [pygame.image.load('./图片名称0),
pygame.image.load('./图片名称1),
pygame.image.load('./图片名称2)]
#小鸟的初始化属性
self.status = 0
self.birdX = 120
self.birdY = 120
self.jump =False
self.jumpSpeed = 10
self.gravity = 5
self.dead = False
def birdUpdate(self):
'''定义移动方法'''
if self.jump:
#小鸟跳跃状态
self.jumpSpeed -=1
self.birdY -= self.jumpSpeed
else:
self.gravity +=0.2
self.birdY += self.gravity
self.birdRect[1] = self.birdY
class Pipeline(object):
'''定义一个管道类'''
def __init__(self):
'''定义初始化方法'''
self.wallx = 400
self.pineUp = pygame.image.load('./图片名称0)
self.pineDown = pygame.image.load('./图片名称0)
def updatePipeline(self):
'''定义移动方法'''
self.wallx -=5
if self.wallx < -80:
global score
score +=1
self.wallx = 400
def createMap():
'''创建地图'''
screen.blit(background,ballrect(位置)) #将图片显示到窗口
#显示管道
screen.blit(Pipeline.pineUp,(Pipeline.wallx,-300))
screen.blit(Pipeline.pineDown,(Pipeline.wallx,500))
Pipeline.updatePipeline()
#显示小鸟
if Bird.dead:
Bird.status = 2
elif Bird.jump:
Bird.status = 1
screen.blit(Bird.birdState[Bird.status],(Bird.birdX,Bird.birdY)
Bird.birdUpdate() #更新小鸟状态
#显示字体
font.render('Score:'str(score),-1,(255,255,255))
screen.blit(font.render('Score:'str(score),-1,(255,255,255)),(100,50))
pygame.display.update() #显示窗口内容
def checkDead():
upRect = pygame.Rect(Pipeline.wallx,-300,Pipeline.pineUp.get_width(),Pipeline.pineUp.get_height())
downRect =pygame.Rect(Pipeline.wallx,500,Pipeline.pineDown.get_width(),Pipeline.pineDown.get_height())
#检测矩形碰撞
if upRect.colliderect(Bird.birdRect) or downRect .colliderect(Bind.birdRect):
Bird.dead = True
#边界检测
if not 0< Bird.birdRect[1] < height:
Bird.dead = True
return True
else :
return False
def getResult():
'''获取总分'''
final_text1 = "Game Over"
final_text2 = "Your final score is :" + str(score)
ft1_font = pygame.font.SysFont("Arial",70)
ft1_surf = font.render(final_text1,1,(242,3,36))
ft2_font = pygame.font.SysFont("Arial",70)
ft2_surf = font.render(final_text2,1,(242,3,36))
screen.blit(ft1_surf ,screen.get.width()/2 - ft1_surf.get_width()/2,100])
screen.blit(ft2_surf ,screen.get.width()/2 - ft1_surf.get_width()/2,200])
pygame.display.update()
if __name__ == 'main__':
'''主程序'''
#初始化pygame
pygame.init()
pygame.font.init() #初始化字体
font=pygame.font.SysFont(None,50)
size = width ,height = 640,480
screen = pygame.display.set_mode(size ) #设置窗口
clock = pygame.time.Clock()
color = (255,0,0)
Bird = Bird() #实例化小鸟类
Pipeline = Pipeline()
#计算得分
score = 0
while True:
clock.tick(60) #每秒执行的次数
#轮询事件检测
for event in pygame.event.get():
if event.type == pygame.QUIT:
sysy.exit()
if event.type == (pygame.KEYDOWN or event.type == pygame.MOUSEBUTTONDOWN) and not Bird.dead :
Bird.jump = True
Bird.gravity = 5
Bird.jumpSpeed = 10
background= pygame.image.load('./图片名称) #导入当前目录下的图片
if checkDead():
getResult()
else:
createMap()
pygame.quit()