欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Pygame實戰(zhàn)之實現(xiàn)扎氣球游戲

 更新時間:2021年12月25日 09:11:23   作者:顧木子吖  
這篇文章主要為大家介紹了利用Python中的Pygame模塊實現(xiàn)的一個扎氣球游戲,文中的示例代碼講解詳細(xì),對我們了解Pygame模塊有一定的幫助,感興趣的可以學(xué)習(xí)一下

導(dǎo)語

?前幾天,有人私信小編:

說陪女朋友在小廣場上面逛街玩兒扎氣球:結(jié)果一個都沒扎破,扎心了老鐵。

女朋友都要離家出走了~讓我給想想辦法:小編只想給你一個表情。

?哈哈哈,開玩笑的~于是,為了滿足需求,小編做了一個重大決定:熬夜給他做了一款扎氣球的小

游戲,可以拿去哄哄女朋友啦~

這游戲做完之后木子已經(jīng)替大家玩兒過了,這個很棒,不信的話你自己試試?

正文

本文的扎氣球小游戲原型就是路邊的扎氣球的游戲撒,基于Pygame做的!

就準(zhǔn)備好射的箭、不同顏色的氣球、一張背景圖片、然后爆炸的特效就可。哦~對了音樂還是要準(zhǔn)備,游戲的話有音樂背景才更有趣哦~

一、準(zhǔn)備中

1)素材資料

首先是準(zhǔn)備好需要的素材、圖片、背景音樂:

2)運行環(huán)境

環(huán)境安裝 本文用到的運行環(huán)境:Python3.7、Pycharm社區(qū)版2020、Pygame游戲模塊部分自帶

模塊直 接導(dǎo)入不需要安裝。

模塊安裝:

pip install -i https://pypi.douban.com/simple/ +模塊名

二、代碼演示

這款小游戲總的有6個.py文件組成的,代碼比較都啦,這里就只放一點點哈!

主程序運行:

?from game import *
 
def main() :
    intro = True
    game = Game()
    game.loadMusic()
    game.readHighScore()
    pygame.mixer.music.play(loops=-1)
    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                intro = False
        game.screen.fill(SKY_BLUE)
        game.screen.blit(game.background, game.background_rect)
        game.draw.Button(200, 2*game.HEIGHT/3, "PLAY", BRIGHT_GREEN, GREEN, game.gameloop, 150, 100)
        game.draw.Button(game.WIDTH/2 - 75, 2*game.HEIGHT/3, "PLAY TIMED", BRIGHT_RED, RED, game.time_restricted, 150, 100)
        game.draw.Button(game.WIDTH-350, 2*game.HEIGHT/3, "QUIT", BRIGHT_GREEN, GREEN, quit, 150, 100)
        game.draw.draw_text("__ArcuS__", game.WIDTH/2, game.HEIGHT/3, 200, BLUE)
        game.draw.draw_text("HIGH SCORE:%d" % (game.highscore), game.WIDTH-400, 50, 30, BLACK)
        pygame.display.flip()
        game.clock.tick(FPS)
 
main()
定義的一些常量:桌面背景、音樂等等。
 
FPS = 60
GRAVITY = 0.15
PI = 3.142
 
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (200, 0, 0)
BRIGHT_RED = (255, 0, 0)
GREEN = (0, 200, 0)
BRIGHT_GREEN = (0, 255, 0)
SKY_BLUE = (0, 255, 255)
BLUE = (0, 0, 255)
GREEN_YELLOW=(181,255,98)
BROWN=(204,102,0)
DARK_BROWN=(204,76,0)
 
HIGHSCORE_FILE="highscore.txt"
ARROW_IMAGE = "assets/arrow_1.png"
BACKGROUND_IMAGE = "assets/background.png"
 
EXPLOSION_SOUND = "assets/boom.wav"
CLICK_SOUND = "assets/select.wav"
MUSIC_FILE = "assets/tgfcoder-FrozenJam-SeamlessLoop.ogg"
VOLUME = 0.2
 
ARROW_SIZE = (16, 150)
BALOON_SIZE = (100, 100)
HIT_RADIUS = 15
MISSES = 15
GAME_TIME = 60
定義游戲精靈類等:
 
import pygame
import math
import random
from os import path
from constants import *
 
