Python 简易版贪食蛇(源代码)

时间:2022-10-28 16:06:06

Python 简易版贪食蛇

简易版贪食蛇代码如下,直接运行即可。

1. 效果图

Python 简易版贪食蛇(源代码)

2.源代码

源代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pygame as pygame
import random
import sys

from pygame.rect import Rect


class Snake(object):
    def __init__(self):
        self.black = pygame.Color(0, 0, 0)
        self.green = pygame.Color(0, 255, 0)
        self.white = pygame.Color(255, 255, 255)

    def gameover(self):
        pygame.quit()
        sys.exit()

    def initialize(self):
        pygame.init()
        clock = pygame.time.Clock()

        playSurface = pygame.display.set_mode((800, 600))

        pygame.display.set_caption('tanshishe')

        snakePosition = [80, 80]

        snakebody = [[80, 80], [60, 80], [40, 80]]

        targetPosition = [200, 400]
        targetflag = 1
        direction = 'right'

        changeDirection = direction
        self.main(snakebody, targetPosition, targetflag, direction, changeDirection, snakePosition, playSurface, clock)

    def main(self, snakebody, targetPosition, targetflag, direction, changeDirection, snakePosition, playSurface,
             clock):
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()

                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_RIGHT:
                        changeDirection = 'right'
                        print("向右转")
                    if event.key == pygame.K_LEFT:
                        changeDirection = 'left'
                        print("向左转")
                    if event.key == pygame.K_DOWN:
                        changeDirection = 'down'
                        print("向上走")
                    if event.key == pygame.K_UP:
                        changeDirection = 'up'
                        print("向下走")
                    if event.key == pygame.K_ESCAPE:
                        pygame.event.post(pygame.event.Event(pygame.QUIT))

            if (changeDirection == 'left' and not direction == 'right'):
                direction = changeDirection
            if (changeDirection == 'right' and not direction == 'left'):
                direction = changeDirection
            if (changeDirection == 'down' and not direction == 'up'):
                direction = changeDirection
            if (changeDirection == 'up' and not direction == 'down'):
                direction = changeDirection

            if direction == 'right':
                snakePosition[0] += 20
            if direction == 'left':
                snakePosition[0] -= 20
            if direction == 'down':
                snakePosition[1] += 20
            if direction == 'up':
                snakePosition[1] -= 20

            snakebody.insert(0, list(snakePosition))
            if (snakePosition[0] == targetPosition[0] and snakePosition[1] == targetPosition[1]):
                targetflag = 0
            else:
                snakebody.pop()
            if targetflag == 0:
                x = random.randrange(1, 40)
                y = random.randrange(1, 30)
                targetPosition = [int(x * 20), int(y * 20)]
                targetflag = 1

            playSurface.fill(self.black)
            for position in snakebody:
                pygame.draw.rect(playSurface, self.white, Rect(position[0], position[1], 20, 20))
                pygame.draw.rect(playSurface, self.green, Rect(targetPosition[0], targetPosition[1], 20, 20))

            pygame.display.flip()
            if (snakePosition[0] > 900 or snakePosition[0] < 0):
                snake.gameover()
            elif (snakePosition[1] > 800 or snakePosition[0] < 0):
                snake.gameover()
            for i in snakebody[1:]:
                if (snakePosition[0] == i[0] and snakePosition[1] == i[1]):
                    snake.gameover()
            clock.tick(5)


snake = Snake()
snake.initialize()