咕子吃大西瓜蛤蟆兔子

由于无聊,我让ds写了一个咕子吃大西瓜蛤蟆兔子,游戏我放在我的文件

需要咕子
大西瓜蛤蟆兔子图片

点击查看代码

import pygame
import random
import sys
import os

def resource_path(relative_path):
    """获取资源的绝对路径,兼容开发环境和 PyInstaller 打包后的环境"""
    try:
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")
    return os.path.join(base_path, relative_path)

# 初始化 Pygame
pygame.init()

# 创建游戏窗口
WIDTH, HEIGHT = 400, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Bobo Chicken Eats Razor Clams")
clock = pygame.time.Clock()

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (200, 50, 50)
YELLOW = (255, 220, 80)
BROWN = (180, 140, 100)
LIGHT_BROWN = (220, 190, 150)
GREEN = (50, 180, 80)
BLUE = (100, 150, 255)
ORANGE = (255, 150, 50)

# 字体
font_large = pygame.font.SysFont("Arial", 48, bold=True)
font_small = pygame.font.SysFont("Arial", 28)

# 加载图片(确保文件存在:player.png, clam.png, sugar.png)
player_img = pygame.image.load(resource_path("player.png")).convert_alpha()
player_img = pygame.transform.scale(player_img, (120, 120))

clam_img = pygame.image.load(resource_path("clam.png")).convert_alpha()
clam_img = pygame.transform.scale(clam_img, (40, 40))

sugar_img = pygame.image.load(resource_path("sugar.gif")).convert_alpha()
sugar_img = pygame.transform.scale(sugar_img, (40, 40))

class Player:
    def __init__(self):
        self.width = 120
        self.height = 120
        self.x = WIDTH // 2 - self.width // 2
        self.y = HEIGHT - 120
        self.speed = 12
        self.rect = pygame.Rect(self.x, self.y, self.width, self.height)

    def move(self, keys):
        if keys[pygame.K_LEFT] and self.x > 0:
            self.x -= self.speed
        if keys[pygame.K_RIGHT] and self.x < WIDTH - self.width:
            self.x += self.speed
        self.rect.x = self.x
        self.rect.y = self.y

    def draw(self, screen):
        screen.blit(player_img, (self.x, self.y))

class Clam:
    def __init__(self, speed_y=None):
        self.width = 40
        self.height = 40
        self.x = random.randint(20, WIDTH - self.width - 20)
        self.y = -self.height
        self.speed_y = speed_y if speed_y else random.randint(4, 8)
        self.rect = pygame.Rect(self.x, self.y, self.width, self.height)

    def update(self):
        self.y += self.speed_y
        self.rect.y = self.y

    def draw(self, screen):
        screen.blit(clam_img, (self.x, self.y))

class Sugar:
    def __init__(self, speed_y=None):
        self.width = 40
        self.height = 40
        self.x = random.randint(20, WIDTH - self.width - 20)
        self.y = -self.height
        self.speed_y = speed_y if speed_y else random.randint(4, 7)
        self.rect = pygame.Rect(self.x, self.y, self.width, self.height)

    def update(self):
        self.y += self.speed_y
        self.rect.y = self.y

    def draw(self, screen):
        screen.blit(sugar_img, (self.x, self.y))

def draw_text(text, font, color, surface, x, y, center=True):
    textobj = font.render(text, True, color)
    textrect = textobj.get_rect()
    if center:
        textrect.center = (x, y)
    else:
        textrect.topleft = (x, y)
    surface.blit(textobj, textrect)

def main():
    player = Player()
    clams = []
    sugars = []
    score = 0
    lives = 15
    spawn_timer = 0
    spawn_delay = 30
    sugar_spawn_timer = 0
    sugar_spawn_delay = 30  # 约每2秒生成一个糖
    game_state = "start"

    running = True
    while running:
        clock.tick(60)
        keys = pygame.key.get_pressed()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    if game_state in ["start", "gameover", "win"]:
                        # 重新开始游戏
                        game_state = "playing"
                        player = Player()
                        clams = []
                        sugars = []
                        score = 0
                        lives = 15
                        spawn_timer = 0
                        sugar_spawn_timer = 0

        screen.fill(WHITE)  # 白色背景

        if game_state == "start":
            draw_text("Bobo Chicken Eats Razor Clams", font_large, YELLOW, screen, WIDTH//2, HEIGHT//2 - 60)
            draw_text("Use LEFT/RIGHT arrows to move", font_small, BLACK, screen, WIDTH//2, HEIGHT//2)
            draw_text("Catch clams! Avoid sugar! Reach 100 points to win!", font_small, RED, screen, WIDTH//2, HEIGHT//2 + 40)
            draw_text("Press SPACE to start", font_small, BLACK, screen, WIDTH//2, HEIGHT//2 + 80)

        elif game_state == "playing":
            player.move(keys)

            # 生成蛏子
            spawn_timer += 1
            if spawn_timer >= spawn_delay:
                spawn_timer = 0
                base_speed = random.randint(4, 8) + score // 10
                clams.append(Clam(speed_y=min(base_speed, 15)))

            # 生成糖
            sugar_spawn_timer += 1
            if sugar_spawn_timer >= sugar_spawn_delay:
                sugar_spawn_timer = 0
                sugars.append(Sugar())

            # 更新蛏子
            for clam in clams[:]:
                clam.update()
                if player.rect.colliderect(clam.rect):
                    clams.remove(clam)
                    score += 1
                    # 检查胜利条件
                    if score >= 100:
                        game_state = "win"
                elif clam.y > HEIGHT:
                    clams.remove(clam)
                    lives -= 1
                    if lives <= 0:
                        game_state = "gameover"

            # 更新糖(只有还在游戏中才处理)
            if game_state == "playing":
                for sugar in sugars[:]:
                    sugar.update()
                    if player.rect.colliderect(sugar.rect):
                        sugars.remove(sugar)
                        score -= 1
                        if score <= 0:
                            lives -= 1
                            score = 0
                            if lives <= 0:
                                game_state = "gameover"
                    elif sugar.y > HEIGHT:
                        sugars.remove(sugar)  # 糖掉出屏幕不扣命

            # 绘制所有对象(即使游戏状态刚变为 win,也绘制当前帧,下一帧会切换到 win 画面)
            player.draw(screen)
            for clam in clams:
                clam.draw(screen)
            for sugar in sugars:
                sugar.draw(screen)

            # 显示分数和生命
            draw_text(f"Score: {score} / 100", font_small, BLACK, screen, 100, 30, center=False)
            draw_text(f"Lives: {lives}", font_small, RED, screen, WIDTH - 150, 30, center=False)

        elif game_state == "gameover":
            draw_text("Game Over", font_large, RED, screen, WIDTH//2, HEIGHT//2 - 60)
            draw_text(f"Final Score: {score}", font_small, BLACK, screen, WIDTH//2, HEIGHT//2)
            draw_text("Press SPACE to play again", font_small, BLACK, screen, WIDTH//2, HEIGHT//2 + 40)

        elif game_state == "win":
            draw_text("You Win!", font_large, GREEN, screen, WIDTH//2, HEIGHT//2 - 60)
            draw_text(f"Final Score: {score}", font_small, BLACK, screen, WIDTH//2, HEIGHT//2)
            draw_text("Press SPACE to play again", font_small, BLACK, screen, WIDTH//2, HEIGHT//2 + 40)

        pygame.display.flip()

if __name__ == "__main__":
    main()

主要是消遣,无不良引导

文章摘自:https://www.cnblogs.com/zhuoshu/p/22623225