0% found this document useful (0 votes)
8 views4 pages

SNAKEGAME

Uploaded by

mb0ye24.ye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

SNAKEGAME

Uploaded by

mb0ye24.ye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

import pygame,sys,random

from pygame.math import Vector2

pygame.init()

cell_size= 30
cell_number =20
screen = pygame.display.set_mode((cell_number*cell_size,cell_number*cell_size))
clock= pygame.time.Clock()

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

def dessine_fruit(self):
fruit_rect =
pygame.Rect(int(self.pos.x*cell_size),int(self.pos.y*cell_size),cell_size,cell_size)
pygame.draw.rect(screen,'red',fruit_rect)
def radom_lol(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 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 dessine_serpent(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,'green',block_rect)

def mouvement_serpent(self):
if self.new_block == True:
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 MAIN:
def __init__(self):
self.snake= SNAKE()
self.fruit= FRUIT()

def update(self):
self.snake.mouvement_serpent()
self.regarde_collision()
self.verifie_collision()

def dessine_elements(self):
self.fruit.dessine_fruit()
self.snake.dessine_serpent()
def regarde_collision(self):
if self.fruit.pos == self.snake.body[0]:
self.fruit.radom_lol()
self.snake.add_block()

def verifie_collision(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 game_over(self) :
pygame.quit()
sys.exit()

main_game= MAIN()

SREEN_UPDATE = pygame.USEREVENT
pygame.time.set_timer(SREEN_UPDATE,150)

while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type ==SREEN_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_LEFT:
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_RIGHT:
if main_game.snake.direction.x != -1:
main_game.snake.direction = Vector2(1,0)
screen.fill((175,215,70))
main_game.dessine_elements()
pygame.display.update()
clock.tick(60)

You might also like