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
e5af3971
Commit
e5af3971
authored
6 years ago
by
Hameed Andy
Browse files
Options
Downloads
Patches
Plain Diff
First Draft of Class implementation
parent
6eff3f9a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
BlankProjectTemplate/src/Snake.py
+41
-0
41 additions, 0 deletions
BlankProjectTemplate/src/Snake.py
BlankProjectTemplate/src/Snake_2.o_Demo.py
+20
-30
20 additions, 30 deletions
BlankProjectTemplate/src/Snake_2.o_Demo.py
BlankProjectTemplate/src/init.py
+18
-0
18 additions, 0 deletions
BlankProjectTemplate/src/init.py
with
79 additions
and
30 deletions
BlankProjectTemplate/src/Snake.py
0 → 100644
+
41
−
0
View file @
e5af3971
## @file Snake.py
# @author Andy Hameed
# @brief implements an abstract data type for a snake
# @date 21/02/2018
from
init
import
*
## @brief An Abstract Data type representing a snake character object
class
Snake
():
## @brief Snake constructor
# @details Initializes a Snake object with its initial attributes
# @param blockSize the width and height of the square block representing the snake
# @param direct The direction of the snake's movement
# @param speed The initial speed of the snake's movement
def
__init__
(
self
,
blockSize
,
direct
,
speed
,
axis
):
self
.
speed
=
speed
self
.
direct
=
direct
self
.
size
=
blockSize
self
.
axis
=
axis
## @brief Draw method uses pygame to draw the snake object
# @param x The x-coordinate where the block should be drawn
# @param y The y-coordinate where the block should be drawn
def
draw
(
self
,
x
,
y
):
pygame
.
draw
.
rect
(
win
,
red
,
(
x
,
y
,
self
.
size
,
self
.
size
))
## @brief Snake consumes a food block
## def consume():
##
##class Block():
##
## def __init__(self, width, height):
## self.w = width
## swlf.h = height
This diff is collapsed.
Click to expand it.
BlankProjectTemplate/src/Snake_2.o_Demo.py
+
20
−
30
View file @
e5af3971
...
@@ -3,21 +3,10 @@
...
@@ -3,21 +3,10 @@
# @brief implements gameplay and connects the different components of the game
# @brief implements gameplay and connects the different components of the game
# @date 11/09/2018
# @date 11/09/2018
import
pygame
from
random
import
randint
#initializing PyGame and setting game window dimensions
pygame
.
init
()
screenSize
=
500
win
=
pygame
.
display
.
set_mode
((
screenSize
,
screenSize
))
pygame
.
display
.
set_caption
(
"
Snake 2.o
"
)
#Define color constants
white
=
(
255
,
255
,
255
)
red
=
(
255
,
0
,
0
)
blue
=
(
0
,
0
,
255
)
black
=
(
0
,
0
,
0
)
from
random
import
randint
from
Snake
import
*
from
init
import
*
# One size for all blocks created for snake
# One size for all blocks created for snake
size
=
20
size
=
20
...
@@ -27,8 +16,8 @@ vel = 10
...
@@ -27,8 +16,8 @@ vel = 10
score
=
0
score
=
0
#initial x and y coordinates of the snake
#initial x and y coordinates of the snake
x
_init
=
randint
(
0
,
screenSize
-
size
)
x
=
randint
(
0
,
screenSize
-
size
)
y
_init
=
randint
(
0
,
screenSize
-
size
)
y
=
randint
(
0
,
screenSize
-
size
)
speed
=
40
speed
=
40
...
@@ -44,7 +33,8 @@ food_x = randint(0,screenSize - size)
...
@@ -44,7 +33,8 @@ food_x = randint(0,screenSize - size)
food_y
=
randint
(
0
,
screenSize
-
size
)
food_y
=
randint
(
0
,
screenSize
-
size
)
location
=
[
food_x
,
food_y
]
location
=
[
food_x
,
food_y
]
##initialize snake
snake
=
Snake
(
size
,
0
,
20
,
0
)
#Loop through the events as long as the game is running
#Loop through the events as long as the game is running
run
=
True
run
=
True
...
@@ -62,19 +52,19 @@ while run:
...
@@ -62,19 +52,19 @@ while run:
#Each event type has an integer assigned to it. KEYDOWN has the code 2
#Each event type has an integer assigned to it. KEYDOWN has the code 2
if
event
.
type
==
pygame
.
KEYDOWN
:
if
event
.
type
==
pygame
.
KEYDOWN
:
if
(
event
.
key
==
pygame
.
K_LEFT
):
if
(
event
.
key
==
pygame
.
K_LEFT
):
axis
=
0
;
direct
ion
=
-
1
snake
.
axis
=
0
;
snake
.
direct
=
-
1
if
(
event
.
key
==
pygame
.
K_RIGHT
):
if
(
event
.
key
==
pygame
.
K_RIGHT
):
axis
=
0
;
direct
ion
=
1
snake
.
axis
=
0
;
snake
.
direct
=
1
if
(
event
.
key
==
pygame
.
K_UP
):
if
(
event
.
key
==
pygame
.
K_UP
):
axis
=
1
;
direct
ion
=
-
1
snake
.
axis
=
1
;
snake
.
direct
=
-
1
if
(
event
.
key
==
pygame
.
K_DOWN
):
if
(
event
.
key
==
pygame
.
K_DOWN
):
axis
=
1
;
direct
ion
=
1
snake
.
axis
=
1
;
snake
.
direct
=
1
#Snake moving depending on axis and direction
#Snake moving depending on axis and direction
if
(
axis
):
if
(
snake
.
axis
):
y
+=
vel
*
direct
ion
y
+=
vel
*
snake
.
direct
else
:
else
:
x
+=
vel
*
direct
ion
x
+=
vel
*
snake
.
direct
#Boundary conditions for snake hitting window edge
#Boundary conditions for snake hitting window edge
if
x
<
0
:
if
x
<
0
:
...
@@ -98,16 +88,16 @@ while run:
...
@@ -98,16 +88,16 @@ while run:
food_x
=
randint
(
0
,
screenSize
-
size
)
food_x
=
randint
(
0
,
screenSize
-
size
)
food_y
=
randint
(
0
,
screenSize
-
size
)
food_y
=
randint
(
0
,
screenSize
-
size
)
location
=
[
food_x
,
food_y
]
location
=
[
food_x
,
food_y
]
print
(
'
score =
'
,
score
)
## --------------------------DELETE --------------------------
## --------------------------DELETE --------------------------
#extend length or size depending on the direction of the snake
#extend length or size depending on the direction of the snake
if
(
axis
):
##
if (axis):
size
+=
20
##
size += 20
else
:
##
else:
size
+=
20
##
size += 20
##---------------------------DELETE----------------------------
##---------------------------DELETE----------------------------
print
(
'
score =
'
,
score
)
...
@@ -117,7 +107,7 @@ while run:
...
@@ -117,7 +107,7 @@ while run:
pygame
.
draw
.
rect
(
win
,
blue
,
(
location
[
0
],
location
[
1
],
size
,
size
))
pygame
.
draw
.
rect
(
win
,
blue
,
(
location
[
0
],
location
[
1
],
size
,
size
))
#Draw snake
#Draw snake
pygam
e
.
draw
.
rect
(
win
,
red
,
(
x
,
y
,
size
,
size
)
)
snak
e
.
draw
(
x
,
y
)
#we have to update the display to see the drawing of our object. Since it does
#we have to update the display to see the drawing of our object. Since it does
...
...
This diff is collapsed.
Click to expand it.
BlankProjectTemplate/src/init.py
0 → 100644
+
18
−
0
View file @
e5af3971
## @file Colors.py
# @author Andy Hameed
# @brief Define color constants
# @date 11/09/2018
import
pygame
#initializing PyGame and setting game window dimensions
pygame
.
init
()
screenSize
=
500
win
=
pygame
.
display
.
set_mode
((
screenSize
,
screenSize
))
pygame
.
display
.
set_caption
(
"
Snake 2.o
"
)
#Define color constants
white
=
(
255
,
255
,
255
)
red
=
(
255
,
0
,
0
)
blue
=
(
0
,
0
,
255
)
black
=
(
0
,
0
,
0
)
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