随笔 - 27,  文章 - 1,  评论 - 4,  阅读 - 63358
准备工作开发环境

Python版本:3.7.8

相关模块:
requests模块;
tqdm模块;
pyfreeproxy模块;
pyechats模块;
以及一些python自带的模块。

效果预览
开始界面

 

游戏规则

wasd 控制人物的移动,空格启动律师函炸毁全部篮球。

 

 

 

 

 

 代码实现

导入模块

复制代码
import pygame
import sys
import traceback
import os
import CXK
import enemy
import bullet
import supply
from pygame.locals import *
from random import *
复制代码

 

 游戏主界面

复制代码
#游戏主界面
def ui():
    #循环播放背景音乐
    pygame.mixer.music.play(-1)

    #初始化界面按键图片并获取图片的矩形位置
    start_game_image = pygame.image.load("images/start_game.png").convert_alpha()
    start_game_image_rect = start_game_image.get_rect()
    game_rules_image = pygame.image.load("images/game_rules.png").convert_alpha()
    game_rules_image_rect = game_rules_image.get_rect()
    game_quit_image = pygame.image.load("images/game_quit.png").convert_alpha()
    game_quit_image_rect = game_quit_image.get_rect()

    #初始化游戏规则图片并获取图片的矩形位置
    rules_image = pygame.image.load("images/游戏玩法.png").convert_alpha()
    back_image = pygame.image.load("images/back.png").convert_alpha()
    back_image_rect =  back_image.get_rect()

    #标志是否在主界面
    is_ui = True

    #帧率
    clock = pygame.time.Clock()

    #主界面循环
    while True:
        #获取事件信息
        for event in pygame.event.get():
            #如果点击右上角退出
            if event.type == QUIT:
                #退出程序
                pygame.quit()
                sys.exit()

        #如果是主界面
        if is_ui:
            #绘制背景
            screen.blit(background,(0,0))

            #更改主界面按键图片的矩形位置并绘制主界面按键
            start_game_image_rect.left,start_game_image_rect.top = (width - start_game_image_rect.width)//2,height - 500
            screen.blit(start_game_image,start_game_image_rect)

            game_rules_image_rect = game_rules_image.get_rect()
            game_rules_image_rect.left,game_rules_image_rect.top = (width - game_rules_image_rect.width)//2,start_game_image_rect.bottom+50
            screen.blit(game_rules_image,game_rules_image_rect)

            game_quit_image_rect.left,game_quit_image_rect.top = (width - game_quit_image_rect.width)//2, game_rules_image_rect.bottom+50
            screen.blit(game_quit_image,game_quit_image_rect)

            #检测用户的鼠标操作
            #如果用户按下鼠标左键
            if pygame.mouse.get_pressed()[0]:
                #获取鼠标坐标
                pos = pygame.mouse.get_pos()
                #如果用户点击”开始游戏“
                if start_game_image_rect.left < pos[0] < start_game_image_rect.right and start_game_image_rect.top < pos[1] < start_game_image_rect.bottom:
                    #调用主函数
                    main()
                #如果用户点击”退出游戏“
                if game_quit_image_rect.left < pos[0] < game_quit_image_rect.right and game_quit_image_rect.top < pos[1] < game_quit_image_rect.bottom:
                    pygame.quit()
                    sys.exit()
                #如果用户点击”游戏规则“
                if game_rules_image_rect.left < pos[0] < game_rules_image_rect.right and game_rules_image_rect.top < pos[1] < game_rules_image_rect.bottom:
                    #离开主界面
                    is_ui = False
复制代码

 

 

posted on   小明谈Python  阅读(498)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示