吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1796|回复: 16
收起左侧

[Python 原创] Python编写小游戏——抓青蛙

[复制链接]
sunnychen 发表于 2024-4-7 17:06
本帖最后由 sunnychen 于 2024-4-9 11:26 编辑

通过Python 编写小游戏,让学习变得有趣。通过练习逐步掌握 Python 编程的基本概念和技能,也能给学习者带来成就感和乐趣。
在开发游戏时,主要用到Pygame库,用于处理游戏中的图形、音频、事件等。
抓青蛙游戏是一种有趣的反应游戏,主要用到Python的游戏循环、随机数生成和鼠标事件处理。
[Python] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import pygame
import random
 
pygame.init()
 
window_width = 800
window_height = 600
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption('抓青蛙游戏')
 
white = (255, 255, 255)
black = (0, 0, 0)
green = (0, 128, 0)
 
mole_radius = 20
mole_x = random.randint(mole_radius, window_width - mole_radius)
mole_y = random.randint(mole_radius + 100, window_height - mole_radius)
ear_radius = 5
 
score = 0
clock = pygame.time.Clock()
spawn_timer = 0
spawn_delay = 1500  # 1.5秒
 
# 显示得分
def show_score():
    font = pygame.font.SysFont(None, 36)
    score_text = font.render(f'Score: {score}', True, black)
    window.blit(score_text, (10, 10))
 
# 绘制青蛙
def draw_mole():
    pygame.draw.circle(window, green, (mole_x, mole_y), mole_radius)
    pygame.draw.circle(window, black, (mole_x - mole_radius//2, mole_y - mole_radius//2 - ear_radius), ear_radius)
    pygame.draw.circle(window, black, (mole_x + mole_radius//2, mole_y - mole_radius//2 - ear_radius), ear_radius)
 
# 显示青蛙
def show_objects():
    window.fill(white)
    draw_mole()
    pygame.draw.circle(window, black, pygame.mouse.get_pos(), 20)
    show_score()
    pygame.display.update()
 
# 绘制锤子
def draw_hammer(position):
    pygame.draw.circle(window, hammer_color, position, hammer_radius, 3)
 
# 主循环
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
 
    spawn_timer += clock.get_time()
    if spawn_timer >= spawn_delay:
        mole_x = random.randint(mole_radius, window_width - mole_radius)
        mole_y = random.randint(mole_radius + 100, window_height - mole_radius)
        spawn_timer = 0
 
    mouse_x, mouse_y = pygame.mouse.get_pos()
    if pygame.mouse.get_pressed()[0]:
        if (mole_x - mouse_x)**2 + (mole_y - mouse_y)**2 <= mole_radius**2:
            score += 1
            mole_x = random.randint(mole_radius, window_width - mole_radius)
            mole_y = random.randint(mole_radius + 100, window_height - mole_radius)
 
    show_objects()
    clock.tick(60)
 
pygame.quit()
111726.jpg

免费评分

参与人数 3吾爱币 +9 热心值 +2 收起 理由
silenter6speake + 1 我很赞同!
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
lin_xop + 1 + 1 热心回复!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

silenter6speake 发表于 2024-4-8 15:40
很久以前就像研究python来做简单的小游戏,书都买了。各种原因,都是没有沉下心学习和实践。
只能偶尔看看 这样的代码。

adewangluo 发表于 2024-4-7 18:18
booluo 发表于 2024-4-7 18:52
之前学习Python的时候写过一个2048的小游戏,挺有意思的,楼主可以试试
shengforever 发表于 2024-4-7 19:01
太牛了 学习下
bluepeb 发表于 2024-4-7 19:22
大神,闲暇时玩几把。
52bojie 发表于 2024-4-7 21:18
学习了,谢谢分享
zhmsun 发表于 2024-4-7 23:03
学python中,感谢分享,
877 发表于 2024-4-8 00:11
不错哦,玩玩试试
Ming4444 发表于 2024-4-8 12:47
不错哦,玩玩试试
cageforawalk666 发表于 2024-4-8 14:45
这个好玩,期待更新
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-4-7 00:33

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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