mirror of
https://github.com/d3vyce/Python-Game.git
synced 2025-07-02 01:48:19 +02:00
Compare commits
15 Commits
efb27a0a92
...
master
Author | SHA1 | Date | |
---|---|---|---|
43b88a27ca | |||
4a8d02efd2 | |||
232bb211ee | |||
d1f6110b10 | |||
cd50022bef | |||
aa6c9e907f | |||
142a684fd6 | |||
9a681d7618 | |||
b98cff34e3 | |||
bd8d9fc2fb | |||
e46fe34ea0 | |||
fdeff9e17f | |||
f2bc255a8a | |||
63618996e6 | |||
1d87e580b4 |
1
Chess/README.md
Normal file
1
Chess/README.md
Normal file
@ -0,0 +1 @@
|
||||
|
14
README.md
14
README.md
@ -1,3 +1,13 @@
|
||||
Python Game
|
||||
# Python Game
|
||||
Here are the different games I've made so far in python.
|
||||
|
||||
- Snake [Current Project]
|
||||
- Snake
|
||||
<p>
|
||||
<img src="Snake/img/Snake.png" width="500" title="hover text">
|
||||
</p>
|
||||
|
||||
- Chess [WIP]
|
||||
|
||||
- Sudoku [SOON]
|
||||
|
||||
- Pandemic [SOON]
|
||||
|
@ -1,4 +1,6 @@
|
||||
Snake in Python using pygame
|
||||
# Snake in Python using pygame
|
||||
|
||||

|
||||
|
||||
## Install
|
||||
```
|
||||
@ -11,4 +13,4 @@ pip install -r .\requirements.txt
|
||||
## Start Application
|
||||
```
|
||||
python .\main.py
|
||||
```
|
||||
```
|
||||
|
BIN
Snake/img/Snake.png
Normal file
BIN
Snake/img/Snake.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 12 KiB |
@ -5,42 +5,74 @@ 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 = 10
|
||||
Score = 0
|
||||
Difficulty = 1
|
||||
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)
|
||||
# Spawn first apple
|
||||
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, Score = snake.Snake_move(Matrix, L_snake, Direction, Score, 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
|
||||
if(Score > 1000):
|
||||
Difficulty = int(Score/1000) + 1
|
||||
Move_loop = 11 - Difficulty
|
||||
|
||||
Move_loop -= 1
|
||||
|
||||
# Draw Scene
|
||||
Screen = window.draw_level(Screen, Matrix, Difficulty, Score, int(clock.get_fps()), HEIGH, WIDTH)
|
||||
pygame.display.flip()
|
||||
|
||||
# Print current fps
|
||||
print(float("{0:.2f}".format(clock.get_fps())), 'fps')
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
@ -29,7 +29,7 @@ class List:
|
||||
|
||||
return i
|
||||
|
||||
def Snake_move(M, L, dir, heigh, width):
|
||||
def Snake_move(M, L, dir, score, heigh, width):
|
||||
Fruit = False
|
||||
List_save = L
|
||||
Save_Value = L.val
|
||||
@ -71,7 +71,8 @@ def Snake_move(M, L, dir, heigh, width):
|
||||
# If snake eat fruit -> spawn new segment at the end of the snake
|
||||
if Fruit:
|
||||
List_save = List.List_add(List_save, Save_Value)
|
||||
score += 50
|
||||
else:
|
||||
M[Save_Value[0]][Save_Value[1]] = 0
|
||||
|
||||
return List_save, M
|
||||
return List_save, M, Fruit, score
|
@ -3,7 +3,48 @@ from pygame.locals import *
|
||||
|
||||
def init_game():
|
||||
pygame.init()
|
||||
screen = pygame.display.set_mode((1280, 720), pygame.SCALED)
|
||||
pygame.display.set_caption("Test Window")
|
||||
pygame.font.init()
|
||||
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, fps, heigh, width):
|
||||
WHITE = (255, 255, 255) # AIR
|
||||
GREEN = (0, 255, 0) # SNAKE
|
||||
BROWN = (255,248,220) # WALL
|
||||
RED = (255, 0, 0) # APPLE
|
||||
|
||||
# Reset screen
|
||||
pygame.draw.rect(S, (0, 0, 0), pygame.Rect(0, 0, 1000, 525))
|
||||
|
||||
my_font = pygame.font.SysFont('Comic Sans MS', 18)
|
||||
|
||||
# Add FPS counter
|
||||
text_fps = my_font.render(str(fps) + ' fps', False, (255, 255, 255))
|
||||
S.blit(text_fps, (10, 0))
|
||||
|
||||
# Add Score
|
||||
text_score = my_font.render('Score : '+ str(score), False, (255, 255, 255))
|
||||
S.blit(text_score, (400, 0))
|
||||
|
||||
# Add Diffucilty
|
||||
text_difficulty = my_font.render('Difficulty : '+ str(difficulty), False, (255, 255, 255))
|
||||
S.blit(text_difficulty, (850, 0))
|
||||
|
||||
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
|
Reference in New Issue
Block a user