天天看點

【Pygame實戰】趣味籃球——迎“籃”而上 ,樂在“球”中,喜歡打籃球的小可愛前來報道~

導語

貪玩的我~終于回來了!

今日過後,日常更新——挺長一段時間都不在狀态的。好好調整中!

最近在給大家研究一些新遊戲,大家喜歡打籃球嘛?

(木子高中還參加過籃球????比賽,棒棒~雖然打的不咋滴就是了~哈哈哈)

所有文章完整的素材+源碼都在

粉絲福利,請移步至社群

大學時期,最喜歡跟着室友一起去看學校的各種籃球????比賽的。哈哈哈,有姐妹的話就懂得~

【Pygame實戰】趣味籃球——迎“籃”而上 ,樂在“球”中,喜歡打籃球的小可愛前來報道~

估計學程式設計的女孩子還是挺少的哈,男孩子的話不懂我就不解釋啦~回家了可以問下自己的女朋

友是不是也這樣子幹過!嘻嘻.jpg

今天小編的話就給大家用代碼做一款簡約的《籃球????小遊戲》,愛我的代碼評論區扣666吖~

正文

一、環境安裝

1)各種素材(圖檔、代碼)

【Pygame實戰】趣味籃球——迎“籃”而上 ,樂在“球”中,喜歡打籃球的小可愛前來報道~

資料挺多滴,等下代碼就隻展示主要的一些代碼哈!

2)運作環境

小編使用的環境:Python3、Pycharm社群版、Pygame、numpy、 scipy 子產品部分自帶就不

展示啦。

子產品安裝:pip install -i https://pypi.douban.com/simple/+子產品名      

二、代碼展示

1)遊戲界面文字

設定的是雙人模式撒,可以兩個人一起玩兒的,玩家1跟玩家2輪流投籃滴。

import pygame

BLACK = (0, 0, 0)
RED = (255, 0, 0)


class Text:
    def text_objects(self, text, font, color):
        textSurface = font.render(text, True, color)
        return textSurface, textSurface.get_rect()

    def score_display(self, world, screen):
        p1color = RED if world.p1turn else BLACK
        p2color = BLACK if world.p1turn else RED
        self.add_to_screen(
            screen, 30, "Player 1: " + str(world.p1score) + " points", 150, 50, p1color
        )
        self.add_to_screen(
            screen, 30, "Player 2: " + str(world.p2score) + " points", 150, 90, p2color
        )

    def victory_message(self, world, screen):
        winner = 1 if world.p1score > world.p2score else 2
        self.add_to_screen(
            screen, 100, "The winner is Player " + str(winner) + "!", 640, 320
        )

    def add_to_screen(self, screen, font_size, text, center_x, center_y, color):
        largeText = pygame.font.Font("freesansbold.ttf", font_size)
        TextSurf, TextRect = self.text_objects(text, largeText, color)
        TextRect.center = (center_x, center_y)
        screen.blit(TextSurf, TextRect)      

2)主程式

import pygame
from Ball import Ball2D
from World import World
from PowerBar import PowerBar
from Text import Text

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)


def main():

    # initializing pygame
    pygame.init()

    clock = pygame.time.Clock()

    # top left corner is (0,0)
    win_width = 1280
    win_height = 640
    screen = pygame.display.set_mode((win_width, win_height))
    pygame.display.set_caption("Basketball籃球遊戲")

    world = World()
    power = PowerBar()
    scoreboard = Text()

    world.add_rim("disk-red.png", 5).set_pos([1000, 300])
    world.add_rim("disk-red.png", 5).set_pos([1075, 300])

    dt = 0.1

    while True:
        # 100 fps
        clock.tick(60)

        # Clear the background, and draw the sprites
        screen.fill(WHITE)
        power.draw(screen)
        world.draw(screen)
        pygame.draw.arc(screen, RED, (50, 50, 50, 50), 1, 1, 10)
        # draw rim line
        pygame.draw.line(screen, RED, [1000, 340], [1075, 340], 10)
        # draw backboard
        pygame.draw.line(screen, RED, [1075, 250], [1075, 640], 10)
        scoreboard.score_display(world, screen)
        if world.won:
            scoreboard.victory_message(world, screen)
            pygame.display.update()
            clock.tick(1)
            # countdown timer to close the game when won
            for i in range(100):
                pass
            break
        elif not world.shot:
            power.start(world)
        else:
            won = world.update(dt, power)

        pygame.display.update()


if __name__ == "__main__":
    main()      

三、效果展示

1)遊戲玩家一

【Pygame實戰】趣味籃球——迎“籃”而上 ,樂在“球”中,喜歡打籃球的小可愛前來報道~

2)遊戲玩家二

【Pygame實戰】趣味籃球——迎“籃”而上 ,樂在“球”中,喜歡打籃球的小可愛前來報道~

3)随機投籃

用多大的力氣投籃????就在蓄力的時候點一下滑鼠左鍵在相應的藍條點選,剛開始肯定不适應,

要慢慢試,看那裡是最适合的時候。每次投籃一次10分哦~

【Pygame實戰】趣味籃球——迎“籃”而上 ,樂在“球”中,喜歡打籃球的小可愛前來報道~

總結

好啦~這款《籃球????小遊戲》到這裡就結束啦!小編寫的簡易版本的撒,要繼續優化啦~

老規矩,資料素材源碼啥的????順便加一張我看過的籃球動漫裡面的人物!好看吧~

完整的免費源碼領取處:找我吖!文末可得自行領取,滴滴我也可!