#游戲精靈類
class Arrow(pygame.sprite.Sprite):
    def __init__(self,game):
        pygame.sprite.Sprite.__init__(self)
        self.WIDTH = game.WIDTH
        self.HEIGHT = game.HEIGHT
        self.image_orig = pygame.transform.scale(game.arrow_img, ARROW_SIZE)
        self.image_orig.set_colorkey(BLACK)
        self.image = self.image_orig
        self.rect = self.image.get_rect()
        self.rect.centerx = self.WIDTH/2
        self.rect.bottom = self.HEIGHT-100
        self.rot = 0
        self.speedx = 0
        self.speedy = 0
        self.range = 0
        self.max_height = 0
        self.release_angle = 0
        self.set_vel = False
        self.Released = False
        self.releasex = self.rect.centerx
        self.releasey = self.rect.bottom
        self.cy = self.rect.centery
        self.game = game
 
    def update(self):
        if self.Released:
            self.speedy -= GRAVITY
            self.rect.bottom -= self.speedy
            self.rect.centerx += self.speedx
            self.rot = (-math.atan2(self.speedx, self.speedy)*180/3.14) % 360
            new_image = pygame.transform.rotate(self.image_orig, self.rot)
 
            old_center = self.rect.center
            self.image = new_image
            self.rect = self.image.get_rect()
            self.rect.center = old_center
            # print "moving"
            if self.rect.bottom < 0 or self.rect.left > self.WIDTH + 10 or self.rect.right < -10:
                self.kill()
            
        else:
            mouse = pygame.mouse.get_pos()
            click = pygame.mouse.get_pressed()
            if mouse[1] > self.rect.centery and click[0] == 1:
                self.set_vel = True
                dist = math.sqrt(
                    math.pow(self.rect.centerx-mouse[0], 2)+math.pow(self.rect.bottom-mouse[1], 2))
                # print dist
 
                self.rect.centerx = mouse[0]
                self.rect.centery = mouse[1]
                # print(2*GRAVITY*(self.rect.centery-mouse[1]))
                self.speedy = math.sqrt(2*GRAVITY*(-self.cy+mouse[1]))*4
                self.speedx = self.speedy * \
                    (mouse[0]-self.releasex)/(self.cy-mouse[1])
                self.rot = (-math.atan2(self.speedx, self.speedy)
                            * 180/3.14*0.5) % 360
                new_image = pygame.transform.rotate(self.image_orig, self.rot)
                old_center = self.rect.center
                self.image = new_image
                self.rect = self.image.get_rect()
                self.rect.center = old_center
 
                # print "setting velocity"
 
            else:
                if self.set_vel:
                    self.Released = True
                    self.game.last_arrow_time = pygame.time.get_ticks()
                    self.max_height = (self.rect.bottom-mouse[1])
                    self.range = (mouse[0]-self.rect.centerx)*2
                    # print "releasing"
                # math.sqrt(math.pow(mouse[0]-self.rect.centerx,2)+math.pow(mouse[1]-self.rect.centery,2)) < 200:
                else:
                    if (mouse[0]-self.rect.centerx) != 0:
                        theta = math.atan(
                            (mouse[1]-self.rect.bottom)/(self.rect.centerx-mouse[0]))
                    else:
                        theta = PI
                    move = theta-self.rot
                    self.rot = math.degrees(theta)
                    new_image = pygame.transform.rotate(
                        self.image_orig, self.rot)
                    old_center = self.rect.center
                    self.image = new_image
                    self.rect = self.image.get_rect()
                    self.rect.center = old_center
                    # print "rotating"
                    # print self.rot
                    # print theta
 
 
