Python游戏编程之旅(9):嗷大喵快跑小游戏开发实例,python,有写的不对之处还望各位海
Python游戏编程之旅(9):嗷大喵快跑小游戏开发实例,python,有写的不对之处还望各位海
本系列博客介绍以python+pygame库进行小游戏的开发。有写的不对之处还望各位海涵。
前几期博客我们一起学习了,pygame中的冲突检测技术以及一些常用的数据结构。
这次我们来一起做一个简单的酷跑类游戏综合运用以前学到的知识。
程序下载地址:http://yunpan.cn/cLIcJgTvq4tZS 访问密码 901f
源代码网盘地址:http://yunpan.cn/cLIc67S4nNRFY 访问密码 c139
github地址:https://github.com/XINCGer/catRunFast
效果图:
现在我们来分析一下制作流程:
游戏中一共有嗷大喵,恶龙,火焰,爆炸动画和果实(就是上方蓝色的矩形块)这几种精灵。这里我们使用到了前几期博客中的MyLibrary.py。上述这几个精灵都是 MySprite类实例化的对象。
为了方便管理。我们建立了几个精灵组,并且将一些精灵塞到了里面:
#创建精灵组 group = pygame.sprite.Group() group_exp = pygame.sprite.Group() group_fruit = pygame.sprite.Group() #创建怪物精灵 dragon = MySprite() dragon.load("dragon.png", 260, 150, 3) dragon.position = 100, 230 group.add(dragon) #创建爆炸动画 explosion = MySprite() explosion.load("explosion.png",128,128,6) #创建玩家精灵 player = MySprite() player.load("sprite.png", 100, 100, 4) player.position = 400, 270 group.add(player) #创建子弹精灵 arrow = MySprite() arrow.load("flame.png", 40, 16, 1) arrow.position = 800,320 group.add(arrow)
在程序开始的时候我们可以看到有一个欢迎界面,为了简单我这里是直接在ps里面做好了图片,然后加载到程序中的:
interface = pygame.image.load("interface.png")
界面上面还有一个按钮,当鼠标经过的时候,会变成灰底的,因此我们设计一个button类:
简单来说就是预先加载一张正常状态下在的button图片和一个按下状态的button图片,然后判断鼠标的pos是否和button的位置有重合,如果有则显示button被按下时的图片。
关于button的设计我参考了这位博友的教程:http://www.cnblogs.com/SRL-Southern/p/4949624.html,他的教程写的非常不错。
Python#定义一个按钮类 class Button(object): def __init__(self, upimage, downimage,position): self.imageUp = pygame.image.load(upimage).convert_alpha() self.imageDown = pygame.image.load(downimage).convert_alpha() self.position = position self.game_start = False def isOver(self): point_x,point_y = pygame.mouse.get_pos() x, y = self. position w, h = self.imageUp.get_size() in_x = x - w/2 < point_x < x + w/2 in_y = y - h/2 < point_y < y + h/2 return in_x and in_y def render(self): w, h = self.imageUp.get_size() x, y = self.position if self.isOver(): screen.blit(self.imageDown, (x-w/2,y-h/2)) else: screen.blit(self.imageUp, (x-w/2, y-h/2)) def is_start(self): if self.isOver(): b1,b2,b3 = pygame.mouse.get_pressed() if b1 == 1: self.game_start = True bg_sound.play_pause() btn_sound.play_sound() bg_sound.play_sound()
可以看到这个button类里面我还添加了一个isStart的方法,他是用来判断是否开始游戏的。当鼠标的位置与button重合,且按下鼠标左键的时候,游戏就开始。
(将game_start变量置为True)然后通过btn_sound.play_sound(),bg_sound.play_sound() 这两句来播放按钮被按下的声音和游戏的背景音乐。
关于pygame中声音的操作,我稍后介绍一下。
可以看到程序中还有一个不停滚动的地图,让我们来实现这个滚动地图类:
Python#定义一个滚动地图类 class MyMap(pygame.sprite.Sprite): def __init__(self,x,y): self.x = x self.y = y self.bg = pygame.image.load("background.png").convert_alpha() def map_rolling(self): if self.x < -600: self.x = 600 else: self.x -=5 def map_update(self): screen.blit(self.bg, (self.x,self.y)) def set_pos(x,y): self.x =x self.y =y
创建两个地图对象:
#创建地图对象 bg1 = MyMap(0,0) bg2 = MyMap(600,0)
在程序中直接调用update和rolling方法就可以让地图无限的滚动起来了。
bg1.map_update() bg2.map_update() bg1.map_rolling() bg2.map_rolling()
你看明白这个无限滚动地图是如何工作的了吗。首先渲染两张地图背景,一张展示在屏幕上面,一张在屏幕之外预备着(我们暂时看不到),如下图所示:
然后两张地图一起以相同的速度向左移动:
当地图1完全离开屏幕范围的时候,再次将它的坐标置为600,0(这样就又回到了状态1):
这样通过两张图片的不断颠倒位置,然后平移,在我们的视觉中就形成了一张不断滚动的地图了。
下面介绍一下如何在pygame中加载并且使用声音:
1.初始化音频模块:
我们要使用的音频系统包含在了pygame的pygame.mixer模块里面。因此在使用音频之前要初始化这个模块:
pygame.mixer.init()
这个初始化模块语句在程序中执行一次就好。
2.加载音频文件:
使用的是pygame.mixer.Sound类来加载和管理音频文件,pygame支持两种音频文件:未压缩的WAV和OGG音频文件,如果要播放长时间的音乐,我推荐你使用OGG格式音频文件,因为它的体积比较小,适合长时间的加载和播放。当你要播放比较短的音频的时候可以选择WAV。
hit_au = pygame.mixer.Sound("exlposion.wav")
3.播放音乐:
上面的pygame.mixer.Sound函数返回了一个sound对象,我们可以使用play和stop方法来播放和停止播放音乐。
但是这里我们介绍一种更为高级的用法,使用pygame.mixer.Channel,这个类提供了比sound对象更为丰富的功能。
首先我们先申请一个可用的音频频道:
channel = pygame.mixer.find_channel(True)
一旦有了频道之后我们就可以使用Channel.play()方法来播放一个sound对象了。
channel.play(sound)
好了现在让我们来实现一下和音频有关的模块:
首先定义一个初始化的函数,它初始化了音频模块,并且加载了一些音频文件以方便我们在程序中使用:
def audio_init(): global hit_au,btn_au,bg_au,bullent_au pygame.mixer.init() hit_au = pygame.mixer.Sound("exlposion.wav") btn_au = pygame.mixer.Sound("button.wav") bg_au = pygame.mixer.Sound("background.ogg") bullent_au = pygame.mixer.Sound("bullet.wav")
然后我们实现了一个Music类,这个类可以控制声音的播放和暂停(set_volume函数是用来设置音乐声音大小的):
class Music(): def __init__(self,sound): self.channel = None self.sound = sound def play_sound(self): self.channel = pygame.mixer.find_channel(True) self.channel.set_volume(0.5) self.channel.play(self.sound) def play_pause(self): self.channel.set_volume(0.0) self.channel.play(self.sound)
跳跃函数:
当按下空格键的时候,嗷大喵会跳起,这个是如何实现的呢?
for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() keys = pygame.key.get_pressed() if keys[K_ESCAPE]: pygame.quit() sys.exit() elif keys[K_SPACE]: if not player_jumping: player_jumping = True jump_vel = -12.0
当按下空格键的时候,会将player_jumping变量置为True 并且给jump_vel一个初速度-12.0
然后在每次循环的时候,将jump_vel 加0.6,当嗷大喵回到起跳位置的时候,将速度置为0,使人物不再在y方向上有移动。
#检测玩家是否处于跳跃状态 if player_jumping: if jump_vel < 0: jump_vel += 0.6 elif jump_vel >= 0: jump_vel += 0.8 player.Y += jump_vel if player.Y > player_start_y: player_jumping = False player.Y = player_start_y jump_vel = 0.0
然后我们还需要一个不断发出的子弹:
Python#更新子弹 if not game_over: arrow.X -= arrow_vel if arrow.X < -40: reset_arrow()
#重置火箭函数 def reset_arrow(): y = random.randint(270,350) arrow.position = 800,y bullent_sound.play_sound()
关于嗷大喵和子弹冲突检测我们使用了之前学过的矩形冲突检测技术,当玩家和子弹产生冲突的时候,重置子弹,播放爆炸动画,然后将人物的x坐标值向左移动10,以表示人物受到伤害。恶龙和子弹的冲突和这个是一样的,这里就不再赘述了。
#碰撞检测,子弹是否击中玩家 if pygame.sprite.collide_rect(arrow, player): reset_arrow() explosion.position =player.X,player.Y player_hit = True hit_sound.play_sound() if p_first: group_exp.add(explosion) p_first = False player.X -= 10
然后我们还需要考虑一下玩家被恶龙追上的时候的情形,还是应用矩形检测技术:
if pygame.sprite.collide_rect(player, dragon): game_over = True
为了使果实移动,我们需要遍历group_fruit里面的果实,然后依次将他们左移5个单位,然后我们还需要判断玩家吃到果实的场景,果实会消失,然后玩家的积分增加。
这里使用了之前学过的pygame.sprite.spritecollide(sprite,sprite_group,bool)。
调用这个函数的时候,一个组中的所有精灵都会逐个地对另外一个单个精灵进行冲突检测,发生冲突的精灵会作为一个列表返回。
这个函数的第一个参数就是单个精灵,第二个参数是精灵组,第三个参数是一个bool值,最后这个参数起了很大的作用。当为True的时候,会删除组中所有冲突的精灵,False的时候不会删除冲突的精灵。因此我们这里将第三个参数设置为True,这样就会删除掉和精灵冲突的对象了,看起来就好像是玩家吃掉了这些果实一样。
#遍历果实,使果实移动 for e in group_fruit: e.X -=5 collide_list = pygame.sprite.spritecollide(player,group_fruit,False) score +=len(collide_list)
最后还是看一下全部的代码:
# -*- coding: utf-8 -*- import sys, time, random, math, pygame,locale from pygame.locals import * from MyLibrary import * #重置火箭函数 def reset_arrow(): y = random.randint(270,350) arrow.position = 800,y bullent_sound.play_sound() #定义一个滚动地图类 class MyMap(pygame.sprite.Sprite): def __init__(self,x,y): self.x = x self.y = y self.bg = pygame.image.load("background.png").convert_alpha() def map_rolling(self): if self.x < -600: self.x = 600 else: self.x -=5 def map_update(self): screen.blit(self.bg, (self.x,self.y)) def set_pos(x,y): self.x =x self.y =y #定义一个按钮类 class Button(object): def __init__(self, upimage, downimage,position): self.imageUp = pygame.image.load(upimage).convert_alpha() self.imageDown = pygame.image.load(downimage).convert_alpha() self.position = position self.game_start = False def isOver(self): point_x,point_y = pygame.mouse.get_pos() x, y = self. position w, h = self.imageUp.get_size() in_x = x - w/2 < point_x < x + w/2 in_y = y - h/2 < point_y < y + h/2 return in_x and in_y def render(self): w, h = self.imageUp.get_size() x, y = self.position if self.isOver(): screen.blit(self.imageDown, (x-w/2,y-h/2)) else: screen.blit(self.imageUp, (x-w/2, y-h/2)) def is_start(self): if self.isOver(): b1,b2,b3 = pygame.mouse.get_pressed() if b1 == 1: self.game_start = True bg_sound.play_pause() btn_sound.play_sound() bg_sound.play_sound() def replay_music(): bg_sound.play_pause() bg_sound.play_sound() #定义一个数据IO的方法 def data_read(): fd_1 = open("data.txt","r") best_score = fd_1.read() fd_1.close() return best_score #定义一个控制声音的类和初始音频的方法 def audio_init(): global hit_au,btn_au,bg_au,bullent_au pygame.mixer.init() hit_au = pygame.mixer.Sound("exlposion.wav") btn_au = pygame.mixer.Sound("button.wav") bg_au = pygame.mixer.Sound("background.ogg") bullent_au = pygame.mixer.Sound("bullet.wav") class Music(): def __init__(self,sound): self.channel = None self.sound = sound def play_sound(self): self.channel = pygame.mixer.find_channel(True) self.channel.set_volume(0.5) self.channel.play(self.sound) def play_pause(self): self.channel.set_volume(0.0) self.channel.play(self.sound) #主程序部分 pygame.init() audio_init() screen = pygame.display.set_mode((800,600),0,32) pygame.display.set_caption("嗷大喵快跑!") font = pygame.font.Font(None, 22) font1 = pygame.font.Font(None, 40) framerate = pygame.time.Clock() upImageFilename = 'game_start_up.png' downImageFilename = 'game_start_down.png' #创建按钮对象 button = Button(upImageFilename,downImageFilename, (400,500)) interface = pygame.image.load("interface.png") #创建地图对象 bg1 = MyMap(0,0) bg2 = MyMap(600,0) #创建一个精灵组 group = pygame.sprite.Group() group_exp = pygame.sprite.Group() group_fruit = pygame.sprite.Group() #创建怪物精灵 dragon = MySprite() dragon.load("dragon.png", 260, 150, 3) dragon.position = 100, 230 group.add(dragon) #创建爆炸动画 explosion = MySprite() explosion.load("explosion.png",128,128,6) #创建玩家精灵 player = MySprite() player.load("sprite.png", 100, 100, 4) player.position = 400, 270 group.add(player) #创建子弹精灵 arrow = MySprite() arrow.load("flame.png", 40, 16, 1) arrow.position = 800,320 group.add(arrow) #定义一些变量 arrow_vel = 10.0 game_over = False you_win = False player_jumping = False jump_vel = 0.0 player_start_y = player.Y player_hit = False monster_hit = False p_first = True m_first = True best_score = 0 global bg_sound,hit_sound,btn_sound,bullent_sound bg_sound=Music(bg_au) hit_sound=Music(hit_au) btn_sound=Music(btn_au) bullent_sound =Music(bullent_au) game_round = {1:'ROUND ONE',2:'ROUND TWO',3:'ROUND THREE',4:'ROUND FOUR',5:'ROUND FIVE'} game_pause = True index =0 current_time = 0 start_time = 0 music_time = 0 score =0 replay_flag = True #循环 bg_sound.play_sound() best_score = data_read() while True: framerate.tick(60) ticks = pygame.time.get_ticks() for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() keys = pygame.key.get_pressed() if keys[K_ESCAPE]: pygame.quit() sys.exit() elif keys[K_SPACE]: if not player_jumping: player_jumping = True jump_vel = -12.0 screen.blit(interface,(0,0)) button.render() button.is_start() if button.game_start == True: if game_pause : index +=1 tmp_x =0 if score >int (best_score): best_score = score fd_2 = open("data.txt","w+") fd_2.write(str(best_score)) fd_2.close() #判断游戏是否通关 if index == 6: you_win = True if you_win: start_time = time.clock() current_time =time.clock()-start_time while current_time<5: screen.fill((200, 200, 200)) print_text(font1, 270, 150,"YOU WIN THE GAME!",(240,20,20)) current_time =time.clock()-start_time print_text(font1, 320, 250, "Best Score:",(120,224,22)) print_text(font1, 370, 290, str(best_score),(255,0,0)) print_text(font1, 270, 330, "This Game Score:",(120,224,22)) print_text(font1, 385, 380, str(score),(255,0,0)) pygame.display.update() pygame.quit() sys.exit() for i in range(0,100): element = MySprite() element.load("fruit.bmp", 75, 20, 1) tmp_x +=random.randint(50,120) element.X = tmp_x+300 element.Y = random.randint(80,200) group_fruit.add(element) start_time = time.clock() current_time =time.clock()-start_time while current_time<3: screen.fill((200, 200, 200)) print_text(font1, 320, 250,game_round[index],(240,20,20)) pygame.display.update() game_pause = False current_time =time.clock()-start_time else: #更新子弹 if not game_over: arrow.X -= arrow_vel if arrow.X < -40: reset_arrow() #碰撞检测,子弹是否击中玩家 if pygame.sprite.collide_rect(arrow, player): reset_arrow() explosion.position =player.X,player.Y player_hit = True hit_sound.play_sound() if p_first: group_exp.add(explosion) p_first = False player.X -= 10 #碰撞检测,子弹是否击中怪物 if pygame.sprite.collide_rect(arrow, dragon): reset_arrow() explosion.position =dragon.X+50,dragon.Y+50 monster_hit = True hit_sound.play_sound() if m_first: group_exp.add(explosion) m_first = False dragon.X -= 10 #碰撞检测,玩家是否被怪物追上 if pygame.sprite.collide_rect(player, dragon): game_over = True #遍历果实,使果实移动 for e in group_fruit: e.X -=5 collide_list = pygame.sprite.spritecollide(player,group_fruit,False) score +=len(collide_list) #是否通过关卡 if dragon.X < -100: game_pause = True reset_arrow() player.X = 400 dragon.X = 100 #检测玩家是否处于跳跃状态 if player_jumping: if jump_vel <0: jump_vel += 0.6 elif jump_vel >= 0: jump_vel += 0.8 player.Y += jump_vel if player.Y > player_start_y: player_jumping = False player.Y = player_start_y jump_vel = 0.0 #绘制背景 bg1.map_update() bg2.map_update() bg1.map_rolling() bg2.map_rolling() #更新精灵组 if not game_over: group.update(ticks, 60) group_exp.update(ticks,60) group_fruit.update(ticks,60) #循环播放背景音乐 music_time = time.clock() if music_time > 150 and replay_flag: replay_music() replay_flag =False #绘制精灵组 group.draw(screen) group_fruit.draw(screen) if player_hit or monster_hit: group_exp.draw(screen) print_text(font, 330, 560, "press SPACE to jump up!") print_text(font, 200, 20, "You have get Score:",(219,224,22)) print_text(font1, 380, 10, str(score),(255,0,0)) if game_over: start_time = time.clock() current_time =time.clock()-start_time while current_time<5: screen.fill((200, 200, 200)) print_text(font1, 300, 150,"GAME OVER!",(240,20,20)) current_time =time.clock()-start_time print_text(font1, 320, 250, "Best Score:",(120,224,22)) if score >int (best_score): best_score = score print_text(font1, 370, 290, str(best_score),(255,0,0)) print_text(font1, 270, 330, "This Game Score:",(120,224,22)) print_text(font1, 370, 380, str(score),(255,0,0)) pygame.display.update() fd_2 = open("data.txt","w+") fd_2.write(str(best_score)) fd_2.close() pygame.quit() sys.exit() pygame.display.update()
相关内容
- 用 Python 将文本转为图片,,目前类似的工具已经有了不
- python生成汉字图片字库,, pygame渲染字体来
- python调用摄像头摄像并保存图片,,[Python]代码#!
- Python加pyGame设计的简单拼图游戏,pythonpygame,import pyga
- python通过PyGame模块播放声音,pythonpygame,import pygam
- python通过PyGame播放Midi和Mp3文件,pythonpygame,''' pg_midi_
- python通过PyGame模块播放声音,pythonpygame,import pygam
- python学习笔记(七) 类和pygame实现打飞机游戏,pythonpy
- Python资源大全中文版,,环境管理管理 Pyt
- Python通过Pygame绘制移动的矩形实例代码,pythonpygame
评论关闭