Pygame Snake, Pt. 2
📰 Dev.to · David Newberry
Learn to build a Snake game using Pygame by setting up a grid system and implementing game logic
Action Steps
- Set up a Pygame project with a 600x600 pixel canvas
- Define variables for width, height, and tile size to create a 30x30 grid
- Replace hardcoded numbers with references to these variables
- Implement boundary checking to keep the snake within the grid
- Use Pygame's Vector2 and Rect classes to represent the snake's position and size
Who Needs to Know This
This tutorial is suitable for beginners in game development, especially those interested in using Pygame. It can be beneficial for solo developers or small teams looking to create simple games.
Key Insight
💡 Using variables for width, height, and tile size makes it easy to modify the game's grid system
Share This
🐍 Build a Snake game with Pygame! Learn how to set up a grid system and implement game logic in this beginner-friendly tutorial 💻
Key Takeaways
Learn to build a Snake game using Pygame by setting up a grid system and implementing game logic
Full Article
Title: Pygame Snake, Pt. 2
URL Source: https://dev.to/paxfeline/pygame-snake-pt-2-362l
Published Time: 2026-04-21T22:23:58Z
Markdown Content:
# Pygame Snake, Pt. 2 - DEV Community
[Skip to content](https://dev.to/paxfeline/pygame-snake-pt-2-362l#main-content)
[](https://dev.to/)
[Powered by Algolia](https://www.algolia.com/developers/?utm_source=devto&utm_medium=referral)
[Log in](https://dev.to/enter?signup_subforem=1)[Create account](https://dev.to/enter?signup_subforem=1&state=new-user)
## DEV Community
0 Add reaction
0 Like 0 Unicorn 0 Exploding Head 0 Raised Hands 0 Fire
0 Jump to Comments 0 Save Boost
Copy link
Copied to Clipboard
[Share to X](https://twitter.com/intent/tweet?text=%22Pygame%20Snake%2C%20Pt.%202%22%20by%20David%20Newberry%20%23DEVCommunity%20https%3A%2F%2Fdev.to%2Fpaxfeline%2Fpygame-snake-pt-2-362l)[Share to LinkedIn](https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fdev.to%2Fpaxfeline%2Fpygame-snake-pt-2-362l&title=Pygame%20Snake%2C%20Pt.%202&summary=Building%20Snake%20in%20Python%20using%20pygame%2C%20bit-by-bit.&source=DEV%20Community)[Share to Facebook](https://www.facebook.com/sharer.php?u=https%3A%2F%2Fdev.to%2Fpaxfeline%2Fpygame-snake-pt-2-362l)[Share to Mastodon](https://s2f.kytta.dev/?text=https%3A%2F%2Fdev.to%2Fpaxfeline%2Fpygame-snake-pt-2-362l)
[Share Post via...](https://dev.to/paxfeline/pygame-snake-pt-2-362l#)[Report Abuse](https://dev.to/report-abuse)
[](https://dev.to/paxfeline)
[David Newberry](https://dev.to/paxfeline)
Posted on Apr 21
# Pygame Snake, Pt. 2
[#python](https://dev.to/t/python)[#gamedev](https://dev.to/t/gamedev)[#tutorial](https://dev.to/t/tutorial)[#beginners](https://dev.to/t/beginners)
In part 1, we just got pygame going. We have a 1000x1000 pixel canvas, with a 50x50 square moving more-or-less continuously. Snake is played on a fixed grid.
Instead of 50x50 pixels, we'll shrink the snake parts/tiles to 20x20, and we'll shrink the canvas to 600x600 pixels, treated as a 30x30 grid. (30 tiles x 20 pixels = 600 pixels.)
We'll add three variables to represent these values:
```
W = 30 # width
H = 30 # height
S = 20 # tile size
```
Now we should replace all other numbers in the code with references to these values.
```
screen = pygame.display.set_mode((W * S, H * S))
```
```
dot = pygame.Vector2(W / 2, H / 2) # approx. the center
```
```
square = pygame.Rect(dot * S, (S, S))
```
These values are referenced in several places in the code. By using variables, you can update them in one place and it will be reflected everywhere. You can easily change the width, height, or tile size.
Add this just above `square = ...` (the line shown above):
```
if dot.x > W: dot.x = 0
```
Full code at this point:
```
import pygame
W = 30
H = 30
S = 20
# pygame setup
pygame.init()
screen = pygame.display.set_mode((W * S,
URL Source: https://dev.to/paxfeline/pygame-snake-pt-2-362l
Published Time: 2026-04-21T22:23:58Z
Markdown Content:
# Pygame Snake, Pt. 2 - DEV Community
[Skip to content](https://dev.to/paxfeline/pygame-snake-pt-2-362l#main-content)
[](https://dev.to/)
[Powered by Algolia](https://www.algolia.com/developers/?utm_source=devto&utm_medium=referral)
[Log in](https://dev.to/enter?signup_subforem=1)[Create account](https://dev.to/enter?signup_subforem=1&state=new-user)
## DEV Community
0 Add reaction
0 Like 0 Unicorn 0 Exploding Head 0 Raised Hands 0 Fire
0 Jump to Comments 0 Save Boost
Copy link
Copied to Clipboard
[Share to X](https://twitter.com/intent/tweet?text=%22Pygame%20Snake%2C%20Pt.%202%22%20by%20David%20Newberry%20%23DEVCommunity%20https%3A%2F%2Fdev.to%2Fpaxfeline%2Fpygame-snake-pt-2-362l)[Share to LinkedIn](https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fdev.to%2Fpaxfeline%2Fpygame-snake-pt-2-362l&title=Pygame%20Snake%2C%20Pt.%202&summary=Building%20Snake%20in%20Python%20using%20pygame%2C%20bit-by-bit.&source=DEV%20Community)[Share to Facebook](https://www.facebook.com/sharer.php?u=https%3A%2F%2Fdev.to%2Fpaxfeline%2Fpygame-snake-pt-2-362l)[Share to Mastodon](https://s2f.kytta.dev/?text=https%3A%2F%2Fdev.to%2Fpaxfeline%2Fpygame-snake-pt-2-362l)
[Share Post via...](https://dev.to/paxfeline/pygame-snake-pt-2-362l#)[Report Abuse](https://dev.to/report-abuse)
[](https://dev.to/paxfeline)
[David Newberry](https://dev.to/paxfeline)
Posted on Apr 21
# Pygame Snake, Pt. 2
[#python](https://dev.to/t/python)[#gamedev](https://dev.to/t/gamedev)[#tutorial](https://dev.to/t/tutorial)[#beginners](https://dev.to/t/beginners)
In part 1, we just got pygame going. We have a 1000x1000 pixel canvas, with a 50x50 square moving more-or-less continuously. Snake is played on a fixed grid.
Instead of 50x50 pixels, we'll shrink the snake parts/tiles to 20x20, and we'll shrink the canvas to 600x600 pixels, treated as a 30x30 grid. (30 tiles x 20 pixels = 600 pixels.)
We'll add three variables to represent these values:
```
W = 30 # width
H = 30 # height
S = 20 # tile size
```
Now we should replace all other numbers in the code with references to these values.
```
screen = pygame.display.set_mode((W * S, H * S))
```
```
dot = pygame.Vector2(W / 2, H / 2) # approx. the center
```
```
square = pygame.Rect(dot * S, (S, S))
```
These values are referenced in several places in the code. By using variables, you can update them in one place and it will be reflected everywhere. You can easily change the width, height, or tile size.
Add this just above `square = ...` (the line shown above):
```
if dot.x > W: dot.x = 0
```
Full code at this point:
```
import pygame
W = 30
H = 30
S = 20
# pygame setup
pygame.init()
screen = pygame.display.set_mode((W * S,
DeepCamp AI