class Baloon(pygame.sprite.Sprite):
    def __init__(self,game):
        pygame.sprite.Sprite.__init__(self)
        self.WIDTH = game.WIDTH
        self.HEIGHT = game.HEIGHT
        bcolor = random.choice(game.baloon_color)
        temp = "assets/balloon_{}.png".format(bcolor)
        self.image_orig = pygame.image.load(
            path.join(path.dirname(__file__), temp))
        if bcolor == "blue":
            self.image_orig.set_colorkey(BLUE)
        elif bcolor == "black":
            self.image_orig.set_colorkey(BLACK)
        elif bcolor == "green":
            self.image_orig.set_colorkey(BRIGHT_GREEN)
        elif bcolor == "red":
            self.image_orig.set_colorkey(BRIGHT_RED)
 
        self.image_orig = pygame.transform.scale(self.image_orig, BALOON_SIZE)
        self.image = self.image_orig.copy()
        self.rect = self.image.get_rect()
        self.radius = HIT_RADIUS
 
        temp = random.randrange(self.WIDTH - self.rect.width)
        while (-150 < temp-self.WIDTH/2 < 150):
            temp = random.randrange(self.WIDTH - self.rect.width)
        self.rect.x = temp
        self.rect.y = random.randrange(self.HEIGHT+100, self.HEIGHT+150)
        self.speedy = random.randrange(-4, -1)
        self.speedx = random.randrange(-3, 3)
        self.game = game
        self.last_update = pygame.time.get_ticks()
        # print "baloon"
 
    def update(self):
        self.rect.y += self.speedy
        if self.rect.top < -20 or self.rect.left < -25 or self.rect.right > self.WIDTH + 20:
            self.kill()
            self.game.misses += 1
?

三、效果展示

游戲規(guī)則的話:箭會出現(xiàn)在游戲界面底部中間位置,拉動它:即是鼠標(biāo)左鍵拉動方向可自己調(diào)整,

直接向下拉動然后放箭射中氣球即可。射中的越多積累的分?jǐn)?shù)越高哦!

1)截圖展示效果——

游戲開始界面如下:

游戲開始界面如下:

扎中氣球效果如下:

游戲結(jié)束總成績13分:

2)視頻展示效果——

?到此這篇關(guān)于Pygame實戰(zhàn)之實現(xiàn)扎氣球游戲的文章就介紹到這了,更多相關(guān)Pygame扎氣球游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python爬蟲parsel-css選擇器的具體用法

    python爬蟲parsel-css選擇器的具體用法

    本文主要介紹了python爬蟲parsel-css選擇器的具體用法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • Python實現(xiàn)多行注釋的另類方法

    Python實現(xiàn)多行注釋的另類方法

    這篇文章主要介紹了Python實現(xiàn)多行注釋的另類方法,本文使用的是Python多行文本功能來另類的實現(xiàn)Python的多行注釋,需要的朋友可以參考下
    2014-08-08
  • Win下PyInstaller 安裝和使用教程

    Win下PyInstaller 安裝和使用教程

    pyinstaller是一個非常簡單的打包python的py文件的庫,這篇文章主要介紹了PyInstaller-Win安裝和使用教程,本文通過流程實例相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2019-12-12
  • Python裝飾器詳情

    Python裝飾器詳情

    這篇文章主要介紹了Python裝飾器,裝飾器Decorator從字面上理解,就是裝飾對象的器件,其的特點是特點是函數(shù)是作為其參數(shù)出現(xiàn)的,裝飾器還擁有閉包的特點,下面來看看文中的具體內(nèi)容
    2021-11-11
  • Pytorch模型微調(diào)fine-tune詳解

    Pytorch模型微調(diào)fine-tune詳解

    微調(diào)(fine-tune)通過使用在大數(shù)據(jù)上得到的預(yù)訓(xùn)練好的模型來初始化自己的模型權(quán)重,從而提升精度,這篇文章主要介紹了Pytorch模型微調(diào)(fine-tune),需要的朋友可以參考下
    2023-01-01
  • Python pandas 重命名索引和列名稱的實現(xiàn)

    Python pandas 重命名索引和列名稱的實現(xiàn)

    本文主要介紹了Python pandas 重命名索引和列名稱的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Python必備技巧之字典(Dictionary)詳解

    Python必備技巧之字典(Dictionary)詳解

    Python中的字典由于是對象的集合屬于復(fù)合數(shù)據(jù)類型,類似于列表。本文將通過示例詳細(xì)講解Python中字典的使用方法,感興趣的可以了解一下
    2022-03-03
  • Python批量添加圖片水印的實現(xiàn)

    Python批量添加圖片水印的實現(xiàn)

    水印在很多時候都會使用的到,本文主要介紹了Python批量添加圖片水印的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 用Python調(diào)用win命令行提高工作效率的實例

    用Python調(diào)用win命令行提高工作效率的實例

    今天小編就為大家分享一篇用Python調(diào)用win命令行提高工作效率的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-08-08
  • Python相互導(dǎo)入的問題解決

    Python相互導(dǎo)入的問題解決

    大家好,本篇文章主要講的是Python相互導(dǎo)入的問題解決,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01

最新評論