Python+Pygame实战之泡泡游戏的实现
作者:顾木子吖 发布时间:2023-05-19 15:40:58
标签:Python,Pygame,游戏
导语
泡泡王国 欢乐多多
咕噜噜,吹泡泡,七彩泡泡满天飘。大的好像彩气球,小的就像紫葡萄。
当泡泡漫天飞舞时,大朋友、小朋友都会情不自禁地被它吸引。而当珍珠般的泡泡遇上可爱的程序员门时,又会出现什么样的美丽风景呢?
说到4399小游戏,没有人会陌生吧?我小时候经常趁着家长不在家的时候偷偷打开电脑打开小游戏的网页,在电脑桌前一坐就是一下午,真的不能赖我不适可而止,而是这些游戏真的太好
玩了!关于童年的经典游戏and有关泡泡的,之前已经仿写了一款童年《泡泡龙》游戏!
Pygame实战:风靡全球的经典泡泡龙小游戏来袭
今天续写另一个版本的泡泡故事叭
一、环境安装
1)素材(图片)
嘻嘻很久没写游戏了,先写个简单的大家润润喉~
2)运行环境
小编使用的环境:Python3、Pycharm社区版、Pygame 模块部分自带就不一一展示啦。
模块安装:pip install -i https://pypi.douban.com/simple/+模块名
二、代码展示
import pygame,random,os
from pygame.locals import *
from pygame.sprite import Sprite,Group,spritecollide
def load_image(name):
fullname=os.path.join(os.path.join(os.path.split(os.path.abspath(__file__))[0],"filedata"),name)
image=pygame.image.load(fullname)
return image
class Surface:
def __init__(self,screen):
self.screen=screen
self.screenrect=self.screen.get_rect()
self.surfacetype=1
self.image=load_image("surface"+str(self.surfacetype)+".png")
self.rect=self.image.get_rect()
self.rect.center=self.screenrect.center
self.speed=3.5
self.moveUp=False
self.moveDown=False
self.moveLeft=False
self.moveRight=False
def update(self):
if self.moveUp and self.rect.top>0:
self.rect.centery-=self.speed
if self.moveDown and self.rect.bottom<HEIGHT:
self.rect.centery+=self.speed
if self.moveLeft and self.rect.left>0:
self.rect.centerx-=self.speed
if self.moveRight and self.rect.right<WIDTH:
self.rect.centerx+=self.speed
def blit(self):
self.screen.blit(self.image,self.rect)
def addtype(self):
self.surfacetype+=1
if self.surfacetype>3:
self.surfacetype=1
self.image=load_image("surface"+str(self.surfacetype)+".png")
class Trap(Sprite):
def __init__(self,screen,traptyper):
super(Trap,self).__init__()
self.screen=screen
self.screenrect=self.screen.get_rect()
self.traptype=traptyper
self.image=load_image("trap"+str(self.traptype)+".png")
self.rect=self.image.get_rect()
self.rectat=random.choice(["top","left","right","bottom"])
self.__updatespeed()
def __updatespeed(self):
self.xspeed=round(random.uniform(1,3),2)
self.yspeed=round(random.uniform(1,3),2)
if self.rectat=="top":
self.rect.center=(random.randint(0,WIDTH),0)
elif self.rectat=="bottom":
self.rect.center=(random.randint(0,WIDTH),HEIGHT)
self.yspeed=-self.yspeed
elif self.rectat=="left":
self.xspeed=-self.xspeed
self.rect.center=(0,random.randint(0,HEIGHT))
elif self.rectat=="right":
self.rect.center=(WIDTH,random.randint(0,HEIGHT))
def update(self):
global traptype
self.traptype=traptype
self.image=load_image("trap"+str(self.traptype)+".png")
self.rect.centerx+=self.xspeed
self.rect.centery+=self.yspeed
if self.rect.top>self.screenrect.height or self.rect.bottom<0:
self.kill()
elif self.rect.left>self.screenrect.width or self.rect.right<0:
self.kill()
WIDTH=1200
HEIGHT=650
traptype=1
def initmain():
global traptype
traptype=1
pygame.init()
screen=pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("泡泡王国")
bgcolorlist=((0,255,255),(255,128,0),(128,0,255))
colortype=0
bgcolor=bgcolorlist[colortype]
rates=0
gamestart=True
fpstime=pygame.time.Clock()
fps=100
gameFont=pygame.font.SysFont("黑体",26,True)
surface=Surface(screen)
traps=Group()
while gamestart:
fpstime.tick(fps)
screen.fill(bgcolor)
surface.blit()
rates+=1
if rates%20==0:
newtrap=Trap(screen,traptype)
traps.add(newtrap)
if rates%1000==0 and rates!=0:
colortype+=1
traptype+=1
if colortype>2:
colortype=0
if traptype>3:
traptype=1
bgcolor=bgcolorlist[colortype]
surface.addtype()
if spritecollide(surface,traps,True):
screen.fill(bgcolor)
gamestart=False
break
screen.blit(gameFont.render("Score: "+str(rates),True,(0,0,0)),(2,2))
surface.update()
traps.update()
traps.draw(screen)
for event in pygame.event.get():
if event.type==QUIT:
gamestart=False
elif event.type==KEYDOWN:
if event.key==K_RIGHT:
surface.moveRight=True
elif event.key==K_LEFT:
surface.moveLeft=True
elif event.key==K_UP:
surface.moveUp=True
elif event.key==K_DOWN:
surface.moveDown=True
elif event.key==K_SPACE:
surface.speed=4.5
elif event.type==KEYUP:
if event.key==K_RIGHT:
surface.moveRight=False
elif event.key==K_LEFT:
surface.moveLeft=False
elif event.key==K_UP:
surface.moveUp=False
elif event.key==K_DOWN:
surface.moveDown=False
elif event.key==K_SPACE:
surface.speed=3.5
pygame.display.flip()
screen.fill(bgcolor)
screen.blit(gameFont.render("Score: "+str(rates//30),True,(0,0,0)),(2,2))
screen.blit(gameFont.render("Press enter to retry ; Press ESC to exit",True,(0,0,0)),(6,32))
screen.blit(gameFont.render("Rules:",True,(0,0,0)),(6,82))
screen.blit(gameFont.render("Press the arrow keys to control the bubbles to avoid touching the stinging ball and press the space bar to speed up.",
True,(0,0,0)),(66,112))
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
__import__("sys").exit()
elif event.type==KEYDOWN:
if event.key==K_RETURN:
pygame.quit()
initmain()
elif event.key==K_ESCAPE:
pygame.quit()
__import__("sys").exit()
if __name__=="__main__":
initmain()
三、效果展示
游戏规则:Enter开始游戏,ESC退出游戏,上下左右键按住泡泡移动。躲避随机出现的陨石
哦!躲避的时间越长,分数越高啦!勇士们开始你门的挑战叭!
1)随机截图一
2)随机截图二
3)撞击结束
来源:https://blog.csdn.net/weixin_55822277/article/details/125670634
0
投稿
猜你喜欢
- 在看子串匹配问题的时候,书上的关于KMP的算法的介绍总是理解不了。看了一遍代码总是很快的忘掉,后来决定好好分解一下KMP算法,算是给自己加深
- 本文实例讲述了Python列表解析操作。分享给大家供大家参考,具体如下:列表解析Python 的强大特性之一是其对 list 的解析,它提供
- 获取CPU信息我们先来获取CPU的信息:>>> import psutil>>> psutil.cpu_
- fileno()方法返回所使用的底层实现,要求从操作系统I/O操作的整数文件描述符。语法以下是fileno()方法的语法:fil
- 目前用于Python的格式化程序(如autopep8和pep8ify)都用于删除代码中的lint错误。这有很明显的局限性。YAPF采用了不同
- 示例可以附件中下载 1.加载xml文件 加载XML文件共有2种方法,一是加载指定字符串,二是加载指定文件 2.获取element的方法 a)
- 计算两个信号的交叉谱密度结果展示:完整代码:import numpy as npimport matplotlib.pyplot
- 使用python进行程序编写时,经常会使用第三方模块包。这种包我们可以通过python setup install 进行安装后,通过impo
- 事情是这样的,我写了一个tornado的服务,过程当中我用logging记录一些内容,由于一开始并没有仔细观察tornado自已的日志管理,
- 这几天写代码中遇到的一个常见问题,在Python中如何批量的生成一些变量,如生成变量X1, X2, X3,并在后续的方法中调用,完成赋值、取
- 可能接触网站与编程设计的人都知道,一个属性值需要引号包括起来,但是有的时候就是因为没有正确设定引号类型,导致程序出错,就连我自己也是这样,我
- 导读:如何使用scrapy框架实现爬虫的4步曲?什么是CrawSpider模板?如何设置下载中间件?如何实现Scrapyd远程部署和监控?想
- 1.前期准备用户models.pyclass User(models.Model): username = mod
- 顺序表python版的实现(部分功能未实现)结果展示:代码示例:#!/usr/bin/env python# -*- coding:utf-
- Windows下采用PyInstall将py文件转换成exe可执行文件好不容易写完的py文件,想做成exe文件,最开始选择用py2exe,结
- 如下所示:函数功能abs(x)返回一个数的绝对值。 参数可以是一个整数或浮点数。 如果参数是一个复数,则返回它的模。all(iterable
- 在上一篇Python接口自动化测试系列文章:Python接口自动化浅析登录接口测试实战,主要介绍接口概念、接口用例设计及登录接口测试实战。以
- 刚开始学习python,python相对于java确实要简洁易用得多。内存回收类似hotspot的可达性分析, 不可变对象也如同java得I
- scipy.misc.logsumexp函数的输入参数有(a, axis=None, b=None, keepdims=False, ret