运用python编写简易贪吃蛇小游戏

import pygame
import random

pygame.init()

WIDTH, HEIGHT = 600, 400
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("贪吃蛇")

WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)

UP = 0
DOWN = 1
LEFT = 2
RIGHT = 3

class Snake:
    def __init__(self):
        self.body = [(200, 200), (210, 200), (220, 200)]
        self.direction = random.choice([UP, DOWN, LEFT, RIGHT])
        self.score = 0

    def move(self):
        head = self.body[0]
        x, y = head

        if self.direction == UP:
            y -= 10
        elif self.direction == DOWN:
            y += 10
        elif self.direction == LEFT:
            x -= 10
        elif self.direction == RIGHT:
            x += 10

        self.body.insert(0, (x, y))
        self.body.pop()

    def grow(self):
        tail = self.body[-1]
        x, y = tail

        if self.direction == UP:
            y += 10
        elif self.direction == DOWN:
            y -= 10
        elif self.direction == LEFT:
            x += 10
        elif self.direction == RIGHT:
            x -= 10

        self.body.append((x, y))
        self.score += 1

    def draw(self, surface):
        for segment in self.body:
            pygame.draw.rect(surface, GREEN, (segment[0], segment[1], 10, 10))

class Food:
    def __init__(self):
        self.position = (random.randint(0, (WIDTH - 10) // 10) * 10, random.randint(0, (HEIGHT - 10) // 10) * 10)

    def draw(self, surface):
        pygame.draw.rect(surface, RED, (self.position[0], self.position[1], 10, 10))

def main():
    snake = Snake()
    food = Food()
    clock = pygame.time.Clock()

    running = True
    while running:
        WIN.fill(WHITE)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP and snake.direction != DOWN:
                    snake.direction = UP
                elif event.key == pygame.K_DOWN and snake.direction != UP:
                    snake.direction = DOWN
                elif event.key == pygame.K_LEFT and snake.direction != RIGHT:
                    snake.direction = LEFT
                elif event.key == pygame.K_RIGHT and snake.direction != LEFT:
                    snake.direction = RIGHT

        head = snake.body[0]
        if head[0] < 0 or head[0] >= WIDTH or head[1] < 0 or head[1] >= HEIGHT:
            print("Game Over!")
            running = False

        snake.move()

        if snake.body[0] == food.position:
            snake.grow()
            food = Food()

        snake.draw(WIN)
        food.draw(WIN)

        pygame.display.update()
        clock.tick(10)  
    pygame.quit()

if __name__ == "__main__":
    main()

  • 34
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值