Python Pygame篮球游戏

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Python Pygame篮球游戏

顾木子吖   2022-06-03 我要评论

导语

贪玩的我~终于回来了!

今日过后,日常更新——挺长一段时间都不在状态的。好好调整中!

最近在给大家研究一些新游戏,大家喜欢打篮球嘛?

(木子高中还参加过篮球比赛,棒棒~虽然打的不咋滴就是了~哈哈哈)

大学时期,最喜欢跟着室友一起去看学校的各种篮球比赛的。哈哈哈,有姐妹的话就懂得~

估计学编程的女孩子还是挺少的哈,男孩子的话不懂我就不解释啦~回家了可以问下自己的女朋友是不是也这样子干过!嘻嘻.jpg

今天小编的话就给大家用代码做一款简约的《篮球小游戏》

一、环境安装

1)各种素材(图片、代码)

资料挺多滴,等下代码就只展示主要的一些代码哈!

2)运行环境

小编使用的环境:Python3、Pycharm社区版、Pygame、numpy、 scipy 模块部分自带就不

展示啦。

模块安装:pip install -i http://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)游戏玩家一

2)游戏玩家二

3)随机投篮

用多大的力气投篮就在蓄力的时候点一下鼠标左键在相应的蓝条点击,刚开始肯定不适应

要慢慢试,看那里是最适合的时候。每次投篮一次10分哦~

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们