diff options
| author | Joel Kronqvist <joel.kronqvist@iki.fi> | 2025-10-26 12:52:34 +0200 |
|---|---|---|
| committer | Joel Kronqvist <joel.kronqvist@iki.fi> | 2025-10-26 12:52:34 +0200 |
| commit | 57f20a5ef761985b34817846d471a064b180e089 (patch) | |
| tree | 9d0029868b9e170bf07ffab9e0386b06b4189837 /main.py | |
| download | SnakePuzzle-57f20a5ef761985b34817846d471a064b180e089.tar.gz SnakePuzzle-57f20a5ef761985b34817846d471a064b180e089.zip | |
feat: implemented simple snake game
Diffstat (limited to 'main.py')
| -rwxr-xr-x | main.py | 44 |
1 files changed, 44 insertions, 0 deletions
@@ -0,0 +1,44 @@ +#!/bin/python + +import pygame +import Vec +from GameView import GameView + + +view = GameView() + +nextControlDirection = None +pygame.init +screen = pygame.display.set_mode((view.width(), view.height())) +clock = pygame.time.Clock() +running = True + +while running: + + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + + keys = pygame.key.get_pressed() + if keys[pygame.K_w] and view.game.snake.heading != Vec.up.neg(): + view.nextControlDirection = Vec.up + elif keys[pygame.K_a] and view.game.snake.heading != Vec.left.neg(): + view.nextControlDirection = Vec.left + elif keys[pygame.K_r] and view.game.snake.heading != Vec.down.neg(): + view.nextControlDirection = Vec.down + elif keys[pygame.K_s] and view.game.snake.heading != Vec.right.neg(): + view.nextControlDirection = Vec.right + + if view.isRunning(): + view.update(pygame.time.get_ticks()) + + # fill the screen with a color to wipe away anything from last frame + screen.fill("purple") + + view.render(screen) + + pygame.display.flip() + + clock.tick(60) # limits FPS to 60 + +pygame.quit() |
