Pygame实战练习之保护单身狗游戏
导语
各位戏精大家好!我是木木子,这个中秋已经结束了,你们都带着对象回家了码?
中秋那几天朋友圈简直是大型秀恩爱现场。
【Pygame实战练习之保护单身狗游戏】又是一年中秋夜,依旧凭实力单身!呼吁大家记得保护下单身狗啊喂!
文章图片
?
作为一条人畜无害可可爱爱的单身狗
每年的假期几乎都被伤的体无完肤,今年我们不仅要好好生存,还要勇敢报复,让情侣们知道:
单身狗不是这么好欺负的~单身狗们报仇吧!今天带大家写一款贼有趣的单身狗小游戏~
?
文章图片
?
正文
首先准备好图片素材等:
文章图片
bgm音乐必备:
import pygame class Sound():def __init__(self):pygame.mixer.music.load('../sound/baab.mp3')pygame.mixer.music.play()
?先上效果图嘿嘿嘿 看好哈:
游戏界面——
文章图片
程序运行——
文章图片
(1)设置滚动的背景:
#地图滚动import pygameimport random class GameBackground(object):# 初始化地图def __init__(self, scene):# 加载相同张图片资源,做交替实现地图滚动self.image1 = pygame.image.load("../image/background.png")self.image2 = pygame.image.load("../image/background.png")# 保存场景对象self.main_scene = scene# 辅助移动地图self.x1 = 0self.x2 = 1280self.snowflag=False # 计算地图图片绘制坐标def action(self):self.x1 = self.x1 - 1self.x2 = self.x2 - 1if self.x1 <= -1279:self.x1 = 1279if self.x2 <= -1279:self.x2 = 1279 # 绘制地图的两张图片def draw(self):self.main_scene.blit(self.image1, (self.x1,0))self.main_scene.blit(self.image2, (self.x2,0)) class Snow():# 雪花的竖直速度def __init__(self):self.x = 0# 雪花的横坐标self.y = 0# 雪花的纵坐标self.vx = 0# 雪花的水平速度self.vy = 0self.x = random.randint(0,1280)# 初始化雪花横坐标self.y = random.randint(0,390)#初始化雪花纵坐标def getsnowpos(self):return self.x,self.y# 返回雪花坐标位置
(2)单身狗不能碰到情侣检测碰撞:
import pygame class Collide():def __init__(self):self.gamestatus=Trueself.snowflag=Falseself.dogflag=0self.score=0self.scoreup=200self.gameover=Falseself.count=0 def dogs_cpdogs(self,dogs,cpdogs):boooooooooooooooool=pygame.sprite.groupcollide(dogs,cpdogs,False,True)if boooooooooooooooool:self.score += self.scoreupprint(self.score) def dog_cpdog(self,dog,cpdog2s):booooooool=pygame.sprite.spritecollide(dog,cpdog2s,True)if booooooool:self.snowflag = Trueself.count += 1if self.count>=3:self.gameover=True
?(3)主函数:
import pygameimport sys,osimport randomfrom background import GameBackground,Snowfrom dog import MySpritefrom cpdog import Monster,MySprite2from pygame.sprite import Groupfrom collidedetection import Collidefrom sound import Soundfrom score import Fontffrom crydog import MySpritec pygame.init()screen=pygame.display.set_mode((1280,390))fpsset=pygame.time.Clock()bgcolor=(255,240,30)background=GameBackground(screen)dog=MySprite() dog.load("../image/dog.png", 82, 62, 3)dog.X = 0dog.Y = 285dogs = pygame.sprite.Group()dogs.add(dog)cpdogs=Group()lasttimecreat=0collidetest=Collide()sound=Sound()cpdog2s = pygame.sprite.Group()snowlist = []for i in range(0, 50):# 建立50个雪花snow=Snow()snowlist.append(snow)scor=Fontf(screen)onoff = Truecrydog=None while 1:for event in pygame.event.get():if event.type == pygame.QUIT:sys.exit()# player ctrlif event.type==pygame.KEYDOWN:if event.key==pygame.K_RIGHT:dog.movieRight=Trueif event.key == pygame.K_LEFT:dog.movieLeft=Trueif event.key==pygame.K_SPACE:if not dog.jumping:dog.jumping=Truedog.vUP = -14if event.type == pygame.KEYUP:if event.key == pygame.K_RIGHT:dog.movieRight=Falseif event.key==pygame.K_LEFT:dog.movieLeft=Falseif event.type==pygame.MOUSEBUTTONDOWN:mousex,mousey=pygame.mouse.get_pos()if mousex>=540 and mousex<=540 + scor.text_width:if mousey>=180 and mousey<=180 + scor.text_height:onoff = Falseif dog.jumping:if dog.vUP < 0:dog.vUP += 0.6elif dog.vUP >= 0:dog.vUP += 0.8dog.Y += dog.vUPif dog.Y >= 290:dog.jumping = Falsedog.Y = 290dog.vUP = 0.0if not collidetest.gameover: if onoff:scor.beginpage()print(onoff) else:print(onoff)#碰撞检测collidetest.dogs_cpdogs(dogs,cpdogs)collidetest.dog_cpdog(dog,cpdog2s)#背景background.action()background.draw()#分数# scor.beginpage()collidetest.score+=1scor.displayScore(collidetest.score)#时间控制ticks = pygame.time.get_ticks()#player绘制dogs.update(ticks)dogs.draw(screen) cpdog2s.update(ticks)cpdog2s.draw(screen)if ticks >lasttimecreat+ random.randint(1000,20000):#生成障碍物cp = Monster(screen)cpdogs.add(cp)cpdog2 = MySprite2()cpdog2.load("../image/cpdogs.png", 113, 62, 3)cpdog2.X = 1280cpdog2.Y = 285cpdog2s.add(cpdog2)lasttimecreat=ticks for m in cpdogs:m.draw_monster()if m.rect.x <=640:del mcpdogs.update()#snowif collidetest.snowflag:for snow in snowlist:# 每个雪花位置的变换# if random.randint(0,1):snow.vx = random.randint(-3,3)# 雪花的横向速度snow.vy = 1# 雪花的竖直速度snow.x += snow.vx# 雪花的横轴移动位置snow.y += snow.vy# 雪花的纵轴移动位置if snow.y > 500:snow.y = 0pygame.draw.circle(screen,[255,255,255],snow.getsnowpos(),1)else:if collidetest.snowflag:background.action()background.draw()for snow in snowlist:snow.vx = random.randint(-3, 3)snow.vy = 1snow.x += snow.vxsnow.y += snow.vyif snow.y > 500:snow.y = 0pygame.draw.circle(screen, [255, 255, 255], snow.getsnowpos(), 1)if not crydog:crydog=MySpritec()crydog.load("../image/crydog.png", 82, 62,3)crydog.X = 100crydog.Y = 285crydogs=Group()crydogs.add(crydog)ticks = pygame.time.get_ticks()# player绘制crydogs.update(ticks)crydogs.draw(screen)scor.gameoverrrrrrrrr()if crydog.X>1280:sys.exit()fpsset.tick(60)pygame.display.update()
效果图:游戏规则躲避情侣狗,一直奔跑加分!
文章图片
?总结 好啦!单身狗大战即将开始,come on。
哈哈哈哈 这游戏蛮有趣的撒!你们的支持是我最大的动力,记得三连哦~木啊!
文章图片
?
到此这篇关于Pygame实战练习之保护单身狗游戏的文章就介绍到这了,更多相关Pygame 保护单身狗游戏内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- 【生信技能树】R语言练习题|【生信技能树】R语言练习题 - 中级
- 绘本讲师训练营【24期】14/21阅读原创《小黑鱼》
- 绘本讲师训练营【18期】14/21《我的情绪小怪兽》故事会新体验
- 合理情绪疗法之试用|李克富思维训练营56/90
- 绘本讲师训练营7期9/21阅读原创《蜗牛屋|绘本讲师训练营7期9/21阅读原创《蜗牛屋 》
- 拆书方法训练营
- 阿菘的ScalersTalk第五轮新概念朗读持续力训练Day15|阿菘的ScalersTalk第五轮新概念朗读持续力训练Day15 20191025
- Python基础|Python基础 - 练习1
- 特种兵训练第四天
- 2018-09-03(李克富视角点评训练营81/90)|2018-09-03(李克富视角点评训练营81/90) 那只蛙从“井”爬出来又进入了“隧道”