首页 > Python资料 博客日记
python小游戏——躲避球(可当课设)
2024-09-09 13:00:05Python资料围观46次
Python资料网推荐python小游戏——躲避球(可当课设)这篇文章给大家,欢迎收藏Python资料网享受知识的乐趣
游戏简介:
没有美术,画面简洁(懒得做)。玩家控制小球躲避敌人(上下左右,闪避),敌人体积越大速度越慢,随机生成道具球(目前只有生命球),靠近道具球可拾取。
未来展望:
1. 添加其他道具球
2. 添加攻击手段,目前只能闪避。
3. 添加耐力条
4. 添加更多属性
核心代码
玩家移动
def player_move(space_down, player):
"""
控制玩家移动处理
:param space_down: 是否按下空格
:param player: 玩家
:return:
"""
global monitor
if space_down:
speed = player.dodge_speed
else:
speed = player.speed
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player.x -= speed
if keys[pygame.K_RIGHT]:
player.x += speed
if keys[pygame.K_UP]:
player.y -= speed
if keys[pygame.K_DOWN]:
player.y += speed
if player.x < 0:
player.x = monitor.width + player.x
elif player.x > monitor.width:
player.x = player.x - monitor.width
if player.y < 0:
player.y = monitor.height + player.y
elif player.y > monitor.height:
player.y = player.y - monitor.height
生成小怪
def make_enemy():
"""
生成小怪
:return:
"""
global monitor, score
boss = False
if score % 20000 == 0 and score != 0:
boss = True
sizeList = [10, 30, 50, 70, 100, 130, 150, 180, 200, 250]
random_int = random.randint(1, 10) if boss is False else 20
enemy = Enemy(
atc=random_int,
max_health=random_int,
defense=0,
speed=(11 - random_int) * 1.5 if boss is False else 1.5,
attribute=0,
x=random.uniform(0.1, 1) * monitor.width,
y=random.uniform(0.1, 1) * monitor.height,
size=sizeList[random_int - 1] if boss is False else 500
)
return enemy
道具球处理
def propBall_handle(propBall_list, window, player):
"""
道具球处理
:param propBall_list:
:param window:
:param player:
:return:
"""
count = 0
for propBall in propBall_list:
pygame.draw.circle(window, propBall.color, (propBall.x, propBall.y), propBall.size)
propBall.moveToPlayer(player.x, player.y)
if detectIntersect(player, propBall):
propBall_function(player, propBall)
del propBall_list[count]
count += 1
if score % 200 == 0:
propBall = generate_propBall()
if propBall is not None:
propBall_list.append(propBall)
return propBall_list
游戏主要逻辑
def main():
global is_running, is_playing, font, pass_time, pass_time_made, score, start_time, monitor
propBall_list = []
window = pygame.display.set_mode((monitor.width, monitor.height))
pygame.display.set_caption("demo")
player = init_player()
health_bal = pygame.Rect(20, 20, player.health * 20, 20)
enemyList = [make_enemy()]
button_playAgain = pygame.Rect(monitor.width // 2 - button_width // 2, monitor.height * 0.6,
button_width, button_height)
button_quit = pygame.Rect(monitor.width // 2 - button_width // 2,
monitor.height * 0.6 + 30 + button_height,
button_width, button_height)
buttonList = [button_playAgain, button_quit]
while is_running:
score += 1
window.fill(color_dict['black']) # 填充黑色
health_bal.width = player.health * 20
space_down = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_running = False
elif event.type == KEYDOWN:
if event.key == K_SPACE:
space_down = True
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = pygame.mouse.get_pos()
button_serialNum = button_clicked(buttonList, mouse_pos)
if button_serialNum == 0:
is_playing = True
player = init_player()
enemyList = [make_enemy()]
elif button_serialNum == 1:
is_running = False
if is_playing:
if pass_time_made is False:
pass_time = 0
start_time = time.perf_counter()
pass_time_made = True
if player.health == 0:
is_playing = False
if score % 400 == 0:
enemyList.append(make_enemy())
propBall_list = propBall_handle(propBall_list, window, player)
player_move(space_down, player) # 玩家移动
player_twinkle(player, window) # 玩家绘制
draw_healthBar(window, player, health_bal) # 血条更新
make_enemyThreading(enemyList, window, player) # 小怪更新
draw_score(window)
pass_time = int(time.perf_counter() - start_time)
else:
draw_scoreTitle(window)
draw_button(buttonList, window)
pass_time_made = False
propBall_list = []
enemyList = []
pygame.display.flip() # 刷新屏幕
time.sleep(0.01)
pygame.quit()
游戏画面
完整代码
有需要者自取,盘内还有打包好的exe文件
链接:https://pan.baidu.com/s/1rZ1xNZJYtvyXPIG9Rgh5Hw
提取码:iq6l
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签:
相关文章
最新发布
- 【Python】selenium安装+Microsoft Edge驱动器下载配置流程
- Python 中自动打开网页并点击[自动化脚本],Selenium
- Anaconda基础使用
- 【Python】成功解决 TypeError: ‘<‘ not supported between instances of ‘str’ and ‘int’
- manim边学边做--三维的点和线
- CPython是最常用的Python解释器之一,也是Python官方实现。它是用C语言编写的,旨在提供一个高效且易于使用的Python解释器。
- Anaconda安装配置Jupyter(2024最新版)
- Python中读取Excel最快的几种方法!
- Python某城市美食商家爬虫数据可视化分析和推荐查询系统毕业设计论文开题报告
- 如何使用 Python 批量检测和转换 JSONL 文件编码为 UTF-8
点击排行
- 版本匹配指南:Numpy版本和Python版本的对应关系
- 版本匹配指南:PyTorch版本、torchvision 版本和Python版本的对应关系
- Python 可视化 web 神器:streamlit、Gradio、dash、nicegui;低代码 Python Web 框架:PyWebIO
- 相关性分析——Pearson相关系数+热力图(附data和Python完整代码)
- Python与PyTorch的版本对应
- Anaconda版本和Python版本对应关系(持续更新...)
- Python pyinstaller打包exe最完整教程
- Could not build wheels for llama-cpp-python, which is required to install pyproject.toml-based proj