吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2381|回复: 29
收起左侧

[Python 原创] Python编写小游戏——炮兵训练营

  [复制链接]
sunnychen 发表于 2024-4-18 15:31
通过Python 编写小游戏,让学习变得有趣。通过练习逐步掌握 Python 编程的基本概念和技能,也能给学习者带来成就感和乐趣。
在开发游戏时,主要用到Pygame库,用于处理游戏中的图形、音频、事件等。
炮兵训练营是一种有趣的技巧游戏,主要用到Python的游戏循环、随机数生成和键盘事件处理。
[Python] 纯文本查看 复制代码
import pygame
import sys
import math
import random
import time

# 初始化 Pygame
pygame.init()

# 设置窗口大小
WIDTH, HEIGHT = 800, 600
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("炮兵训练营")

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
tree_x, tree_y = 50, HEIGHT - 200
tree_destroyed = False

cannon_x, cannon_y = WIDTH - 150, HEIGHT - 100
cannon_angle = math.pi * 3 / 4 
cannon_angle_step = math.pi / 36

cannonball_radius = 15
cannonball_speed = 10
cannonball_fired = False
cannonball_hit_tree = False

random.seed(time.time())
tree_x = random.randint(40, round(WIDTH/3))
score = 0
gravity = 1

run = True
font = pygame.font.Font("c:/windows/fonts/simsun.ttc", 20)
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                if not cannonball_fired:
                    cannonball_fired = True
                    cannonball_x = cannon_x + 100
                    cannonball_y = cannon_y + 10
                    cannonball_velocity_x = cannonball_speed * math.cos(cannon_angle)
                    cannonball_velocity_y = -cannonball_speed * math.sin(cannon_angle)
            elif event.key == pygame.K_UP:
                cannon_angle -= cannon_angle_step
            elif event.key == pygame.K_DOWN:
                cannon_angle += cannon_angle_step
            elif event.key == pygame.K_LEFT:
                gravity -= 1
                if gravity < 1:
                    gravity = 1
            elif event.key == pygame.K_RIGHT:
                gravity += 1
                if gravity > 5:
                    gravity = 5

    if cannonball_fired and not cannonball_hit_tree:
        cannonball_x += cannonball_velocity_x
        cannonball_velocity_y += 0.2 - (gravity * 0.01) 
        cannonball_y += cannonball_velocity_y
        if cannonball_y > HEIGHT:
            cannonball_fired = False

    win.fill(WHITE)

    if not tree_destroyed:
        if cannonball_hit_tree:
            pygame.draw.polygon(win, RED, [(tree_x + 50, tree_y), (tree_x, tree_y + 150), (tree_x + 100, tree_y + 150)]) 
        else:
            pygame.draw.polygon(win, GREEN, [(tree_x + 50, tree_y), (tree_x, tree_y + 150), (tree_x + 100, tree_y + 150)])
            
        pygame.draw.rect(win, BLACK, (tree_x + 45, tree_y + 150, 10, 50))
    cannonball_hit_tree=False

    pygame.draw.line(win, BLACK, (cannon_x + 100, cannon_y + 10),
                     (cannon_x + 100 + 50 * math.cos(cannon_angle),
                      cannon_y + 10 - 50 * math.sin(cannon_angle)), 3)

    if cannonball_fired:
        pygame.draw.circle(win, BLACK, (int(cannonball_x), int(cannonball_y)), cannonball_radius)

    if not tree_destroyed and cannonball_fired:
        if tree_x < cannonball_x < tree_x + 100 and tree_y < cannonball_y < tree_y + 200:
            cannonball_x = -50
            score += 1
            cannonball_hit_tree=True
            tree_x = random.randint(40, round(WIDTH/3))

    score_text = font.render("得分: " + str(score), True, BLACK)
    win.blit(score_text, (10, 10))
    gravity_text = font.render("当前火力: " + str(gravity), True, BLACK)
    win.blit(gravity_text, (WIDTH - 130, HEIGHT - 70))
    gravity_text = font.render("上下键调整角度,左右键火力 ", True, BLACK)
    win.blit(gravity_text, (WIDTH - 280, HEIGHT - 30))

    pygame.display.update()
    pygame.time.Clock().tick(30)

pygame.quit()
sys.exit()

152532.jpg

免费评分

参与人数 4吾爱币 +8 热心值 +4 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
heitxin + 1 用心讨论,共获提升!
wanfon + 1 + 1 热心回复!
lyj722 + 1 谢谢@Thanks!

查看全部评分

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

sun420 发表于 2024-4-25 14:22
我的运行报错了,是版本跟代码不兼容吗?

pygame 2.5.2 (SDL 2.28.3, Python 3.11.0)
Hello from the pygame community. https://www.pygame.org/contribute.html
繁花落尽秭归陈 发表于 2024-4-18 16:06
158025 发表于 2024-4-18 16:38
111wdw 发表于 2024-4-18 16:43
不错,没事放松放松
cgh121 发表于 2024-4-18 17:01
支持支持,拿来学习一下
Yb丶 发表于 2024-4-18 18:57
学习了,支持
lxl092966 发表于 2024-4-18 19:40
我还在入门中
gofighting 发表于 2024-4-18 20:32
看起来挺厉害啊
52PJ070 发表于 2024-4-18 20:42
游戏性趣味性还挺不错的,美化稍差点,总体不错,感谢原创分享!期待持续提升
52bojie 发表于 2024-4-18 21:35
学习了,谢谢分享
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

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

GMT+8, 2024-11-24 10:49

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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