Python Pygame實戰(zhàn)之趣味籃球游戲的實現(xiàn)
導(dǎo)語
貪玩的我~終于回來了!
今日過后,日常更新——挺長一段時間都不在狀態(tài)的。好好調(diào)整中!
最近在給大家研究一些新游戲,大家喜歡打籃球嘛?
(木子高中還參加過籃球比賽,棒棒~雖然打的不咋滴就是了~哈哈哈)
大學時期,最喜歡跟著室友一起去看學校的各種籃球比賽的。哈哈哈,有姐妹的話就懂得~
估計學編程的女孩子還是挺少的哈,男孩子的話不懂我就不解釋啦~回家了可以問下自己的女朋友是不是也這樣子干過!嘻嘻.jpg
今天小編的話就給大家用代碼做一款簡約的《籃球小游戲》
一、環(huán)境安裝
1)各種素材(圖片、代碼)

資料挺多滴,等下代碼就只展示主要的一些代碼哈!
2)運行環(huán)境
小編使用的環(huán)境:Python3、Pycharm社區(qū)版、Pygame、numpy、 scipy 模塊部分自帶就不
展示啦。
模塊安裝:pip install -i https://pypi.douban.com/simple/+模塊名
二、代碼展示
1)游戲界面文字
設(shè)置的是雙人模式撒,可以兩個人一起玩兒的,玩家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)游戲玩家一

2)游戲玩家二

3)隨機投籃
用多大的力氣投籃就在蓄力的時候點一下鼠標左鍵在相應(yīng)的藍條點擊,剛開始肯定不適應(yīng)
要慢慢試,看那里是最適合的時候。每次投籃一次10分哦~

到此這篇關(guān)于Python Pygame實戰(zhàn)之趣味籃球游戲的實現(xiàn)的文章就介紹到這了,更多相關(guān)Python Pygame籃球游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Python的Dataframe取兩列時間值相差一年的所有行方法
今天小編就為大家分享一篇使用Python的Dataframe取兩列時間值相差一年的所有行方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
python使用redis模塊來跟redis實現(xiàn)交互
這篇文章主要介紹了python使用redis模塊來跟redis實現(xiàn)交互,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-06-06
Python中內(nèi)置函數(shù)filter函數(shù)用法詳解
filter()函數(shù)是Python內(nèi)置的另一個有用的高階函數(shù),filter()函數(shù)接收一個函數(shù)f和一個序列,函數(shù)f的作用是對每個元素進行判斷,返回True或False,下面這篇文章主要給大家介紹了關(guān)于Python中內(nèi)置函數(shù)filter函數(shù)用法的相關(guān)資料,需要的朋友可以參考下2024-05-05
NumPy中np.random.rand函數(shù)的實現(xiàn)
np.random.rand是NumPy庫中的一個函數(shù),用于生成隨機數(shù),本文主要介紹了NumPy中np.random.rand函數(shù)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-07-07

