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
47569710
Commit
47569710
authored
1 year ago
by
to
Browse files
Options
Downloads
Patches
Plain Diff
finito
parent
6eb0739b
No related branches found
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
+11
-18
11 additions, 18 deletions
snake/snake_template.py
with
11 additions
and
18 deletions
snake/snake_template.py
+
11
−
18
View file @
47569710
...
...
@@ -15,7 +15,7 @@ bleu = (50, 153, 213)
# Step 1: Defining the window's graphical variables
unite
=
10
largeur_zone
=
60
*
unite
hauteur_zone
=
4
0
*
unite
hauteur_zone
=
3
0
*
unite
couleur_nourriture
=
rouge
couleur_serpent
=
vert
couleur_fond
=
blanc
...
...
@@ -49,33 +49,28 @@ def position_aleatoire() -> tuple:
# Print a message at the top left of the screen in Comic Sans MS, size 20
def
affiche_message
(
message
:
str
):
console
.
blit
(
pygame
.
font
.
SysFont
(
"
comicsansms
"
,
20
).
render
(
message
,
True
,
blanc
),
[
0
,
0
])
def
affiche_message
(
message
:
str
,
couleur
:
tuple
):
console
.
blit
(
pygame
.
font
.
SysFont
(
"
comicsansms
"
,
20
).
render
(
message
,
True
,
couleur
),
[
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
]]
position
=
[
serpent
[
-
1
][
0
]
+
direction_actuelle
[
0
]
*
unite
,
serpent
[
-
1
][
1
]
+
direction_actuelle
[
1
]
*
unite
]
return
position
...
...
@@ -92,11 +87,12 @@ def dans_zone(position):
# One game turn
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
))
affiche_message
(
str
(
len
(
serpent
)
-
1
)
,
noir
)
# Step 6.3: Show the snake
dessiner_liste_carres
(
serpent
,
couleur_serpent
)
...
...
@@ -114,7 +110,9 @@ def tour_jeu(serpent, fin, direction_actuelle, pomme):
position_future
=
nouvelle_position
(
serpent
,
direction_future
)
if
dans_zone
(
position_future
):
serpent
.
append
(
position_future
)
if
serpent
[
-
1
]
==
pomme
:
direction_actuelle
=
direction_future
print
(
f
"
serpent:
{
serpent
}
, tuple(-1)
{
tuple
(
serpent
[
-
1
])
}
, pomme:
{
pomme
}
"
)
if
tuple
(
serpent
[
-
1
])
==
pomme
:
pomme
=
position_aleatoire
()
else
:
serpent
=
serpent
[
1
:]
...
...
@@ -138,17 +136,12 @@ position_pomme = position_aleatoire()
# the clock to make the time move
clock
=
pygame
.
time
.
Clock
()
tick
=
60
print
(
f
"
Initialization done
"
)
tick
=
30
# Step 6.4: As long as the game is not over
while
fin_jeu
is
False
:
print
(
f
"
Looping
"
)
# Step 2.2: colour the background
console
.
fill
(
noir
)
console
.
fill
(
blanc
)
# Update the elements
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)
...
...
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