Compare commits

...

3 Commits

Author SHA1 Message Date
f2bc255a8a Remove test command and add Game loop 2022-08-05 18:44:14 +02:00
63618996e6 add draw level function 2022-08-05 18:43:45 +02:00
1d87e580b4 Add fruit status in return 2022-08-05 18:42:30 +02:00
3 changed files with 87 additions and 35 deletions

View File

@ -5,42 +5,71 @@ import src.level as level
import src.snake as snake
import src.window as window
LEVEL = 1
HEIGH = 10
WIDTH = 20
MATRIX = level.init_level(HEIGH, WIDTH)
HEIGH = 20
WIDTH = 40
def main():
global MATRIX
# Init Matrix/Window
Matrix = level.init_level(HEIGH, WIDTH)
Screen = window.init_game()
# Init Game Variables
clock = pygame.time.Clock()
Running = True
Direction = 'right'
Direction_prev = 'right'
Move_loop = 30
Score = 0
Difficulty = 10
Fruit = False
# Spawn snake head
L_snake = snake.List((5, 5), None)
L_snake = snake.List.List_add(L_snake, (5, 4))
L_snake = snake.List.List_add(L_snake, (5, 3))
L_snake = snake.List.List_add(L_snake, (5, 2))
print(L_snake)
print("Size : ", snake.List.List_size(L_snake))
MATRIX[5][5] = 1
MATRIX[5][4] = 1
MATRIX[5][3] = 1
MATRIX[5][2] = 1
MATRIX[5][6] = 3
level.spawn_apple(MATRIX, HEIGH, WIDTH)
print(MATRIX)
level.spawn_apple(Matrix, HEIGH, WIDTH)
try:
L_snake, MATRIX = snake.Snake_move(MATRIX, L_snake, "up")
except ValueError as Error:
if Error.args[0] == 1:
print("You take a wall !")
elif Error.args[0] == 2:
print("You ate your tale !")
exit()
print(L_snake)
print(MATRIX)
while Running:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
Running = False
if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
Direction = 'right'
if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
Direction = 'left'
if event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
Direction = 'up'
if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:
Direction = 'down'
if Move_loop == 0:
if Direction == 'left' and Direction_prev == 'right' or Direction == 'right' and Direction_prev == 'left' or Direction == 'up' and Direction_prev == 'down' or Direction == 'down' and Direction_prev == 'up':
Direction = Direction_prev
try:
L_snake, Matrix, Fruit = snake.Snake_move(Matrix, L_snake, Direction, HEIGH, WIDTH)
except ValueError as Error:
if Error.args[0] == 1:
print("You take a wall !")
elif Error.args[0] == 2:
print("You ate your tale !")
exit()
if Fruit:
level.spawn_apple(Matrix, HEIGH, WIDTH)
Direction_prev = Direction
Move_loop = 30 - Difficulty*2
Move_loop -= 1
# Draw Scene
Screen = window.draw_level(Screen, Matrix, Difficulty, Score, HEIGH, WIDTH)
pygame.display.flip()
# Print current fps
print(float("{0:.2f}".format(clock.get_fps())), 'fps')
if __name__ == "__main__":

View File

@ -74,4 +74,4 @@ def Snake_move(M, L, dir, heigh, width):
else:
M[Save_Value[0]][Save_Value[1]] = 0
return List_save, M
return List_save, M, Fruit

View File

@ -3,7 +3,30 @@ from pygame.locals import *
def init_game():
pygame.init()
screen = pygame.display.set_mode((1280, 720), pygame.SCALED)
pygame.display.set_caption("Test Window")
screen = pygame.display.set_mode((1000, 525), pygame.SCALED)
pygame.display.set_caption("Snake")
return screen
return screen
def draw_level(S, M, difficulty, score, heigh, width):
WHITE = (255, 255, 255) # AIR
GREEN = (0, 255, 0) # SNAKE
BROWN = (255,248,220) # WALL
RED = (255, 0, 0) # APPLE
Color = WHITE
for j in range(heigh):
for i in range(width):
if M[j][i] == 0:
Color = WHITE
elif M[j][i] == 1:
Color = GREEN
elif M[j][i] == 2:
Color = BROWN
elif M[j][i] == 3:
Color = RED
pygame.draw.rect(S, Color, pygame.Rect(i*25, j*25+25, 25, 25))
return S