
Snake Game in 20 Lines with Python | Pygame |
Coder Space
Views: 94392
Like: 4432
The Snake game in 20 lines (!!!) of code in the Python programming language using the Pygame graphics library. Math magic!
Pygame is a convenient and simple library for displaying graphical information, for creating simple games, perfect for beginners to program in Python.
To install Pygame, type in a terminal and press Enter: “pip install pygame” (no quotes)
#coderspace
#shorts
#python
#snakegame
#pythonpygame
23.09.2023
this is alright, but how the hell is a beginner going to gain anything from this. this is pretty unreadable.
Ok now do it with just Turtle
WOAH THIS IS CRAZY, as a fellow wanabe programer, i can berly make pong with 20000000000000000000000 lines of code so good job
Mine opens then closes again
Showing error no module name pygame
I Wish ThIs Work On Mobile 🙁
What applications needed?
…and to think my implementation was 147…
The window crashes as soon as I run the code 🙁
Nobody cares about that apple not being randomly generated? It’s always generated at the end of the snake…but yeah, still cool 😁
What is the code
It took me 137 lines of code 😭
ni siquiera puedo creerlo
woooooow 😍😍 I'm fall in love with pygame and python
great
What are the controls to control the snake
I can't get why this works but its so awesome!
did you mean less than 20
What is at 17th line writed
I wish youtube shorts didn't have to cut the sides of the screen…
import pygame as pg
y, step, head = segments = [15, 16, 17]
n, apple = step, 99
screen_full = pg.display.set_mode([225] * 2, pg SCALED).fill
while segments.count(head) % 2 * head % n * (head & 240):
if e := pg.event.get(768):
step = (e[0].key % 4 * 17 + 15) % 49 – n
segments = segments[apple := head] + [head + step]
screen_fill('black')
if apple == head:
apple = segments[0]
for i, v in enumerate([apple] + segments):
screen_fill('green' if i else 'red',
((v – 1)) % n * y, (v – n) // n * y, y, y))
pg.display.flip()
head+= step
pg.time.wait(100)
That's the code if u want to copy and past and makes eser to copy if u can't copy and past
Also happy new year
gud
Can you paste the 20 line thing in the description
lol
Readability left the chat
It is not working on the last version 3.11.1 of python
BRO THIS IS SO IMPRESSIVE GOOD JOB!!!
here is the actual code bc i think on another comment they spelt some stuff wrong:
import pygame as pg
y, step, head = segments = [15, 16, 17]
n, apple = step, 99
screen_fill = pg.display.set_mode ([225] * 2, pg.SCALED).fill
while segments.count(head) % 2 * head % n * (head & 240):
if e := pg.event.get(768):
step = (e[0].key % 4 * 17 + 15) % 49 – n
segments = segments[apple != head:] + [head + step]
screen_fill('black')
if apple == head:
apple = segments[0]
for i, v in enumerate([apple] + segments):
screen_fill('green' if i else 'red',
((v – 1) % n * y, (v – n) // n * y, y, y))
pg.display.flip()
head+= step
pg.time.wait(100)
soooooooo hard
damm
code explanation
(I'm not creator of his code so it'll not be 100% accurate.)
"import pygame as pg"
imports moudle
"y, step, head = segments = [15, 16, 17]"
y is width and height of TILE,
step is direction that snake moves. (16 = DOWN, -16 = UP, 1 = RIGHT, -1 = LEFT)
segments is snake (remainder of 16 is x, floor devision of 16 is y.)
"n, apple = step, 99"
n is int 16 variable that helps to get x, y coordinate.
apple is apple that is located.
"screen_fill = pg.display.set_mode([225] * 2, pg.SCALED).fill"
screen_fill is variable that helps to show screen.
"while segments.count(head) % 2 * head % n * (head & 240):"
checks if two overlapped. or snake is out of screen. if false, game exits.
"if e := pg.event.get(768):"
checks if any arrow key is pressed. if true, saves to e variable.
"step = (e[0].key % 4 * 17 + 15) % 49 – n"
evalucate key pressed number to step.
if DOWN and RIGHT pressed, DOWN counts.
"segments = segments[apple != head:] + [head + step]"
removes tail, adds new head.
if it hits apple, only adds new head, not remove tail.
"screen_fill('black')"
fill screen with black.
"if apple == head:"
checks if head hits apple.
"apple = segments[0]"
move apple to tail.
"for i, v in enumerate([apple] + segments):"
loop used to draw, uses i variable to check that it's apple or segments.
"screen_fill('green' if i else 'red', ((v – 1) % n * y, (v – n) // n * y, y, y))"
if loop used apple, draws 'red' else 'green'
evalucate int to x, y
y, y is width, height.
"pg.display.flip()"
draws screen_fill surface to screen
"head += step"
head moves to step.
"pg.time.wait(100)"
delays 100 millisecond.
Not working on 3.9.0 version ( cant read pygame )
I ran it and it looked okay but only stayed for about 6 seconds do you have any explanation for this
It not working it telling you perhaps a comma
Now showing display on my screen of pycharm
Code. Please I have an apple in place, what should I do?
the worst part is the fact, it works.
Here is an updated version fully working, and has a scoring system that goes up by 1 for each apple eaten
Code:
import pygame as pg
pg.init() # Initialize pygame
y, step, head = segments = [15, 16, 17]
n, apple = step, 99
screen_width, screen_height = 225, 225
screen = pg.display.set_mode([screen_width, screen_height], pg.SCALED)
font = pg.font.Font(None, 30)
score = 0
while segments.count(head) % 2 * head % n * (head & 240):
for e in pg.event.get(768):
if e.type == pg.KEYDOWN:
if e.key == pg.K_UP and step != n:
step = -n
elif e.key == pg.K_DOWN and step != -n:
step = n
elif e.key == pg.K_LEFT and step != 1:
step = -1
elif e.key == pg.K_RIGHT and step != -1:
step = 1
segments = segments[apple != head:] + [head + step]
screen.fill('black')
if apple == head:
apple = segments[0]
score += 1
for i, v in enumerate([apple] + segments):
screen.fill('green' if i else 'red', ((v – 1) % n * y, (v – n) // n * y, y, y))
score_text = font.render(f"Score: {score}", True, (255, 255, 255))
screen.fill((0, 0, 0), (0, 0, screen_width, 30))
screen.blit(score_text, (screen_width // 2 – score_text.get_width() // 2, 0))
pg.display.flip()
head += step
pg.time.wait(100)
Edit: Yes I know this is more than 20 lines, but it is a bit more advanced, and fixed some bugs that would kill yourself by moving back into the character and not stopping that, and adding a scoring system.
Hipgo
App name
something is wrong on line 17
i have one simple questions
how do you open the game what is shortcut that you click in the keyboard .
i appreciate your help guys thank you
Impossible…
honestly its good but should not be used for beginners because its quite weird and unreadable
hello its say that there is no screen_fill variable
I had a error in this line:
If e:=pg.event.get(768)
Syntax error:invalid error
What did you ever write what the fuck
What IDE you use?
import pygame as pg
y, step, head = segments = [15, 16, 17]
n, apple = step, 99
screen_full = pg.display.set_mode([225] * 2, pg SCALED).fill
while segments.count(head) % 2 * head % n * (head & 240):
if e := pg.event.get(768):
step = (e[0].key % 4 * 17 + 15) % 49 – n
segments = segments[apple := head] + [head + step]
screen_fill('black')
if apple == head:
apple = segments[0]
for i, v in enumerate([apple] + segments):
screen_fill('green' if i else 'red',
((v – 1)) % n * y, (v – n) // n * y, y, y))
pg.display.flip()
head+= step
pg.time.wait(100)
add : after head
[apple := head:]
how too open game?