pygame 贪吃蛇

import pygame, sys
import random
from pygame.math import Vector2
pygame.init()

class SNAKE:
    def __init__(self):
        self.body = [Vector2(5,10),Vector2(4,10),Vector2(3,10)]
        self.direction = Vector2(1,0)
        self.new_block = False

    def draw_snake(self):
        for block in self.body:
            x_pos = int(block.x * cell_size)
            y_pos = int(block.y * cell_size)
            block_rect = pygame.Rect(x_pos,y_pos,cell_size,cell_size)
            pygame.draw.rect(screen,(255,106,106),block_rect)

    def move_snake(self):
        if self.new_block:
            body_copy = self.body[:]
            body_copy.insert(0, body_copy[0] + self.direction)
            self.body = body_copy[:]
            self.new_block = False
        else:
            body_copy = self.body[:-1]
            body_copy.insert(0,body_copy[0] + self.direction)
            self.body = body_copy[:]

    def add_block(self):
        self.new_block = True

class FRUIT:
    def __init__(self):
        self.randommize()

    def draw_fruit(self):
        fruit_rect = pygame.Rect(int(self.pos.x * cell_size),int(self.pos.y * cell_size),cell_size,cell_size)
        screen.blit(apple,fruit_rect)

    def randommize(self):
        self.x = random.randint(0, cell_number - 1)
        self.y = random.randint(0, cell_number - 1)
        self.pos = Vector2(self.x, self.y)

class MAIN:
    def __init__(self):
        self.snake = SNAKE()
        self.fruit = FRUIT()

    def update(self):
        self.snake.move_snake()
        self.check_collision()
        self.check_fail()

    def draw_elements(self):
        self.draw_grass()
        self.draw_score()
        self.fruit.draw_fruit()
        self.snake.draw_snake()

    def check_collision(self):
        if self.fruit.pos == self.snake.body[0]:
            self.fruit.randommize()
            self.snake.add_block()

    def check_fail(self):
        if not ((0 <= self.snake.body[0].x <= cell_number) or
           not (0 <= self.snake.body[0].y <= cell_number)):
            self.game_over()

        for block in self.snake.body[1:]:
            if block == self.snake.body[0]:
                 self.game_over()

    def draw_grass(self):
        grass_color = (167,199,61)
        for cl in range(cell_number):
            c_n = 0 if (cl == 0) or (cl % 2 == 0) else 1
            for col in range(c_n,cell_number,2):
                grass_rect = pygame.Rect(col * cell_size,cl * cell_size,cell_size,cell_size)
                pygame.draw.rect(screen,grass_color,grass_rect)

    def draw_score(self):
        score_num = int(len(self.snake.body) - 3)
        score_surface = game_font.render('SCORE '+str(score_num),True,(85,107,47))
        score_x = int(3 * cell_size)
        score_y = int(1 * cell_size)
        score_rect = score_surface.get_rect(center = (score_x,score_y))
        screen.blit(score_surface,score_rect)

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

cell_size = 30
cell_number = 20
screen = pygame.display.set_mode((cell_size * cell_number,cell_size * cell_number))
clock = pygame.time.Clock()
apple = pygame.image.load('apple.jpg').convert_alpha()
game_font = pygame.font.Font(None,25)

main_game = MAIN()

SCREEN_UPDATE = pygame.USEREVENT
pygame.time.set_timer(SCREEN_UPDATE,100)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            main_game.game_over()
        if event.type == SCREEN_UPDATE:
            main_game.update()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                if main_game.snake.direction.y != 1:
                    main_game.snake.direction = Vector2(0,-1)
            if event.key == pygame.K_RIGHT:
                if main_game.snake.direction.x != -1:
                    main_game.snake.direction = Vector2(1,0)
            if event.key == pygame.K_DOWN:
                if main_game.snake.direction.y != -1:
                    main_game.snake.direction = Vector2(0,1)
            if event.key == pygame.K_LEFT:
                if main_game.snake.direction.x != 1:
                    main_game.snake.direction = Vector2(-1,0)
    screen.fill((175,215,70))
    main_game.draw_elements()
    pygame.display.update()
    clock.tick(60)

楼主没打注释,请谅解

上一篇:十八、绘制游戏背景图片


下一篇:解决pygame的绘制的小圆点pyinstaller打包后变成菱形(以及分享一个打包小坑)