Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import pygame as pg
- from pygame.math import Vector2
- WIDTH = 500
- FPS = 60
- GRID = 20
- CELLSIZE = WIDTH / GRID
- RIGHT = Vector2(1, 0)
- LEFT = Vector2(-1, 0)
- UP = Vector2(0, -1)
- DOWN = Vector2(0, 1)
- class Game:
- def __init__(self):
- pg.init()
- self.screen = pg.display.set_mode((WIDTH, WIDTH))
- self.clock = pg.time.Clock()
- pg.display.set_caption('Little Python')
- self.apple = Apple(self.screen)
- self.snake = Snake(self.screen)
- self.snake_timer = 300
- self.snake_clk = 0
- self.score = 0
- self.hi_score = 0
- self.gameover = False
- def restart(self):
- self.snake = Snake(self.screen)
- self.score = 0
- self.gameover = False
- def draw(self):
- if self.gameover:
- self.display_gameover()
- return
- self.screen.fill([100, 50, 150])
- # [pg.draw.rect(self.screen, 'black', (x * CELLSIZE, y * CELLSIZE, CELLSIZE, CELLSIZE), 1) for y in range(GRID) for x in range(GRID)]
- self.apple.draw()
- self.snake.draw()
- self.write(f'Score: {self.score}', 30, 20, 5, 'white')
- self.write(f'Best: {self.hi_score}', 30, 350, 5, 'white')
- def write(self, text, size, x, y, color):
- font = pg.font.SysFont('comicsansms', size)
- message = font.render(text, True, color)
- text_box = message.get_rect()
- text_box.topleft = x, y
- self.screen.blit(message, text_box)
- def display_gameover(self):
- self.screen.fill([200, 50, 40])
- self.write('GAME OVER!', 50, 100, 30, 'white')
- self.write(f'Score: {self.score}', 35, 180, 200, 'white')
- self.write(f'Best: {self.hi_score}', 35, 180, 250, 'white')
- self.write('press SPACE to play again', 20, 120, 350, 'white')
- def control(self):
- keys = pg.key.get_pressed()
- if keys[pg.K_UP]:
- self.snake.change_direction(UP)
- elif keys[pg.K_DOWN]:
- self.snake.change_direction(DOWN)
- elif keys[pg.K_LEFT]:
- self.snake.change_direction(LEFT)
- elif keys[pg.K_RIGHT]:
- self.snake.change_direction(RIGHT)
- if keys[pg.K_SPACE] and self.gameover:
- self.restart()
- def check_borders(self):
- snake_head = self.snake.body[0]
- if snake_head.x >= GRID or snake_head.x < 0:
- return True
- if snake_head.y >= GRID or snake_head.y < 0:
- return True
- return False
- def check_collisions(self):
- snake_head = self.snake.body[0]
- if snake_head.x == self.apple.x and snake_head.y == self.apple.y:
- self.snake.eat()
- self.apple.move_random()
- self.score += 1
- if self.score > self.hi_score:
- self.hi_score = self.score
- if self.check_borders() or snake_head in self.snake.body[1:]:
- self.gameover = True
- def update(self):
- pg.display.update()
- self.clock.tick(FPS)
- if self.gameover:
- return
- now = pg.time.get_ticks()
- if now - self.snake_clk > self.snake_timer:
- self.snake_clk = now
- self.snake.move()
- self.check_collisions()
- class Snake:
- def __init__(self, screen):
- self.screen = screen
- self.body = [Vector2(10, 10), Vector2(9,10)]
- self.color = [10, 230, 150]
- self.direction = RIGHT
- def _get_segment(self, index):
- rect = pg.Rect(self.body[index].x*CELLSIZE, self.body[index].y*CELLSIZE, CELLSIZE, CELLSIZE)
- return rect
- def draw(self):
- for i in range(len(self.body)):
- rect = self._get_segment(i)
- pg.draw.rect(self.screen, self.color, rect)
- def eat(self):
- self.body.append(self.body[-1].copy())
- def move(self):
- for i in range(len(self.body)-1, 0, -1):
- self.body[i] = self.body[i-1].copy()
- self.body[0] += self.direction
- def change_direction(self, new_dir):
- if new_dir.x == self.direction.x or new_dir.y == self.direction.y:
- return
- self.direction = new_dir
- class Apple:
- def __init__(self, screen):
- self.screen = screen
- self.x = 0
- self.y = 0
- self.move_random()
- def draw(self):
- rect = pg.Rect(self.x * CELLSIZE, self.y * CELLSIZE, CELLSIZE, CELLSIZE)
- pg.draw.rect(self.screen, 'red', rect)
- def move_random(self):
- self.x = random.randrange(GRID)
- self.y = random.randrange(GRID)
- def main():
- game = Game()
- running = True
- while running:
- for event in pg.event.get():
- if event.type == pg.QUIT:
- running = False
- game.control()
- game.draw()
- game.update()
- pg.quit()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment