猫史档案馆


yycrobin

yycrobin

Lv.1

请输入文本。。

获赞:32收藏:8浏览:170作品收藏:1
回复帖子评论
上一页1 页 / 共 1下一页

怎么做贪吃蛇游戏 中回复

import pygame
import random

# 初始化 Pygame
pygame.init()

# 设置游戏区域宽度和高度,以及每个游戏格子的大小
WINDOW_WIDTH = 600
WINDOW_HEIGHT = 600
CELL_SIZE = 30

# 设置游戏界面标题
pygame.display.set_caption("贪吃蛇游戏")

# 创建游戏区域
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))

# 设置游戏的字体(用于显示分数)
font = pygame.font.SysFont("Arial", 24)

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

# 定义游戏区域的横纵坐标数量
CELL_WIDTH = int(WINDOW_WIDTH / CELL_SIZE)
CELL_HEIGHT = int(WINDOW_HEIGHT / CELL_SIZE)

# 定义蛇的初始长度
INIT_SNAKE_LENGTH = 3

# 定义移动方向
UP = 1
DOWN = 2
LEFT = 3
RIGHT = 4


def draw_grid():
    """
    绘制游戏区域网格
    """
    for x in range(WINDOW_WIDTH):
        pygame.draw.line(screen, WHITE, (x * CELL_SIZE, 0),
                         (x * CELL_SIZE, WINDOW_HEIGHT))

    for y in range(WINDOW_HEIGHT):
        pygame.draw.line(screen, WHITE, (0, y * CELL_SIZE),
                         (WINDOW_WIDTH, y * CELL_SIZE))


def draw_cell(x, y, color):
    """
    画出一格游戏格子
    """
    pygame.draw.rect(screen, color, (x * CELL_SIZE, y *
                     CELL_SIZE, CELL_SIZE, CELL_SIZE))


class Snake:
    """
    蛇的类
    """

    def __init__(self):
        self.length = INIT_SNAKE_LENGTH
        self.direction = RIGHT
        self.body = [(x, 0) for x in range(self.length)]

    def move(self):
        """
        更新蛇的身喵置
        """
        head_x, head_y = self.body[-1]

        if self.direction == UP:
            new_head = (head_x, head_y - 1)
        elif self.direction == DOWN:
            new_head = (head_x, head_y + 1)
        elif self.direction == LEFT:
            new_head = (head_x - 1, head_y)
        else:
            new_head = (head_x + 1, head_y)

        self.body.append(new_head)

        if len(self.body) > self.length:
            self.body.pop(0)

    def change_direction(self, new_direction):
        """
        改变蛇移动方向
        """
        if new_direction == UP and self.direction != DOWN:
            self.direction = UP
        elif new_direction == DOWN and self.direction != UP:
            self.direction = DOWN
        elif new_direction == LEFT and self.direction != RIGHT:
            self.direction = LEFT
        elif new_direction == RIGHT and self.direction != LEFT:
            self.direction = RIGHT

    def draw(self):
        """
        绘制蛇
        """
        for x, y in self.body:
            draw_cell(x, y, GREEN)

    def check_collision(self):
        """
        检查蛇是否与自己撞上
        """
        head_x, head_y = self.body[-1]
        for x, y in self.body[:-1]:
            if x == head_x and y == head_y:
                return True
        return False


class Food:
    """
    食物的类
    """

    def __init__(self):
        self.x = random.randint(0, CELL_WIDTH - 1)
        self.y = random.randint(0, CELL_HEIGHT - 1)

    def draw(self):
        """
        绘制食物
        """
        draw_cell(self.x, self.y, RED)


def main():
    """
    游戏主函数
    """
    snake = Snake()
    food = Food()
    score = 0

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    snake.change_direction(UP)
                elif event.key == pygame.K_DOWN:
                    snake.change_direction(DOWN)
                elif event.key == pygame.K_LEFT:
                    snake.change_direction(LEFT)
                elif event.key == pygame.K_RIGHT:
                    snake.change_direction(RIGHT)

        # 清除屏幕
        screen.fill(BLACK)

        # 绘制游戏区域网格
        draw_grid()

        # 绘制蛇
        snake.draw()

        # 绘制食物
        food.draw()

        # 更新蛇的位置
        snake.move()

        # 检测碰撞
        if snake.check_collision():
            gameover = font.render("Game Over!", True, WHITE)
            gameover_rect = gameover.get_rect(
                center=(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2))
            screen.blit(gameover, gameover_rect)
            pygame.display.update()
            pygame.time.delay(2000)
            main()

        # 检测是否吃到食物
        if snake.body[-1][0] == food.x and snake.body[-1][1] == food.y:
            snake.length += 1
            score += 10
            food = Food()

        # 显示分数
        score_text = font.render("Score: {}".format(score), True, WHITE)
        screen.blit(score_text, (10, 10))

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

        # 暂停
        pygame.time.delay(100)


if __name__ == "__main__":
    main()

2023-07-21T14:54:53 点赞:0

怎么做贪吃蛇游戏 中回复

做出来了吗?

2023-07-22T20:03:02 点赞:0

【版本更新合辑】那些年Kitten更新过的那些功能~ 中回复

.sb3文件怎么打不开?

 

2023-07-22T21:58:23 点赞:2

【求助帖】 中回复

你把源代码发给我,我给你正确的代码。

2023-08-14T19:58:03 点赞:0

【Python作品分享】下落的雨点【作品秀】 中回复

动图:

center_image

2023-08-14T20:04:12 点赞:0

【求助帖】 中回复

import pygame
import random

WIDTH = 900
HEIGHT = 600

pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Game")
clock = pygame.time.Clock()

class Ball(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.Surface((50, 50))
        self.image.fill((255, 0, 0))
        self.rect = self.image.get_rect(center=(WIDTH//2, HEIGHT//2))
        self.xvel = random.choice([-5, 5])
        self.yvel = random.choice([-5, 5])

    def update(self):
        self.rect.x += self.xvel
        self.rect.y += self.yvel

sprite_list = pygame.sprite.Group()
ball = Ball()
sprite_list.add(ball)

keep_going = True
while keep_going:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            keep_going = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            keep_going = True
        elif event.type == pygame.MOUSEBUTTONUP:
            pos = pygame.mouse.get_pos()
            for sprite in sprite_list:
                if sprite.rect.collidepoint(pos):
                    sprite_list.remove(sprite)

    screen.fill((255, 255, 255))
    sprite_list.update()
    sprite_list.draw(screen)
    pygame.display.flip()

    clock.tick(60)

pygame.quit()

2023-08-18T20:55:48 点赞:0

怎么解决这个BUG? 中回复

你截个图发过来

2023-09-01T17:10:28 点赞:0

华为手机模拟器 中回复

center_image

2023-12-09T21:48:13 点赞:0

华为浏览器电脑💻模式成功喵过bcm。。。。。。 中回复

center_image

2023-12-09T21:49:09 点赞:0

华为浏览器电脑💻模式成功喵过bcm。。。。。。 中回复

2023-12-09T21:49:28 点赞:0

华为浏览器电脑💻模式成功喵过bcm。。。。。。 中回复

我有一台奇怪的电脑center_image

2023-12-10T20:37:08 点赞:0

编程猫NEMO 在华为手机 上闪退 中回复

用一下鸿蒙4.0

2023-12-10T20:44:07 点赞:0