本程序是一个简单的端午节主题小游戏,玩家需要控制龙舟在河道中避开障碍物并收集粽子,满足一定的条件后即可过关。以下是程序的代码:

import pygame
import random

# 初始化pygame
pygame.init()

# 设置游戏窗口大小
window_width = 800
window_height = 600
window = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("端午节龙舟比赛")

# 加载游戏素材
bg_img = pygame.image.load("bg.jpg").convert()
boat_img = pygame.image.load("boat.png").convert_alpha()
obstacle_img = pygame.image.load("obstacle.png").convert_alpha()
zongzi_img = pygame.image.load("zongzi.png").convert_alpha()
font = pygame.font.Font(None, 36)

# 定义游戏变量
score = 0
health = 3
game_over = False
game_win = False

# 定义游戏对象类
class GameObject:
    def __init__(self, x, y, img):
        self.x = x
        self.y = y
        self.img = img
        self.width = img.get_width()
        self.height = img.get_height()

    def draw(self):
        window.blit(self.img, (self.x, self.y))

# 定义龙舟类
class Boat(GameObject):
    def move(self, dx, dy):
        self.x += dx
        self.y += dy
        if self.x < 0:
            self.x = 0
        elif self.x > window_width - self.width:
            self.x = window_width - self.width
        if self.y < 0:
            self.y = 0
        elif self.y > window_height - self.height:
            self.y = window_height - self.height

# 定义障碍物类
class Obstacle(GameObject):
    def move(self, dx, dy):
        self.x += dx
        self.y += dy
        if self.x < -self.width:
            self.reset()

    def reset(self):
        self.x = window_width + random.randint(100, 300)
        self.y = random.randint(0, window_height - self.height)

# 定义粽子类
class Zongzi(GameObject):
    def move(self, dx, dy):
        self.x += dx
        self.y += dy
        if self.x < -self.width:
            self.reset()

    def reset(self):
        self.x = window_width + random.randint(100, 300)
        self.y = random.randint(0, window_height - self.height)

# 创建游戏对象
boat = Boat(50, window_height // 2, boat_img)
obstacles = [Obstacle(800 + i * 400, random.randint(0, window_height - obstacle_img.get_height()), obstacle_img) for i in range(3)]
zongzis = [Zongzi(800 + i * 600, random.randint(0, window_height - zongzi_img.get_height()), zongzi_img) for i in range(2)]

# 定义游戏循环
while not game_over and not game_win:
    # 处理游戏事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                boat.move(0, -10)
            elif event.key == pygame.K_DOWN:
                boat.move(0, 10)
            elif event.key == pygame.K_LEFT:
                boat.move(-10, 0)
            elif event.key == pygame.K_RIGHT:
                boat.move(10, 0)

    # 绘制背景
    window.blit(bg_img, (0, 0))

    # 移动障碍物和粽子
    for obstacle in obstacles:
        obstacle.move(-5, 0)
        obstacle.draw()
        if obstacle.x < boat.x + boat.width and obstacle.x + obstacle.width > boat.x and obstacle.y < boat.y + boat.height and obstacle.y + obstacle.height > boat.y:
            health -= 1
            obstacle.reset()
    for zongzi in zongzis:
        zongzi.move(-3, 0)
        zongzi.draw()
        if zongzi.x < boat.x + boat.width and zongzi.x + zongzi.width > boat.x and zongzi.y < boat.y + boat.height and zongzi.y + zongzi.height > boat.y:
            score += 10
            zongzi.reset()

    # 绘制龙舟
    boat.draw()

    # 绘制分数和生命值
    score_text = font.render("得分:" + str(score), True, (255, 255, 255))
    window.blit(score_text, (20, 20))
    health_text = font.render("生命:" + "♥" * health, True, (255, 255, 255))
    window.blit(health_text, (window_width - health_text.get_width() - 20, 20))

    # 判断游戏是否结束
    if health <= 0:
        game_over = True
    elif score >= 50:
        game_win = True

    # 更新屏幕
    pygame.display.update()

# 游戏结束,显示结果
if game_over:
    result_text = font.render("你输了,再接再厉!", True, (255, 255, 255))
elif game_win:
    result_text = font.render("你赢了,恭喜通关!", True, (255, 255, 255))
while True:
    window.blit(bg_img, (0, 0))
    window.blit(result_text, ((window_width - result_text.get_width()) // 2, (window_height - result_text.get_height()) // 2))
    pygame.display.update()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit(
格物编程科创板端午节程序

原文地址: https://www.cveoy.top/t/topic/hfog 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录