• 个人简介

    666

    贪吃蛇:

    import pygame
    import random
    
    pygame.init()
    
    # 游戏框架
    W = 400      # 窗口宽度
    H = 400      # 窗口高度
    S = 20       # 方块大小
    BG_COLOR = (200, 200, 200)
    BLOCK_COLOR = (0, 0, 0)
    TIME_DELAY = 100
    
    screen = pygame.display.set_mode((W, H))
    pygame.display.set_caption("贪吃蛇游戏")
    
    font = pygame.font.Font(None, 36)
    
    # 贪吃蛇初始化
    snake_bodies = [(60, 20), (40, 20), (20, 20)]
    (head_x, head_y) = snake_bodies[0]
    dx, dy = S, 0
    
    food_position = (random.randint(1, (W-S)//S)*S, random.randint(1, (H-S)//S)*S)
    
    
    def draw_block(position):
        """绘制方块"""
        rect = (position[0], position[1], S, S)
        pygame.draw.rect(screen, BLOCK_COLOR, rect)
    
    
    def move(snake_bodies, dx, dy):
        """移动贪吃蛇"""
        new_head = (snake_bodies[0][0] + dx, snake_bodies[0][1] + dy)
    
        if new_head == food_position:
            # 吃到食物
            snake_bodies.insert(0, new_head)
            generate_food()
        else:
            # 没有吃到食物
            snake_bodies.insert(0, new_head)
            snake_bodies.pop()
    
        return snake_bodies
    
    def generate_food():
        """生成随机食物"""
        global food_position
        food_position = (random.randint(1, (W-S)//S)*S, random.randint(1, (H-S)//S)*S)
    
    # 游戏主循环
    while True:
        # 事件处理
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP and dy == 0:
                    dx, dy = 0, -S
                elif event.key == pygame.K_DOWN and dy == 0:
                    dx, dy = 0, S
                elif event.key == pygame.K_LEFT and dx == 0:
                    dx, dy = -S, 0
                elif event.key == pygame.K_RIGHT and dx == 0:
                    dx, dy = S, 0
    
        screen.fill(BG_COLOR)
    
        # 绘制贪吃蛇和食物
        for body in snake_bodies:
            draw_block(body)
    
        draw_block(food_position)
    
        # 移动贪吃蛇
        snake_bodies = move(snake_bodies, dx, dy)
    
        # 判断是否死亡
        (head_x, head_y) = snake_bodies[0]
        if head_x < 0 or head_x > W-S or head_y < 0 or head_y > H-S or (head_x, head_y) in snake_bodies[1:]:
            # 显示游戏结束信息,然后退出游戏
            text = font.render("GAME OVER", True, (255, 0, 0))
            text_rect = text.get_rect()
            text_x = screen.get_width() / 2 - text_rect.width / 2
            text_y = screen.get_height() / 2 - text_rect.height / 2
            screen.blit(text, [text_x, text_y])
            pygame.display.flip()
            pygame.time.delay(5000)
            pygame.quit()
            quit()
    
        pygame.display.flip()
        pygame.time.delay(TIME_DELAY)
    

    射只因游戏:

    import pygame
    import random
    
    # 初始化游戏
    pygame.init()
    
    # 设定屏幕大小和标题
    screen_width = 800
    screen_height = 600
    screen = pygame.display.set_mode((screen_width, screen_height))
    pygame.display.set_caption("射击小游戏")
    
    # 设置背景颜色和玩家/敌人的颜色
    background_color = (255, 255, 255)
    player_color = (0, 0, 255)
    enemy_color = (255, 0, 0)
    
    # 玩家初始位置和大小
    player_x = screen_width // 2 - 25
    player_y = screen_height - 50
    player_width = 50
    player_height = 50
    
    # 敌人初始位置和大小
    enemy_x = random.randint(0, screen_width - 20)
    enemy_y = 0
    enemy_width = 20
    enemy_height = 20
    
    # 初始分数和游戏结束标志
    score = 0
    game_over = False
    
    # 游戏主循环
    while not game_over:
        # 处理事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_over = True
            elif event.type == pygame.MOUSEBUTTONDOWN:
                # 判断是否点击到敌人
                if enemy_x <= event.pos[0] <= enemy_x + enemy_width and enemy_y <= event.pos[1] <= enemy_y + enemy_height:
                    score += 1
                    enemy_x = random.randint(0, screen_width - 20)
                    enemy_y = 0
    
        # 移动敌人
        enemy_y += 1
    
        # 判断游戏结束条件
        if enemy_y > screen_height:
            game_over = True
    
        # 填充背景色
        screen.fill(background_color)
    
        # 绘制玩家和敌人
        pygame.draw.rect(screen, player_color, (player_x, player_y, player_width, player_height))
        pygame.draw.rect(screen, enemy_color, (enemy_x, enemy_y, enemy_width, enemy_height))
    
        # 显示分数
        font = pygame.font.Font(None, 36)
        score_text = font.render("Score: " + str(score), True, (0, 0, 0))
        screen.blit(score_text, (10, 10))
    
        # 更新屏幕显示
        pygame.display.flip()
    
    # 游戏结束,退出pygame
    pygame.quit()
    
  • 通过的题目

  • 最近活动

    This person is lazy and didn't join any contests or homework.
  • 最近编写的题解

    This person is lazy and didn't write any solutions.
  • Stat

  • Rating

题目标签

字符串
3
字典树
3
系统测试
1