91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么使用Python+Pygame實現泡泡游戲

發布時間:2022-07-12 13:59:45 來源:億速云 閱讀:175 作者:iii 欄目:開發技術

這篇文章主要講解了“怎么使用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實現泡泡游戲這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

青铜峡市| 饶阳县| 东城区| 贡嘎县| 芷江| 油尖旺区| 西乡县| 防城港市| 抚宁县| 淳化县| 台湾省| 资源县| 文成县| 罗甸县| 靖江市| 会理县| 读书| 濮阳县| 津南区| 洪湖市| 禄劝| 兴和县| 广昌县| 南乐县| 台山市| 元氏县| 山阴县| 娱乐| 临西县| 扎鲁特旗| 孝义市| 高尔夫| 石嘴山市| 色达县| 晋宁县| 长岛县| 萝北县| 泸西县| 肇东市| 广河县| 松溪县|