首页 > 社交 > 科普中国

一个Python摸鱼神器,好“吃”又好玩

常驻编辑 科普中国 2022-10-11 神器   摸鱼   小鸟   背景音乐   脚本   路径   逻辑   愤怒   好玩   编号   游戏
FOh拜客生活常识网

修改后:FOh拜客生活常识网

import sys
from cpgames import cpgames

game_client = cpgames.CPGames()
all_supports = game_client.getallsupported()
games = dict(enumerate(list(all_supports.values()),start=1))

print("-"*64)
for i, element in games.items():
    print(f"-- 游戏编号: {i} -- 游戏名称: {element} -- ")
print("-"*64)

try:
    game_code = int(input("请输出游戏编号,回车后开始游戏:"))
    if game_code > len(games):
        print("请重新运行脚本并输入正确的游戏编号。。")
        sys.exit()
except Exception as e:
    print("请重新运行脚本并输入正确的游戏编号。。")
    sys.exit()

game_client.execute(games[game_code])

运行脚本之后,会让你选择想玩的游戏,输入对应的游戏编号即可,FOh拜客生活常识网

pygame 2.1.2 (SDL 2.0.18, Python 3.9.9)
Hello from the pygame community. https://www.pygame.org/contribute.html
----------------------------------------------------------------
-- 游戏编号:1 -- 游戏名称: ski --
-- 游戏编号:2 -- 游戏名称: maze --
......
-- 游戏编号:28 -- 游戏名称: twozerofoureight --
-- 游戏编号:29 -- 游戏名称: voicecontrolpikachu --
----------------------------------------------------------------
请输出游戏编号,回车后开始游戏:

FOh拜客生活常识网

入门Python游戏开发FOh拜客生活常识网

FOh拜客生活常识网

如果你已经看到这,说明你对Python制作的游戏有一定的兴趣的。FOh拜客生活常识网

这个库对于想入门Python游戏开发或者其他语言的游戏开发来说,简直就是一个宝藏。FOh拜客生活常识网

29款游戏基本覆盖了很多主流游戏的基本逻辑。FOh拜客生活常识网

就像上文中我提到,那款愤怒的小鸟,“物理引擎”非常的棒,我也很好奇他是如何实现的。FOh拜客生活常识网

我就可以去查看他的源码是如何实现这个游戏的。FOh拜客生活常识网

令我没想到的是,这个愤怒的小鸟的代码量只有100行,抛开配置信息,游戏主逻辑只有50多行。FOh拜客生活常识网

'''
Function:
    愤怒的小鸟
'''
import os
import pygame
from ...utils import QuitGame
from ..base import PygameBaseGame
from .modules import GameLevels, Pig, Bird, Block, Slingshot, Slab, Button, Label


'''配置类'''
class Config():
    # 根目录
    rootdir = os.path.split(os.path.abspath(__file__))[0]
    # FPS
    FPS = 60
    # 屏幕大小
    SCREENSIZE = (1800, 700)
    # 标题
    TITLE = '愤怒的小鸟 —— Charles的皮卡丘'
    # 一些颜色定义
    BACKGROUND_COLOR = (51, 51, 51)
    # 背景音乐路径
    BGM_PATH = os.path.join(rootdir, 'resources/audios/bgm.ogg')
    # 游戏图片路径
    IMAGE_PATHS_DICT = {
        'pig': [
            os.path.join(rootdir, 'resources/images/pig_1.png'),
            os.path.join(rootdir, 'resources/images/pig_2.png'),
            os.path.join(rootdir, 'resources/images/pig_damaged.png'),
        ],
        'bird': [
            os.path.join(rootdir, 'resources/images/bird.png'),
        ],
        'wall': [
            os.path.join(rootdir, 'resources/images/wall_horizontal.png'),
            os.path.join(rootdir, 'resources/images/wall_vertical.png'),
        ],
        'block': [
            os.path.join(rootdir, 'resources/images/block.png'),
            os.path.join(rootdir, 'resources/images/block_destroyed.png'),
        ]
    }
    # 字体路径
    FONT_PATHS_DICT_NOINIT = {
        'Comic_Kings': os.path.join(rootdir, 'resources/fonts/Comic_Kings.ttf'),
        'arfmoochikncheez': os.path.join(rootdir, 'resources/fonts/arfmoochikncheez.ttf'),
    }


'''愤怒的小鸟'''
class AngryBirdsGame(PygameBaseGame):
    game_type = 'angrybirds'
    def __init__(self, **kwargs):
        self.cfg = Config
        super(AngryBirdsGame, self).__init__(config=self.cfg, **kwargs)
    '''运行游戏'''
    def run(self):
        # 初始化
        screen, resource_loader, cfg = self.screen, self.resource_loader, self.cfg
        # 播放背景音乐
        resource_loader.playbgm()
        # 开始游戏
        def startgame():
            game_levels = GameLevels(cfg, resource_loader, screen)
            game_levels.start()
        # 开始界面
        components = pygame.sprite.Group()
        title_label = Label(screen, 700, 100, 400, 200)
        title_label.addtext('ANGRY BIRDS', 80, cfg.FONT_PATHS_DICT_NOINIT['arfmoochikncheez'], (236, 240, 241))
        components.add(title_label)
        start_btn = Button(screen, 500, 400, 300, 100, startgame, (244, 208, 63), (247, 220, 111))
        start_btn.addtext('START GAME', 60, cfg.FONT_PATHS_DICT_NOINIT['arfmoochikncheez'], cfg.BACKGROUND_COLOR)
        components.add(start_btn)
        quit_btn = Button(screen, 1000, 400, 300, 100, QuitGame, (241, 148, 138), (245, 183, 177))
        quit_btn.addtext('QUIT', 60, cfg.FONT_PATHS_DICT_NOINIT['arfmoochikncheez'], cfg.BACKGROUND_COLOR)
        components.add(quit_btn)
        charles_label = Label(screen, cfg.SCREENSIZE[0] - 300, cfg.SCREENSIZE[1] - 80, 300, 100)
        charles_label.addtext('CHARLES', 60, cfg.FONT_PATHS_DICT_NOINIT['arfmoochikncheez'], (41, 41, 41))
        components.add(charles_label)
        clock = pygame.time.Clock()
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    QuitGame()
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        QuitGame()
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    if start_btn.selected():
                        start_btn.action()
                    elif quit_btn.selected():
                        quit_btn.action()
            screen.fill(cfg.BACKGROUND_COLOR)
            for component in components: component.draw()
            pygame.display.update()
            clock.tick(cfg.FPS)
    

相关阅读:

  • 吃鸡怎么样(吃鸡神器)
  • 免费读小说软件哪个好用(最全又免费读书神器无广告)
  • 看书软件哪个好(看书神器官方下载)
  • 微信怎么抢红包 微信自动1秒抢红包神器?
  • 不要钱的定位软件有哪些,手机定位不要钱的软件下载
  • 黑客盗取微信密码,黑客盗微信密码软件下载
  • 学法减分答题神器一扫就出答案 答题神器改变学法减分
  • 防晒“神器”竟是珊瑚“杀手”
  • 技术人必备的接口测试神器:apifox、apipost、yapi,实用
  • 自制扫码“神器”“斜挎包”……松江这些居民区核酸检
    • 网站地图 |
    • 声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。文章内容仅供参考,不做权威认证,如若验证其真实性,请咨询相关权威专业人士。