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
ef845b63
Commit
ef845b63
authored
6 years ago
by
Hameed Andy
Browse files
Options
Downloads
Patches
Plain Diff
Changes made
parent
ac70492a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
BlankProjectTemplate/src/Snake_2.o_Demo.py
+53
-31
53 additions, 31 deletions
BlankProjectTemplate/src/Snake_2.o_Demo.py
with
53 additions
and
31 deletions
BlankProjectTemplate/src/Snake_2.o_Demo.py
+
53
−
31
View file @
ef845b63
#standard set up
## Author: Usman Irfan, Andy Hameed
# This module will be used to control snake body movements and gameplay
import
pygame
from
random
import
randint
pygame
.
init
()
#setting width and height of window
screen_x
=
500
screen_y
=
500
win
=
pygame
.
display
.
set_mode
((
screen_x
,
screen_y
))
#setting width and size of window to screenSize
screenSize
=
500
win
=
pygame
.
display
.
set_mode
((
screenSize
,
screenSize
))
pygame
.
display
.
set_caption
(
"
My Game
"
)
#Define color constants
white
=
(
255
,
255
,
255
)
red
=
(
255
,
0
,
0
)
blue
=
(
0
,
0
,
255
)
black
=
(
0
,
0
,
0
)
#width1, size1 correspond to constant width and size for the food block
#width and size change over time as the snake consumes food
width
,
width1
=
20
,
20
# can replace with size - one number
size
,
size1
=
20
,
20
size
=
20
# One size for all block created for snake
width
=
20
height
=
20
vel
=
10
score
=
0
x
=
randint
(
0
,
screen
_x
-
width
)
y
=
randint
(
0
,
screen
_y
-
height
)
x
=
randint
(
0
,
screen
Size
-
size
)
y
=
randint
(
0
,
screen
Size
-
size
)
speed
=
40
# 0 - (- direction) , 1 - (+ direction)
# 0 gives (- direction)
# 1 gives (+ direction)
direction
=
0
# 0 - x-axis , 1 - y-axis
axis
=
0
...
...
@@ -32,14 +41,14 @@ axis = 0
location
=
[]
food_x
=
randint
(
0
,
screen
_x
-
width
)
food_y
=
randint
(
0
,
screen
_y
-
height
)
food_x
=
randint
(
0
,
screen
Size
-
size
)
food_y
=
randint
(
0
,
screen
Size
-
size
)
location
=
[
food_x
,
food_y
]
run
=
True
#Loop through the events as long as the game is running
run
=
True
while
run
:
pygame
.
time
.
delay
(
speed
)
#create a delay to prevent any unwanted behaviour
...
...
@@ -47,6 +56,7 @@ while run:
#we loop through the list of events that happen by the user's actions
for
event
in
pygame
.
event
.
get
():
#Check if the event is a quit command and quit the game if it is
if
event
.
type
==
pygame
.
QUIT
:
run
=
False
...
...
@@ -67,37 +77,49 @@ while run:
else
:
x
+=
vel
*
direction
#Boundary conditions for snake hitting window edge
if
x
<
0
:
#x = 0
x
=
screen_x
-
height
x
=
screenSize
-
size
if
y
<
0
:
#y = 0
y
=
screen_y
-
width
if
y
>
screen_y
-
width
:
#y = 500 - width
y
=
screenSize
-
size
if
y
>
screenSize
-
size
:
#y = 500 - size
y
=
0
if
x
>
screen
_x
-
height
:
#x = 500 -
height
if
x
>
screen
Size
-
size
:
#x = 500 -
size
x
=
0
#all colors are defined in RGB with Pygame
#all colors are defined in RGB with Pygame
#consumption of food block
if
(
abs
(
x
-
food_x
)
<
15
and
abs
(
y
-
food_y
)
<
15
):
score
+=
1
food_x
=
randint
(
0
,
screen
_x
-
width
)
food_y
=
randint
(
0
,
screen
_y
-
height
)
food_x
=
randint
(
0
,
screen
Size
-
size1
)
food_y
=
randint
(
0
,
screen
Size
-
size1
)
location
=
[
food_x
,
food_y
]
## --------------------------DELETE --------------------------
#extend length or size depending on the direction of the snake
if
(
axis
):
size
+=
20
else
:
size
+=
20
print
(
'
score =
'
,
score
)
##---------------------------DELETE----------------------------
win
.
fill
(
white
)
pygame
.
draw
.
rect
(
win
,(
0
,
0
,
255
),
(
location
[
0
],
location
[
1
],
width
,
height
))
pygame
.
draw
.
rect
(
win
,(
255
,
0
,
0
),
(
x
,
y
,
width
,
height
))
#Draw food item
pygame
.
draw
.
rect
(
win
,
blue
,
(
location
[
0
],
location
[
1
],
size1
,
size1
))
#Draw snake
pygame
.
draw
.
rect
(
win
,
red
,
(
x
,
y
,
size
,
size
))
#we have to update the display to see the drawing of our object. Since it does
#not automatically update
pygame
.
display
.
update
()
...
...
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