怎么使用Python+Pygame实现泡泡游戏

发布时间:2022-07-12 13:59:45 作者:iii
来源:亿速云 阅读:175

这篇文章主要讲解了“怎么使用Python+Pygame实现泡泡游戏”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么使用Python+Pygame实现泡泡游戏”吧!

一、环境安装

1)素材(图片) 

怎么使用Python+Pygame实现泡泡游戏

嘻嘻很久没写游戏了,先写个简单的大家润润喉~

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)随机截图一

怎么使用Python+Pygame实现泡泡游戏

2)随机截图二

怎么使用Python+Pygame实现泡泡游戏

3)撞击结束

怎么使用Python+Pygame实现泡泡游戏

感谢各位的阅读,以上就是“怎么使用Python+Pygame实现泡泡游戏”的内容了,经过本文的学习后,相信大家对怎么使用Python+Pygame实现泡泡游戏这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. 如何使用python+pygame实现简单画板
  2. JAVA如何实现CrazyArcade泡泡堂游戏

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python pygame

上一篇:MongoDB数据库索引怎么使用

下一篇:Python实现计算AUC的方式有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》