您的位置:首页 > 编程语言 > Python开发

基于Python中pygame模块实现谷歌内置恐龙小游戏的开发

2019-06-13 19:52 1201 查看

前言:最近在谷歌浏览器发现了一件很有趣的事情,就是谷歌浏览器在离线的时候发现它有一款内置的离线小游戏,我把它称为“恐龙跳跳乐”。游戏界面如下:

之后我心血来潮的翻看了它的源码,发现有足足2700多行左右。又加上最近老师讲了pygame的模块,我便想着如何用Python实现这个“恐龙跳跳龙”。我咨询了老师,问他Python实现这个游戏的整体思路是什么,老师耐心向我讲解了一番。于是我便想着1用面向对象的方法写,我的思路如下图所示:

字很潦草,别介意,凑合凑合。

我在网上找了一些进行此游戏设计的图片、声音等素材便进行如下构思

import pygame
from pygame.locals import *  # 导入pygame常量

SCREENWIDTH=822
SCREENHEIGHT=260
FPS=30

# 定义背景对象
class MyMap():
def __init__(self,x,y):  # 构造函数
self.bg=pygame.image.load(r'D:\pycharm\python核心编程\小游戏\bg.png').convert_alpha()  # 进行图形判断,将背景与其相关的图片透明化
self.x=x
self.y=y

def map_rolling(self):  # 滚动
if self.x<-790:
self.x=800
else:
self.x-=5  # 每帧以5个像素,表示像素移动的距离

def map_update(self):
SCREEN.blit(self.bg,(self.x,self.y))

# 恐龙类
from itertools import cycle  # 让我们读取反复不断的用
class Dinosaur():
def __init__(self):
# 初始化恐龙图片的矩阵
self.rect=pygame.Rect(0,0,0,0)
self.jumpState=False  # 状态
self.jumpHeight=130  # 跳跃高度
self.lowest_y=140  # 步行高度
self.jumpValue=0  # 跳跃速度
# 加载图片
self.dinosaurIndex=0
self.dinosaurIndexGen=cycle([0,1,2])
self.dinosaur_img=(
pygame.image.load(r'dinosaur1.png').convert_alpha(),
pygame.image.load(r'dinosaur2.png').convert_alpha(),
pygame.image.load(r'dinosaur3.png').convert_alpha(),
)
# 加载声音
self.jump_audio=pygame.mixer.Sound('jump.wav')
self.rect.size=self.dinosaur_img[0].get_size()
self.x=50
self.y=self.lowest_y
self.rect.topleft=(self.x,self.y)

def jump(self):
self.jumpState = True

def move(self):
if self.jumpState:
if self.rect.y >= self.lowest_y:
self.jumpValue = -5  # 向上移动5个矩阵
if self.rect.y <= self.lowest_y - self.jumpHeight:
self.jumpValue = 5
self.rect.y += self.jumpValue
if self.rect.y >= self.lowest_y:
self.jumpState = False  # 关闭跳跃状态

def draw_dinosaur(self):
# 匹配恐龙动图
dinosaurIndex=next(self.dinosaurIndexGen)
SCREEN.blit(self.dinosaur_img[dinosaurIndex],
(self.x,self.rect.y))

# 障碍物类
import random
class Obstacle():
def __init__(self):
self.rect=pygame.Rect(0,0,0,0)
self.stone=pygame.image.load(r'stone.png').convert_alpha()
self.cacti=pygame.image.load(r'cacti.png').convert_alpha()
r=random.randint(0,1)
if r==0:
self.image=self.stone
else:
self.image=self.cacti
self.rect.size=self.image.get_size()
self.width,self.height=self.rect.size
self.x=800
self.y=200-(self.height/2)
self.rect.center=(self.x,self.y)

def obstacle_move(self):
self.rect.x -= 5

def draw_obstacle(self):
SCREEN.blit(self.image,(self.rect.x,self.rect.y))

# 游戏结束方法
def game_over():
pass

def mainGame():
over=False
global SCREEN,FPSCLOCK  # 全局变量global
pygame.init()  # 初始化
FPSCLOCK=pygame.time.Clock()
SCREEN=pygame.display.set_mode((SCREENWIDTH,SCREENHEIGHT))
pygame.display.set_caption('google small game')
# 创建地图
bg1=MyMap(0,0)
bg2=MyMap(800,0)
# 创建恐龙对象
dinosaur = Dinosaur()
# 创建一个障碍物集合
addObasacletime = 0  # 计时器
list=[]

while True:
# 事件监控
for event in pygame.event.get():
if event.type==QUIT:
exit()
# 单击空格,跳跃
if event.type == KEYDOWN and event.key == K_SPACE:
if dinosaur.rect.y >= dinosaur.lowest_y:
dinosaur.jump()
dinosaur.jump_audio.play()
if over == True:
mainGame()

if over==False:
bg1.map_update()  # 出现
bg1.map_rolling()  # 滚动
bg2.map_update()  # 出现
bg2.map_rolling()  # 滚动
dinosaur.move()  # 恐龙移动
dinosaur.draw_dinosaur()  # 显示相应动画

if addObasacletime>=1300:
r=random.randint(0,100)
if r > 40:
obstacle=Obstacle()
list.append(obstacle)
addObasacletime=0

for i in range(len(list)):
list[i].obstacle_move()  # 出现的障碍物移动
list[i].draw_obstacle()  # 显示出现的障碍物
if pygame.sprite.collide_rect(dinosaur,list[i]):
over = True
game_over()

addObasacletime+=40
pygame.display.update()
FPSCLOCK.tick(FPS)

if __name__ == '__main__':
mainGame()

执行效果:

感慨:用Python来开发一款游戏真的强,我仅仅只用了160行左右就实现了谷歌2700多行代码的基本功能。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