Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import sys
- import random
- from pygame.math import Vector2
- class SNAKE:
- def __init__(self):
- self.body = [Vector2(5,10), Vector2(6,10), Vector2(7,10)]
- self.direction = Vector2(-1,0)
- self.new_block = False
- def draw_snake(self):
- for block in self.body:
- x_pos = int(block.x * MAIN.cell_size)
- y_pos = int(block.y * MAIN.cell_size)
- block_rect = pygame.Rect(x_pos, y_pos, MAIN.cell_size, MAIN.cell_size)
- pygame.draw.rect(screen,(0,0,255),block_rect)
- def move_snake(self):
- border_case = False
- if self.body[0].x == 0 and MAIN.allow_borders and self.direction == Vector2(-1,0):
- border_case = True
- if self.new_block:
- self.body.insert(0, Vector2(MAIN.cell_number-1,self.body[0].y))
- self.new_block = False
- else:
- body_copy = self.body[:-1]
- body_copy.insert(0, Vector2(MAIN.cell_number-1,self.body[0].y))
- self.body = body_copy
- if self.body[0].x == MAIN.cell_number and MAIN.allow_borders and self.direction == Vector2(1,0):
- border_case = True
- if self.new_block:
- self.body.insert(0, Vector2(0,self.body[0].y))
- self.new_block = False
- else:
- body_copy = self.body[:-1]
- body_copy.insert(0, Vector2(0,self.body[0].y))
- self.body = body_copy
- if self.body[0].y == 0 and MAIN.allow_borders and self.direction == Vector2(0,-1):
- border_case = True
- if self.new_block:
- self.body.insert(0, Vector2(self.body[0].x,MAIN.cell_number-1))
- self.new_block = False
- else:
- body_copy = self.body[:-1]
- body_copy.insert(0, Vector2(self.body[0].x,MAIN.cell_number-1))
- self.body = body_copy
- if self.body[0].y == MAIN.cell_number and MAIN.allow_borders and self.direction == Vector2(0,1):
- border_case = True
- if self.new_block:
- self.body.insert(0, Vector2(self.body[0].x,0))
- self.new_block = False
- else:
- body_copy = self.body[:-1]
- body_copy.insert(0, Vector2(self.body[0].x,0))
- self.body = body_copy
- if not border_case:
- if self.new_block:
- self.body.insert(0, self.body[0] + self.direction)
- 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.x = random.randint(0,MAIN.cell_number - 1)
- self.y = random.randint(0,MAIN.cell_number - 1)
- self.pos = Vector2(self.x, self.y)
- def draw_fruit(self):
- x_pos = int(self.pos.x * MAIN.cell_size)
- y_pos = int(self.pos.y * MAIN.cell_size)
- fruit_rect = pygame.Rect(x_pos, y_pos, MAIN.cell_size, MAIN.cell_size)
- screen.blit(apple,fruit_rect)
- #pygame.draw.rect(screen,(255,0,114),fruit_rect)
- def randomize(self):
- self.x = random.randint(0,MAIN.cell_number - 1)
- self.y = random.randint(0,MAIN.cell_number - 1)
- self.pos = Vector2(self.x, self.y)
- class MAIN:
- # general game settings
- cell_size = 40
- cell_number = 20
- framerate = 60
- scrsize = cell_size * cell_number
- allow_borders = True
- def __init__(self):
- self.fruit = FRUIT()
- self.snake = SNAKE()
- def update(self):
- self.snake.move_snake()
- self.check_collision()
- self.check_fail()
- def draw_elements(self):
- self.fruit.draw_fruit()
- self.snake.draw_snake()
- def check_collision(self):
- if self.fruit.pos == self.snake.body[0]:
- self.fruit.randomize()
- self.snake.add_block()
- def check_fail(self):
- if not MAIN.allow_borders:
- if not 0 <= self.snake.body[0].x <= MAIN.cell_number-1 or not 0 <= self.snake.body[0].y <= MAIN.cell_number-1:
- self.game_over()
- if self.snake.body[0] in self.snake.body[1:]:
- self.game_over()
- def game_over(self):
- pygame.quit()
- sys.exit()
- pygame.init()
- screen = pygame.display.set_mode((MAIN.scrsize,MAIN.scrsize))
- clock = pygame.time.Clock()
- apple = pygame.image.load("Graphics/apple.png").convert_alpha()
- apple = pygame.transform.scale(apple, (MAIN.cell_size, MAIN.cell_size))
- SCREEN_UPDATE = pygame.USEREVENT
- pygame.time.set_timer(SCREEN_UPDATE, 150)
- main_game = MAIN()
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- sys.exit()
- if event.type == SCREEN_UPDATE:
- main_game.update()
- if event.type == pygame.KEYDOWN:
- if event.key in [pygame.K_UP, pygame.K_w]: # GO UP
- if main_game.snake.direction.y != 1:
- main_game.snake.direction = Vector2(0,-1)
- if event.key in [pygame.K_DOWN, pygame.K_s]: # GO DOWN
- if main_game.snake.direction.y != -1:
- main_game.snake.direction = Vector2(0,1)
- if event.key in [pygame.K_LEFT, pygame.K_a]: # GO LEFT
- if main_game.snake.direction.x != 1:
- main_game.snake.direction = Vector2(-1,0)
- if event.key in [pygame.K_RIGHT, pygame.K_d]: # GO RIGHT
- 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(MAIN.framerate)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement