Python编写小游戏——炮兵训练营

查看 107|回复 8
作者:sunnychen   
通过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  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

炮兵, 小游戏

Zero0fBegin   

支持支持,学习一下
繁花落尽秭归陈   

感觉随便中啊   我试了试
158025   

支持,学习
111wdw   

不错,没事放松放松
cgh121   

支持支持,拿来学习一下
STORE123   

支持中,学习了。
Yb丶   

学习了,支持
lxl092966   

我还在入门中
您需要登录后才可以回帖 登录 | 立即注册

返回顶部