Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
Stages_secondes_fork
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Théo Gueuret
Stages_secondes_fork
Commits
6eb0739b
Commit
6eb0739b
authored
1 year ago
by
to
Browse files
Options
Downloads
Patches
Plain Diff
almost done
parent
a2bd215e
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
snake/snake_template.py
+99
-35
99 additions, 35 deletions
snake/snake_template.py
with
99 additions
and
35 deletions
snake/snake_template.py
+
99
−
35
View file @
6eb0739b
#
# RUBIKA - SNAKE GAME
# RUBIKA - SNAKE GAME
#
### IMPORTS ####
#
imports
import
pygame
import
random
#### COLOUR DEFINITION ####
white
=
(
255
,
255
,
255
)
yellow
=
(
255
,
255
,
102
)
black
=
(
0
,
0
,
0
)
red
=
(
213
,
50
,
80
)
green
=
(
0
,
255
,
0
)
blue
=
(
50
,
153
,
213
)
# colors
blanc
=
(
255
,
255
,
255
)
jaune
=
(
255
,
255
,
102
)
noir
=
(
0
,
0
,
0
)
rouge
=
(
213
,
50
,
80
)
vert
=
(
0
,
255
,
0
)
bleu
=
(
50
,
153
,
213
)
# Step 1: Defining the window's graphical variables
unite
=
10
largeur_zone
=
60
hauteur_zone
=
40
couleur_nourriture
=
re
d
couleur_serpent
=
green
couleur_fond
=
white
largeur_zone
=
60
*
unite
hauteur_zone
=
40
*
unite
couleur_nourriture
=
r
oug
e
couleur_serpent
=
vert
couleur_fond
=
blanc
# Step 2.3: Showing a square in the window
def
draw_square
(
x
:
int
,
y
:
int
,
colour
:
tuple
):
pygame
.
draw
.
rect
()
def
dessiner_carre
(
x
:
int
,
y
:
int
,
colour
:
tuple
):
pygame
.
draw
.
rect
(
console
,
colour
,
[
x
,
y
,
unite
,
unite
]
)
# Step 4: Showing multiple squares from a list of positions and a colour
def
dessiner_liste_carres
(
liste_positions
:
list
,
couleur
:
tuple
):
for
position
in
liste_positions
:
dessiner_carre
(
position
[
0
],
position
[
1
],
couleur
)
# Step 3.1: Get a random number between 0 and max (excluded)
def
entier_aleatoire
(
valeur_max
:
int
)
->
int
:
return
random
.
randint
(
0
,
valeur_max
-
1
)
# Step 3.2: Get random coordinates in the zone
def
position_aleatoire
()
->
tuple
:
position_0
=
entier_aleatoire
(
largeur_zone
)
position_1
=
entier_aleatoire
(
hauteur_zone
)
return
position_0
,
position_1
# Print a message at the top left of the screen in Comic Sans MS, size 20
def
print_message
(
message
:
str
)
:
console
.
blit
(
pygame
.
font
.
SysFont
(
"
comicsansms
"
,
20
).
render
(
message
,
True
,
white
),
[
0
,
0
])
def
affiche_message
(
message
:
str
):
console
.
blit
(
pygame
.
font
.
SysFont
(
"
comicsansms
"
,
20
).
render
(
message
,
True
,
blanc
),
[
0
,
0
])
# Step 7: Direction according to the pressed key
def
actualiser_direction
(
keys
:
pygame
.
key
,
direction_actuelle
:
tuple
)
->
tuple
:
if
keys
[
pygame
.
K_UP
]:
print
(
'
key is
"
up
"'
)
direction_future
=
(
0
,
-
1
)
elif
keys
[
pygame
.
K_DOWN
]:
print
(
'
key is
"
down
"'
)
direction_future
=
(
0
,
1
)
elif
keys
[
pygame
.
K_LEFT
]:
print
(
'
key is
"
left
"'
)
direction_future
=
(
-
1
,
0
)
elif
keys
[
pygame
.
K_RIGHT
]:
print
(
'
key is
"
right
"'
)
direction_future
=
(
1
,
0
)
else
:
direction_future
=
direction_actuelle
print
(
f
"
no keys pressed, direction_future:
{
direction_future
}
"
)
return
direction_future
# Step 9: Getting the new position in the current direction
def
nouvelle_position
(
serpent
,
direction_actuelle
):
position
=
[
serpent
[
-
1
][
0
]
+
direction_actuelle
[
0
],
serpent
[
-
1
][
1
]
+
direction_actuelle
[
1
]]
return
position
# Step 10: Checking is the position is in the game area
def
dans_zone
(
position
):
x
=
position
[
0
]
y
=
position
[
1
]
if
x
<
0
or
x
>=
largeur_zone
:
return
False
if
y
<
0
or
y
>=
hauteur_zone
:
return
False
return
True
# One game turn
def
game_turn
(
snake
,
end
,
direction
,
food
):
def
tour_jeu
(
serpent
,
fin
,
direction_actuelle
,
pomme
):
# Step 6.1: Show the food
dessiner_carre
(
pomme
[
0
],
pomme
[
1
],
couleur_nourriture
)
# Step 6.2: Show the score
affiche_message
(
str
(
len
(
serpent
)
-
1
))
# Step 6.3: Show the snake
dessiner_liste_carres
(
serpent
,
couleur_serpent
)
# Update display
pygame
.
display
.
update
()
# Retrieve pressed keys
for
event
in
pygame
.
event
.
get
():
# Step 8.1: × is pressed, exit the game
# Step 8.2: a key is pressed, update direction
print
(
event
)
if
event
.
type
==
pygame
.
QUIT
:
fin
=
True
# Step 11: If the snake is moving
keys
=
pygame
.
key
.
get_pressed
()
direction_future
=
actualiser_direction
(
keys
,
direction_actuelle
)
position_future
=
nouvelle_position
(
serpent
,
direction_future
)
if
dans_zone
(
position_future
):
serpent
.
append
(
position_future
)
if
serpent
[
-
1
]
==
pomme
:
pomme
=
position_aleatoire
()
else
:
serpent
=
serpent
[
1
:]
return
serpent
,
fin
,
direction_actuelle
,
pomme
# Step 12: Is the snake eating?
return
snake
,
end
,
direction
,
food
# Initialisation of the game window ==> do not delete
pygame
.
init
()
# Step 2.1: replace 100,100 by using the correct variables
console
=
pygame
.
display
.
set_mode
((
largeur_zone
,
hauteur_zone
))
# Set window title
pygame
.
display
.
set_caption
(
"
Snake
by Rubika_BI1_Anim3D
"
)
pygame
.
display
.
set_caption
(
"
Snake
eeeee
"
)
# Step 5: Game elements variables
serpent
=
[[
largeur_zone
//
2
,
hauteur_zone
//
2
]]
fin_jeu
=
False
direction_serpent
=
(
0
,
0
)
position_pomme
=
position_aleatoire
()
# the clock to make the time move
clock
=
pygame
.
time
.
Clock
()
tick
=
1
tick
=
60
print
(
f
"
Initialization done
"
)
# Step 6.4: As long as the game is not over
while
False
:
while
fin_jeu
is
False
:
print
(
f
"
Looping
"
)
# Step 2.2: colour the background
console
.
fill
()
console
.
fill
(
noir
)
# Update the elements
snake_positions
,
game_end
,
snake_direction
,
food_position
=
game_turn
(
snake_positions
,
game_end
,
snake_direction
,
food_position
)
serpent
,
fin_jeu
,
direction_serpent
,
position_pomme
=
tour_jeu
(
serpent
,
fin_jeu
,
direction_serpent
,
position_pomme
)
# snake_positions, game_end, snake_direction, food_position = game_turn(snake_positions, game_end, snake_direction, food_position)
clock
.
tick
(
tick
)
# End of game
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment