当前位置:网站首页>[pyGame practice] the super interesting bubble game is coming - may you be childlike and always happy and simple~
[pyGame practice] the super interesting bubble game is coming - may you be childlike and always happy and simple~
2022-07-28 11:45:00 【Gu Muzi acridine】
Introduction
![]()
Bubble Kingdom Lots of fun
Gollum , Blowing bubbles , Colorful bubbles float all over the sky . It's as big as a colored balloon , Small like purple grapes .
When bubbles fly all over the sky , Big friend 、 Children can't help being attracted by it . And when pearly bubbles meet
Love programmer door , What kind of beautiful scenery will appear ?
Complete material for all articles + The source code is in
Speaking of 4399 Little games , No one will be strange ? When I was a child, I often secretly turned on the computer when my parents were not at home
The webpage of the game , Sitting at the computer desk is an afternoon , I really can't rely on it. I can't stop , But these games are really good
Played ! Classic games about childhood and About bubbles , I have imitated a childhood 《 Bubble dragon 》 game !
Today, I will continue to write another version of the bubble story
( Of course, I have written many classic games before : As you can See all the codes at the end of the stamp ~)

Text
One 、 Environmental installation
1) material ( picture )

Hee hee 🤭 I haven't written games for a long time , First of all, write a simple one to moisten your throat ~
2) Running environment
The environment used by Xiaobian :Python3、Pycharm Community Edition 、Pygame The module is not included one by one
Show it .
Module installation :pip install -i https://pypi.douban.com/simple/+ Module name Two 、 Code display
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(" Bubble Kingdom ")
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(" In black ",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()3、 ... and 、 Effect display
The rules of the game :Enter Start the game ,ESC Quit the game , Press up and down the left and right keys to move the bubble . Avoid random meteorites
Oh ! The longer you dodge , The higher the score ! Warriors begin your challenge !
1) Random screenshot 1

2) Random screenshot 2

3) The impact is over

summary
Hey , The article ends here , Children who love playing games can pay attention to me ~
You can not only recommend games, but also code versions , You can play and learn to kill two birds with one stone ! I have written dozens before , Code can be exempted
Fei di , Everyone who paid attention to it before knows ! If you have the source code of love, remember to ask me for it !
Complete free source code collection office : Find me ! At the end of the article, you can get it by yourself , Didi, I can also !
I recommend previous articles ——
project 1.0 Super Marie
project 1.1 Mine clearance
project 1.3 Space mecha game
project 1.4 Fruit ninja
project 2.0 Connected to the Internet 、 Man machine Gobang game
A summary of the article ——
project 1.0 Python—2021 | Summary of existing articles | Continuous updating , Just read this article directly
( More + The source code is summarized in the article !! Welcome to ~)

边栏推荐
- Database advanced learning notes -- object type
- In order to ensure the normal operation of fire-fighting equipment in large buildings, the power supply monitoring system of fire-fighting equipment plays a key role
- An example of the mandatory measures of Microsoft edge browser tracking prevention
- 多线程与高并发(三)—— 源码解析 AQS 原理
- Dialogue with Zhuang biaowei: the first lesson of open source
- 天狼星网络验证源码/官方正版/内附搭建教程
- 1331. 数组序号转换
- Advanced database technology learning notes 1 -- Oracle deployment and pl/sql overview
- 从零开始Blazor Server(2)--整合数据库
- Using C language to compile student achievement management system (C language student achievement management system deleted)
猜你喜欢

Install SSL Certificate in Litespeed web server

什么样的知识付费系统功能,更有利于平台与讲师发展?

b2子主题/博客b2child子主题/开源源码
![完整版H5社交聊天平台源码[完整数据库+完整文档教程]](/img/3f/03239c1b4d6906766348d545a4f234.png)
完整版H5社交聊天平台源码[完整数据库+完整文档教程]

中国业务型CDP白皮书 | 爱分析报告

What is the process of switching c read / write files from user mode to kernel mode?

Techniques for visualizing large time series.

Open source huizhichuang future | 2022 open atom global open source summit openatom openeuler sub forum was successfully held

Learning notes tree array
![[applet] how to notify users of wechat applet version update?](/img/04/848a3d2932e0dc73adb6683c4dca7a.png)
[applet] how to notify users of wechat applet version update?
随机推荐
Outlook suddenly becomes very slow and too laggy. How to solve it
万字详解 Google Play 上架应用标准包格式 AAB
学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难
1331. Array sequence number conversion
Rongyun IM & RTC capabilities on new sites
使用 Terraform 在 AWS 上快速部署 MQTT 集群
服务器在线测速系统源码
Let me think about Linear Algebra: a summary of basic learning of linear algebra
R language uses LM function to build regression model, uses the augmented function of bloom package to store the model results in dataframe, and uses ggplot2 to visualize the regression residual diagr
什么样的知识付费系统功能,更有利于平台与讲师发展?
擦黑板特效表白H5源码+非常浪漫/附BGM
Four advantages of verification code to ensure mailbox security
B2 sub theme / blog b2child sub theme / open source code
拥抱开源指南
WPF dependent attribute (WPF dependent attribute)
Cvpr2020 best paper: unsupervised learning of symmetric deformable 3D objects
大佬们,问下,这个不能checkpoint,因为有个jdbc的task任务状态是FINISHED,
Database advanced learning notes cursor
Flutter tutorial flutter navigator 2.0 with gorouter, use go_ Router package learn about the declarative routing mechanism in fluent (tutorial includes source code)
Guys, ask me, this can't be checkpoint, because there is a JDBC task whose task status is finished,