最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python+Pygame實(shí)戰(zhàn)之泡泡游戲的實(shí)現(xiàn)

 更新時(shí)間:2022年07月12日 09:45:49   作者:顧木子吖  
這篇文章主要為大家介紹了如何利用Python中的Pygame模塊實(shí)現(xiàn)泡泡游戲,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Python游戲開發(fā)有一定幫助,需要的可以參考一下

導(dǎo)語

泡泡王國 歡樂多多

咕嚕嚕,吹泡泡,七彩泡泡滿天飄。大的好像彩氣球,小的就像紫葡萄。

?當(dāng)泡泡漫天飛舞時(shí),大朋友、小朋友都會(huì)情不自禁地被它吸引。而當(dāng)珍珠般的泡泡遇上可愛的程序員門時(shí),又會(huì)出現(xiàn)什么樣的美麗風(fēng)景呢?

說到4399小游戲,沒有人會(huì)陌生吧?我小時(shí)候經(jīng)常趁著家長不在家的時(shí)候偷偷打開電腦打開小游戲的網(wǎng)頁,在電腦桌前一坐就是一下午,真的不能賴我不適可而止,而是這些游戲真的太好

玩了!關(guān)于童年的經(jīng)典游戲and有關(guān)泡泡的,之前已經(jīng)仿寫了一款童年《泡泡龍》游戲!

Pygame實(shí)戰(zhàn):風(fēng)靡全球的經(jīng)典泡泡龍小游戲來襲

今天續(xù)寫另一個(gè)版本的泡泡故事叭

一、環(huán)境安裝

1)素材(圖片) 

嘻嘻很久沒寫游戲了,先寫個(gè)簡單的大家潤潤喉~

2)運(yùn)行環(huán)境

小編使用的環(huán)境:Python3、Pycharm社區(qū)版、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()

三、效果展示

游戲規(guī)則:Enter開始游戲,ESC退出游戲,上下左右鍵按住泡泡移動(dòng)。躲避隨機(jī)出現(xiàn)的隕石

哦!躲避的時(shí)間越長,分?jǐn)?shù)越高啦!勇士們開始你門的挑戰(zhàn)叭!

1)隨機(jī)截圖一

2)隨機(jī)截圖二

3)撞擊結(jié)束

以上就是Python+Pygame實(shí)戰(zhàn)之泡泡游戲的實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于Python Pygame泡泡游戲的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python中的可視化設(shè)計(jì)與UI界面實(shí)現(xiàn)

    Python中的可視化設(shè)計(jì)與UI界面實(shí)現(xiàn)

    本文介紹了如何使用Python創(chuàng)建用戶界面(UI),包括使用Tkinter、PyQt、Kivy等庫進(jìn)行基本窗口、動(dòng)態(tài)圖表和動(dòng)畫效果的實(shí)現(xiàn),通過示例代碼,展示了如何利用這些庫來構(gòu)建功能強(qiáng)大且美觀的界面
    2025-01-01
  • Python OpenCV高斯金字塔與拉普拉斯金字塔的實(shí)現(xiàn)

    Python OpenCV高斯金字塔與拉普拉斯金字塔的實(shí)現(xiàn)

    這篇文章主要介紹了Python OpenCV高斯金字塔與拉普拉斯金字塔的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Python?matplotlib之折線圖的各種樣式與畫法總結(jié)

    Python?matplotlib之折線圖的各種樣式與畫法總結(jié)

    matplotlib是Python中的一個(gè)第三方庫,主要用于開發(fā)2D圖表,以漸進(jìn)式、交互式的方式實(shí)現(xiàn)數(shù)據(jù)可視化,可以更直觀的呈現(xiàn)數(shù)據(jù),使數(shù)據(jù)更具說服力,下面這篇文章主要給大家介紹了關(guān)于Python?matplotlib之折線圖的各種樣式與畫法的相關(guān)資料,需要的朋友可以參考下
    2022-12-12
  • Python中AND、OR的一個(gè)使用小技巧

    Python中AND、OR的一個(gè)使用小技巧

    這篇文章主要介紹了Python中AND、OR的一個(gè)使用小技巧,需要的朋友可以參考下
    2015-02-02
  • python中的內(nèi)置函數(shù)getattr()介紹及示例

    python中的內(nèi)置函數(shù)getattr()介紹及示例

    其實(shí)getattr()這個(gè)方法最主要的作用是實(shí)現(xiàn)反射機(jī)制。也就是說可以通過字符串獲取方法實(shí)例。這樣,你就可以把一個(gè)類可能要調(diào)用的方法放在配置文件里,在需要的時(shí)候動(dòng)態(tài)加載。
    2014-07-07
  • python基于右遞歸解決八皇后問題的方法

    python基于右遞歸解決八皇后問題的方法

    這篇文章主要介紹了python基于右遞歸解決八皇后問題的方法,實(shí)例分析了右遞歸算法的相關(guān)使用技巧,需要的朋友可以參考下
    2015-05-05
  • python編程matplotlib交互繪制Julia集示例解析

    python編程matplotlib交互繪制Julia集示例解析

    matplotlib的Show面板中提供了放大、移動(dòng)等交互式操作,但也未能涵蓋所有的交互需求,比如希望通過mandelbrot集上的一點(diǎn)來生成對應(yīng)的Julia集
    2021-10-10
  • 解決python3輸入的坑——input()

    解決python3輸入的坑——input()

    這篇文章主要介紹了解決python3輸入的坑——input(),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • Python3 獲取一大段文本之間兩個(gè)關(guān)鍵字之間的內(nèi)容方法

    Python3 獲取一大段文本之間兩個(gè)關(guān)鍵字之間的內(nèi)容方法

    今天小編就為大家分享一篇Python3 獲取一大段文本之間兩個(gè)關(guān)鍵字之間的內(nèi)容方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • python中pygame安裝過程(超級詳細(xì))

    python中pygame安裝過程(超級詳細(xì))

    這篇文章主要介紹了python中pygame安裝過程(超級詳細(xì)),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-08-08

最新評論

革吉县| 扶沟县| 东乡县| 余庆县| 静乐县| 兴城市| 湖州市| 汝城县| 白朗县| 务川| 乐亭县| 红桥区| 唐海县| 临朐县| 句容市| 全南县| 龙江县| 家居| 旬邑县| 仙居县| 确山县| 大连市| 呼伦贝尔市| 孟连| 巴彦淖尔市| 万安县| 义马市| 新郑市| 天全县| 上林县| 乌什县| 浪卡子县| 静海县| 祁连县| 双鸭山市| 班玛县| 广平县| 体育| 东山县| 六枝特区| 屏南县|