🔧 AI Nachrichten GitHub Release: openai/codex vrust-v0.155.0-alpha.3.5 (11.09.2026)(11.09.2026 um 07:07 Uhr)
🔧 AI Nachrichten GitHub Release: openai/codex vrust-v0.155.0-alpha.3.6 (11.09.2026)(11.09.2026 um 08:08 Uhr)
🔧 AI Nachrichten GitHub Release: openai/codex vrust-v0.155.0-alpha.3.7 (11.09.2026)(11.09.2026 um 10:17 Uhr)
🔧 AI Nachrichten GitHub Release: openai/codex vrust-v0.155.0-alpha.3.8 (11.09.2026)(11.09.2026 um 12:30 Uhr)
🔧 AI Nachrichten GitHub Release: openai/codex vrust-v0.155.0-alpha.3.9 (11.09.2026)(11.09.2026 um 14:01 Uhr)
🔧 AI Nachrichten GitHub Release: openai/codex vrust-v0.155.0-alpha.3.10 (11.09.2026)(11.09.2026 um 17:56 Uhr)
🔧 AI Nachrichten GitHub Release: openai/codex vrust-v0.155.0-alpha.4 (14.09.2026)(14.09.2026 um 13:49 Uhr)
🔧 AI Nachrichten GitHub Release: openai/codex vrust-v0.155.0-alpha.2.4 (15.09.2026)(15.09.2026 um 01:05 Uhr)
🔧 AI Nachrichten GitHub Release: openai/codex vrust-v0.155.0-alpha.5 (15.09.2026)(15.09.2026 um 02:33 Uhr)
🔧 AI Nachrichten GitHub Release: openai/codex vrust-v0.155.0-alpha.6 (15.09.2026)(15.09.2026 um 04:02 Uhr)
🔧 AI Nachrichten GitHub Release: openai/codex vrust-v0.155.0-alpha.3.5 (11.09.2026)(11.09.2026 um 07:07 Uhr)
🔧 AI Nachrichten GitHub Release: openai/codex vrust-v0.155.0-alpha.3.6 (11.09.2026)(11.09.2026 um 08:08 Uhr)
🔧 AI Nachrichten GitHub Release: openai/codex vrust-v0.155.0-alpha.3.7 (11.09.2026)(11.09.2026 um 10:17 Uhr)
🔧 AI Nachrichten GitHub Release: openai/codex vrust-v0.155.0-alpha.3.8 (11.09.2026)(11.09.2026 um 12:30 Uhr)
🔧 AI Nachrichten GitHub Release: openai/codex vrust-v0.155.0-alpha.3.9 (11.09.2026)(11.09.2026 um 14:01 Uhr)
🔧 AI Nachrichten GitHub Release: openai/codex vrust-v0.155.0-alpha.3.10 (11.09.2026)(11.09.2026 um 17:56 Uhr)
🔧 AI Nachrichten GitHub Release: openai/codex vrust-v0.155.0-alpha.4 (14.09.2026)(14.09.2026 um 13:49 Uhr)
🔧 AI Nachrichten GitHub Release: openai/codex vrust-v0.155.0-alpha.2.4 (15.09.2026)(15.09.2026 um 01:05 Uhr)
🔧 AI Nachrichten GitHub Release: openai/codex vrust-v0.155.0-alpha.5 (15.09.2026)(15.09.2026 um 02:33 Uhr)
🔧 AI Nachrichten GitHub Release: openai/codex vrust-v0.155.0-alpha.6 (15.09.2026)(15.09.2026 um 04:02 Uhr)

🔧 Programmierung 🕛 vor 4 Monaten 3 Min Lesezeit
0

Pygame Snake, Pt. 3

↗ Quelle (dev.to)
🗣️ Stimme:

Part 2 left off with a square moving on a grid. Now, let's take control of it by consuming KEYDOWN events.



One simple approach would be to have a variable that represents one of the four directions the snake can go. It might use values 0-3, or it could use string literals like "up" and "left".



A slightly more advanced technique would be to use the Vector2 class. We use pygame's Vector2 to keep track of the position of out snake part. We could also use a Vector2 to represent the snake's speed and direction. For example, the direction right would be represented by Vector2(1, 0), because if we add that vector to the current position, it will increase the x value (the first coordinate) by 1.



Left would be Vector2(-1, 0). Up and down would be Vector2(0, -1) and Vector2(0, 1), respectively. If up and down seem backwards, it's because in pygame (which uses a common computer drawing convention) the Y-axis increases as you go down, instead of up. The origin is the canvas's upper-left corner.



Add a new variable under dot:




CODE
vel = pygame.Vector2(1, 0)






Inside the for loop that consumes events, add the following under the first if statement:




CODE
        if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
vel = pygame.Vector2(1, 0)
if event.key == pygame.K_DOWN:
vel = pygame.Vector2(0, 1)
if event.key == pygame.K_LEFT:
vel = pygame.Vector2(-1, 0)
if event.key == pygame.K_UP:
vel = pygame.Vector2(0, -1)






Finally, update the line of code that moves dot:




CODE
dot = dot + vel # or dot += vel






When you run your program now, you should be able to control the dot's movement with the arrow keys.



Full code at this point:




CODE
import pygame

W = 30
H = 30
S = 20

# pygame setup
pygame.init()
screen = pygame.display.set_mode((W * S, H * S))
clock = pygame.time.Clock()
running = True

dot = pygame.Vector2(W / 2, H / 2)
vel = pygame.Vector2(1, 0)

while running:
# poll for events
for event in pygame.event.get():
# pygame.QUIT = user closed window
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
vel = pygame.Vector2(1, 0)
if event.key == pygame.K_DOWN:
vel = pygame.Vector2(0, 1)
if event.key == pygame.K_LEFT:
vel = pygame.Vector2(-1, 0)
if event.key == pygame.K_UP:
vel = pygame.Vector2(0, -1)

# fill buffer with white
screen.fill("white")

dot += vel
square = pygame.Rect(dot * S, (S, S))
screen.fill("black", square)

# copy buffer to screen
pygame.display.flip()

# limits FPS
clock.tick(20)

pygame.quit()


Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Debian 11 Long Term Support reaches end-of-life
1 Quelle
Updated Debian 13: 13.7 released
1 Quelle
USN-8563-5: nginx vulnerability
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Pygame Snake, Pt. 3

Thematisch verwandte Begriffe: Pygame, Snake · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...