Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
se3xa3
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hameed Andy
se3xa3
Commits
230660c7
Commit
230660c7
authored
6 years ago
by
Hameed Andy
Browse files
Options
Downloads
Patches
Plain Diff
Dying part completed
parent
99fc66e6
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
BlankProjectTemplate/src/Gameplay.py
+47
-30
47 additions, 30 deletions
BlankProjectTemplate/src/Gameplay.py
BlankProjectTemplate/src/Snake.py
+5
-1
5 additions, 1 deletion
BlankProjectTemplate/src/Snake.py
with
52 additions
and
31 deletions
BlankProjectTemplate/src/Gameplay.py
+
47
−
30
View file @
230660c7
...
...
@@ -7,23 +7,17 @@ from random import randint
from
Snake
import
*
from
Food
import
*
global
a
a
=
0
#defining a list to keep track of snake's head
snake_loc
=
[]
#variable to increment snake's length, initially it would be 1
snake_length
=
1
speed
=
3
0
speed
=
7
0
# 0 gives (- direction)
# 1 gives (+ direction)
direction
=
0
direction
=
1
# 0 - x-axis , 1 - y-axis
axis
=
0
...
...
@@ -38,7 +32,7 @@ food_location = [food_x, food_y]
##initialize snake and draw snake body somewhere on the screen
print
(
x
/
size
,
y
/
size
)
snake
=
Snake
(
size
,
0
,
20
,
0
)
snake
=
Snake
(
size
,
0
,
20
,
1
)
pygame
.
draw
.
rect
(
win
,
red
,
[
x
,
y
,
size
,
size
])
food
=
Food
(
size
)
...
...
@@ -58,13 +52,25 @@ while run:
#Each event type has an integer assigned to it. KEYDOWN has the code 2
if
event
.
type
==
pygame
.
KEYDOWN
:
if
(
event
.
key
==
pygame
.
K_LEFT
):
snake
.
axis
=
0
;
snake
.
direct
=
-
1
# if snake is moving up or down, turn left, otherwise don't turn
if
(
snake
.
axis
):
snake
.
direct
=
-
1
snake
.
axis
=
0
if
(
event
.
key
==
pygame
.
K_RIGHT
):
snake
.
axis
=
0
;
snake
.
direct
=
1
#if snake is moving up or down turn right, otherwise dont turn
if
(
snake
.
axis
):
snake
.
direct
=
1
snake
.
axis
=
0
if
(
event
.
key
==
pygame
.
K_UP
):
snake
.
axis
=
1
;
snake
.
direct
=
-
1
#if snake is moving left or right turn up, otherwise dont turn
if
(
not
snake
.
axis
):
snake
.
direct
=
-
1
snake
.
axis
=
1
if
(
event
.
key
==
pygame
.
K_DOWN
):
snake
.
axis
=
1
;
snake
.
direct
=
1
#if snake is moving left or right turn down, otherwise dont turn
if
(
not
snake
.
axis
):
snake
.
direct
=
1
snake
.
axis
=
1
## print("axis is", "y" if snake.axis else "x")
## print("direction is", snake.direct)
#Snake moving depending on axis and direction
if
(
snake
.
axis
):
...
...
@@ -72,37 +78,48 @@ while run:
else
:
x
+=
(
size
)
*
snake
.
direct
print
(
"
x:
"
,
x
,
"
y:
"
,
y
)
print
(
"
food x:
"
,
food_location
[
0
],
"
food y:
"
,
food_location
[
1
])
##
print("x: ",x,"y: ",y)
##
print("food x: ",food_location[0],"food y: ",food_location[1])
#Boundary conditions for snake hitting window edge
if
x
<
0
:
#x = 0
x
=
screenSize
-
size
if
y
<
0
:
#y = 0
y
=
screenSize
-
size
if
y
>
screenSize
-
size
:
#y = 500 - size
y
=
0
if
x
>
screenSize
-
size
:
#x = 500 - size
x
=
0
if
(
x
<
0
or
y
<
0
or
y
>
screenSize
-
size
or
x
>
screenSize
-
size
):
pygame
.
quit
()
#for now, quit the game when snake hits boundary
## snake.die()
##
## if x < 0:
## #x = 0
## x = screenSize - size
## if y < 0:
## #y = 0
## y = screenSize - size
## if y > screenSize - size:
## #y = 500 - size
## y = 0
## if x > screenSize - size:
## #x = 500 - size
## x = 0
if
(
abs
(
x
-
food_location
[
0
])
<
15
and
abs
(
y
-
food_location
[
1
])
<
15
):
score
+=
10
print
(
score
)
snake_length
+=
5
win
.
fill
(
white
)
#consumption of food block
food
.
redraw_food
(
x
,
y
,
food_location
,
screenSize
)
if
([
x
,
y
]
in
snake_loc
)
and
snake_length
>
1
:
pygame
.
time
.
delay
(
1000
)
pygame
.
quit
()
#quit for now, but should return to main menu
snake_head
=
[]
snake_head
.
append
(
x
)
snake_head
.
append
(
y
)
...
...
This diff is collapsed.
Click to expand it.
BlankProjectTemplate/src/Snake.py
+
5
−
1
View file @
230660c7
...
...
@@ -27,6 +27,10 @@ class Snake():
for
x
,
y
in
snake_loc
:
pygame
.
draw
.
rect
(
win
,
red
,
[
x
,
y
,
self
.
size
,
self
.
size
])
def
die
():
#---------------------INSERT CODE -----------------
# create some pop up message saying game over
return
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