找回密码
 中文实名注册
搜索
查看: 152|回复: 1

打方块(无限)

[复制链接]

21

主题

27

回帖

2389

积分

金牌会员

积分
2389
发表于 2025-8-5 10:48:25 | 显示全部楼层 |阅读模式
购买主题 已有 1 人购买  本主题需向作者支付 6 金钱 才能浏览
回复

使用道具 举报

17

主题

21

回帖

1622

积分

金牌会员

积分
1622
发表于 2025-8-5 15:11:13 | 显示全部楼层
import pygame
import random

# 初始化pygame
pygame.init()

# 设定窗口
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('火柴人射击小游戏')

# 颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)

# 火柴人类
class Stickman:
    def __init__(self):
        self.x = WIDTH // 2
        self.y = HEIGHT - 50
        self.width = 20
        self.height = 60
        self.health = 3

    def draw(self):
        pygame.draw.rect(screen, BLACK, (self.x, self.y, self.width, self.height))

# 子弹类
class Bullet:
    def __init__(self, x, y):
        self.x = x + 10  # 子弹初始位置在火柴人中间
        self.y = y
        self.radius = 5

    def move(self):
        self.y -= 10  # 向上移动

    def draw(self):
        pygame.draw.circle(screen, RED, (self.x, self.y), self.radius)

# 目标类
class Target:
    def __init__(self):
        self.x = random.randint(0, WIDTH - 50)
        self.y = random.randint(50, 200)
        self.width = 50
        self.height = 50

    def draw(self):
        pygame.draw.rect(screen, BLACK, (self.x, self.y, self.width, self.height))

# 主游戏循环
def main():
    clock = pygame.time.Clock()
    stickman = Stickman()
    bullets = []
    target = Target()
     
    score = 0
    running = True
    font = pygame.font.SysFont("Arial", 24)

    while running:
        clock.tick(30)
        screen.fill(WHITE)
         
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and stickman.x > 0:
            stickman.x -= 5
        if keys[pygame.K_RIGHT] and stickman.x < WIDTH - stickman.width:
            stickman.x += 5
        if keys[pygame.K_SPACE]:
            bullets.append(Bullet(stickman.x, stickman.y))

        # 更新子弹
        for bullet in bullets[:]:
            bullet.move()
            if bullet.y < 0:
                bullets.remove(bullet)

            # 检测击中目标
            if (
                target.x < bullet.x < target.x + target.width and
                target.y < bullet.y < target.y + target.height
            ):
                score += 1
                bullets.remove(bullet)
                target = Target()  # 生成新的目标

        # 绘制元素
        stickman.draw()
        for bullet in bullets:
            bullet.draw()
        target.draw()

        # 显示得分和生命值
        score_text = font.render(f"得分: {score}", True, BLACK)
        health_text = font.render(f"生命值: {stickman.health}", True, BLACK)
        screen.blit(score_text, (10, 10))
        screen.blit(health_text, (WIDTH - 150, 10))

        pygame.display.flip()

        # 检查生命值
        if stickman.health <= 0:
            print("游戏结束!得分:", score)
            running = False

    pygame.quit()

if __name__ == "__main__":
    main()
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 中文实名注册

本版积分规则

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