Uploaded by Praveen Krishna

Snake Game

advertisement
Build a game:
Snake
Brought to you by Project Glitch
Supported by MacPherson CC
and MacPherson Youth Network
Intro :)
Download PyCharm IDE
IDE : Integrated Development Environment
www.tinyurl.com/pycharm4glitch
TABLE OF CONTENTS
CONCEPT
METHODS
How are we going
to make this game?
The meat of our game
INITIALIZE
EXECUTION
GUI
Starting Variables
Function calls
Testing our game
“Can I remove this slide
HAHA jooookes”
— Praveen
01
CONCEPT
Lets think through the
game!
CONCEPT
Snake
● Dictating direction
●
Listening for keyboard
●
Growing snake if it eats
food
●
Endgame if snake eats
itself or hits window
Game loop
● while in_game = True
Drawing our snake and food
● Canvas grid
● Indexing of lists
600 px
X-axis increases as
you shift right
60 px
Y-axis increases are
you shift down
(0,0)
0
1
2
3
Grid coordinates
- [5, 3]
Canvas coordinates
- [300, 180]
600 px
X-axis increases as
you shift right
60 px
Y-axis increases are
you shift down
(0,0)
0
1
2
3
02
INITIALIZE
Our starting points!
tkinter
time
The backbone of
our graphics
Control the
framerate of game!
random
Random Number
Generator
pynput
Listen to
keyboard and
mouse
Window
Canvas
Game Frame
Score Label
Score Frame
Final
Score:
Score:
3 3
Restart
Restart
Button
SET UP GUI: Window
●
Window title
●
Window size
○
Non - resizable
SET UP GUI: Game Frame
Why not enter width and height of canvas manually?
●
●
Frame
Canvas
○
○
Width
Height
SET UP GUI: Score Frame
●
●
●
Frame
Label
Button
Block length
Coordinates
of head
Our Snake!
Food
Other
variables
Global Variables
600 px
Block length:
60 px
Canvas_height / 2
= 300px
300 px /
block_length
=5
Block length
Coordinates
of head
Our Snake!
Food
Other
variables
Global Variables
03
METHODS
It’s gonna get a lil messy
draw_board()
[ top_left_X, top_left_Y ]
[ top_left_X + block_length ,
top_left_Y + block_length ]
on_press(key)
Snakes cannot do 180 turn
keyboard.Key.<input>
Update Score
Set Keyboard
game_loop: concept
Sleep
Update Snake
Draw Board
Update Board
Post Game
Menu
game_loop()
Update body
Update each segment of
the body except the head
Create head
Move the head in the
direction currently facing
update_snake()
Food eaten?
Check if food is eaten, and
take necessary actions
Snake dead?
Check is snake is dead, and
break game loop if true
update_snake()
0
1
2
3
4
[ [15, 15], [16, 15], [17, 15], [18, 15], [19, 15]
]
[ [?, ?], [15, 15], [16, 15], [17, 15], [18, 15] ]
new_head()
N
[5, 4]
W
[4, 5]
E
[5, 5]
[5, 6]
S
[6, 5]
food_eaten()
0
0
1
0
2
1
2
1
3
2
4
3
3
food_eaten()
snake
body (block)
[ [15, 15], [16, 15], [17, 15], [18, 15] ]
[ [15, 15], [16, 15], [17, 15], [18, 15], [18, 15]
]
[ [14, 15], [15, 15], [16, 15], [17, 15], [18, 15]
]
respawn_food()
60 px
600 px
X = (canvas_width /
block_length) - 1
Y=0
X=0
X = (canvas_width /
block_length) - 1
Y = (canvas_height /
block_length) - 1
Y = (canvas_height /
block_length) - 1
respawn_food()
check_food()
update_score()
snake_death()
Check if
snake
head has
Overlap (eaten) its
own body
Exited the canvas
update_board()
post_game_menu()
restart()
04
EXECUTION
We are almost done!
game_loop()
root.mainloop()
It is done!
Run the game :)
Q&A Time
THANKS!
Join our discord server through
the QR code here
Follow our instagram
@projectglitch__ for updates on
future events
CREDITS: This presentation template was created by
Slidesgo, including icons by Flaticon, and
infographics & images by Freepik
Joint collaboration by Project
Glitch and MacPherson CC
See you at our future events!
Download