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
38fbf5da
Commit
38fbf5da
authored
6 years ago
by
Usman Irfan
Browse files
Options
Downloads
Patches
Plain Diff
Added functions
parent
24b05534
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
BlankProjectTemplate/src/Gameplay.py
+135
-134
135 additions, 134 deletions
BlankProjectTemplate/src/Gameplay.py
with
135 additions
and
134 deletions
BlankProjectTemplate/src/Gameplay.py
+
135
−
134
View file @
38fbf5da
...
...
@@ -7,144 +7,145 @@ from random import randint
from
Snake
import
*
from
Food
import
*
#defining a list to update snanke's length
snake_loc
=
[]
#variable to increment snake's length, initially it would be 1
snake_length
=
1
speed
=
70
# 0 gives (- direction)
# 1 gives (+ direction)
direction
=
1
# 0 - x-axis , 1 - y-axis
axis
=
0
score
=
0
# parameters for initializing food on the screen
food_location
=
[]
food_x
=
randint
(
0
,
grid_length
-
1
)
*
size
food_y
=
randint
(
0
,
grid_length
-
1
)
*
size
food_location
=
[
food_x
,
food_y
]
##initialize snake and draw snake body somewhere on the screen
snake
=
Snake
(
size
,
0
,
20
,
1
)
pygame
.
draw
.
rect
(
win
,
red
,
[
x
,
y
,
size
,
size
])
food
=
Food
(
size
)
#Loop through the events as long as the game is running
run
=
True
while
run
:
#delay controls part of speed
pygame
.
time
.
delay
(
speed
)
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
pygame
.
QUIT
:
run
=
False
#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
):
# 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
):
#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
):
#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
):
#if snake is moving left or right turn down, otherwise dont turn
if
(
not
snake
.
axis
):
snake
.
direct
=
1
snake
.
axis
=
1
#Snake moving depending on axis and direction
if
(
snake
.
axis
):
y
+=
(
size
)
*
snake
.
direct
else
:
x
+=
(
size
)
*
snake
.
direct
#Boundary conditions for snake hitting window edge
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
def
Gameplay
(
speed
,
colour
,
backgroundColour
):
#defining a list to update snanke's length
snake_loc
=
[]
#variable to increment snake's length, initially it would be 1
snake_length
=
1
speed
=
70
## snake.die()
# 0 gives (- direction)
# 1 gives (+ direction)
direction
=
1
# 0 - x-axis , 1 - y-axis
axis
=
0
#---------------------FOR WRAPING SNAKE AROUND WINDOW---------------------
## 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
score
=
0
#-------------------------------------------------
# parameters for initializing food on the screen
food_location
=
[]
food_x
=
randint
(
0
,
grid_length
-
1
)
*
size
food_y
=
randint
(
0
,
grid_length
-
1
)
*
size
food_location
=
[
food_x
,
food_y
]
if
(
abs
(
x
-
food_location
[
0
])
<
15
and
abs
(
y
-
food_location
[
1
])
<
15
):
score
+=
10
##initialize snake and draw snake body somewhere on the screen
#increment the length by 3 unit every time
snake_length
+=
3
snake
=
Snake
(
size
,
0
,
20
,
1
)
pygame
.
draw
.
rect
(
win
,
colour
,
[
x
,
y
,
size
,
size
])
food
=
Food
(
size
)
#Loop through the events as long as the game is running
run
=
True
while
run
:
#delay controls part of speed
pygame
.
time
.
delay
(
speed
)
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
pygame
.
QUIT
:
run
=
False
#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
):
# 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
):
#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
):
#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
):
#if snake is moving left or right turn down, otherwise dont turn
if
(
not
snake
.
axis
):
snake
.
direct
=
1
snake
.
axis
=
1
#Snake moving depending on axis and direction
if
(
snake
.
axis
):
y
+=
(
size
)
*
snake
.
direct
else
:
x
+=
(
size
)
*
snake
.
direct
#Boundary conditions for snake hitting window edge
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()
#---------------------FOR WRAPING SNAKE AROUND WINDOW---------------------
win
.
fill
(
white
)
font
=
pygame
.
font
.
SysFont
(
"
times
"
,
30
)
text
=
font
.
render
(
"
Score =
"
+
str
(
score
),
True
,[
0
,
0
,
0
])
win
.
blit
(
text
,(
0
,
0
))
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
)
snake_loc
.
append
(
snake_head
)
#function to print
#consumption of food block
food
.
redraw_food
(
x
,
y
,
food_location
,
screenSize
,
snake_loc
)
snake_blocks
=
len
(
snake_loc
)
#Draw food item
food
.
draw_food
(
food_location
)
if
snake_blocks
>
snake_length
:
#keep updating the new block
del
snake_loc
[
0
]
'''
Logic for updating the length is taken from:
CodeWithHarry, CodeWithHarry. “Snakes Game: Length Increment Logic - Python Game Development Using Pygame In Hindi #17.”
YouTube, YouTube, 2 Oct. 2018,
www.youtube.com/watch?v=mkGJb0W03DM&index=17&list=PLu0W_9lII9ailUQcxEPZrWgDoL36BtPYb.
'''
#Draw snake
snake
.
draw
(
snake_loc
)
#update display
pygame
.
display
.
update
()
pygame
.
quit
()
## 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
#increment the length by 3 unit every time
snake_length
+=
3
win
.
fill
(
backgroundColour
)
font
=
pygame
.
font
.
SysFont
(
"
times
"
,
30
)
text
=
font
.
render
(
"
Score =
"
+
str
(
score
),
True
,[
0
,
0
,
0
])
win
.
blit
(
text
,(
0
,
0
))
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
)
snake_loc
.
append
(
snake_head
)
#function to print
#consumption of food block
food
.
redraw_food
(
x
,
y
,
food_location
,
screenSize
,
snake_loc
)
snake_blocks
=
len
(
snake_loc
)
#Draw food item
food
.
draw_food
(
food_location
)
if
snake_blocks
>
snake_length
:
#keep updating the new block
del
snake_loc
[
0
]
'''
Logic for updating the length is taken from:
CodeWithHarry, CodeWithHarry. “Snakes Game: Length Increment Logic - Python Game Development Using Pygame In Hindi #17.”
YouTube, YouTube, 2 Oct. 2018,
www.youtube.com/watch?v=mkGJb0W03DM&index=17&list=PLu0W_9lII9ailUQcxEPZrWgDoL36BtPYb.
'''
#Draw snake
snake
.
draw
(
snake_loc
)
#update display
pygame
.
display
.
update
()
pygame
.
quit
()
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