【pygame游戏】用Python实现一个蔡徐坤大战篮球的小游戏,可还行?【附源码 ...

鼠扑  金牌会员 | 2022-12-25 15:10:16 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 576|帖子 576|积分 1728

准备工作开发环境
Python版本:3.7.8
相关模块:
requests模块;
tqdm模块;
pyfreeproxy模块;
pyechats模块;
以及一些python自带的模块。
效果预览
开始界面

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

 

 
 
 
 
 代码实现
导入模块
  1. import pygame
  2. import sys
  3. import traceback
  4. import os
  5. import CXK
  6. import enemy
  7. import bullet
  8. import supply
  9. from pygame.locals import *
  10. from random import *
复制代码
 
 游戏主界面
  1. #游戏主界面
  2. def ui():
  3.     #循环播放背景音乐
  4.     pygame.mixer.music.play(-1)
  5.     #初始化界面按键图片并获取图片的矩形位置
  6.     start_game_image = pygame.image.load("images/start_game.png").convert_alpha()
  7.     start_game_image_rect = start_game_image.get_rect()
  8.     game_rules_image = pygame.image.load("images/game_rules.png").convert_alpha()
  9.     game_rules_image_rect = game_rules_image.get_rect()
  10.     game_quit_image = pygame.image.load("images/game_quit.png").convert_alpha()
  11.     game_quit_image_rect = game_quit_image.get_rect()
  12.     #初始化游戏规则图片并获取图片的矩形位置
  13.     rules_image = pygame.image.load("images/游戏玩法.png").convert_alpha()
  14.     back_image = pygame.image.load("images/back.png").convert_alpha()
  15.     back_image_rect =  back_image.get_rect()
  16.     #标志是否在主界面
  17.     is_ui = True
  18.     #帧率
  19.     clock = pygame.time.Clock()
  20.     #主界面循环
  21.     while True:
  22.         #获取事件信息
  23.         for event in pygame.event.get():
  24.             #如果点击右上角退出
  25.             if event.type == QUIT:
  26.                 #退出程序
  27.                 pygame.quit()
  28.                 sys.exit()
  29.         #如果是主界面
  30.         if is_ui:
  31.             #绘制背景
  32.             screen.blit(background,(0,0))
  33.             #更改主界面按键图片的矩形位置并绘制主界面按键
  34.             start_game_image_rect.left,start_game_image_rect.top = (width - start_game_image_rect.width)//2,height - 500
  35.             screen.blit(start_game_image,start_game_image_rect)
  36.             game_rules_image_rect = game_rules_image.get_rect()
  37.             game_rules_image_rect.left,game_rules_image_rect.top = (width - game_rules_image_rect.width)//2,start_game_image_rect.bottom+50
  38.             screen.blit(game_rules_image,game_rules_image_rect)
  39.             game_quit_image_rect.left,game_quit_image_rect.top = (width - game_quit_image_rect.width)//2, game_rules_image_rect.bottom+50
  40.             screen.blit(game_quit_image,game_quit_image_rect)
  41.             #检测用户的鼠标操作
  42.             #如果用户按下鼠标左键
  43.             if pygame.mouse.get_pressed()[0]:
  44.                 #获取鼠标坐标
  45.                 pos = pygame.mouse.get_pos()
  46.                 #如果用户点击”开始游戏“
  47.                 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:
  48.                     #调用主函数
  49.                     main()
  50.                 #如果用户点击”退出游戏“
  51.                 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:
  52.                     pygame.quit()
  53.                     sys.exit()
  54.                 #如果用户点击”游戏规则“
  55.                 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:
  56.                     #离开主界面
  57.                     is_ui = False
复制代码

 
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

鼠扑

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表