Skip to content

Instantly share code, notes, and snippets.

@nati
Created August 10, 2018 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nati/5127a27516ff8e244e8b475aa292595c to your computer and use it in GitHub Desktop.
Save nati/5127a27516ff8e244e8b475aa292595c to your computer and use it in GitHub Desktop.
import pygame
import sys
import time
import random
error = pygame.init()
if error[1] > 0:
print error
sys.exit(-1)
width = 720
height = 460
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Snake')
dot_color = pygame.Color(255, 0, 0)
background_color = pygame.Color(0, 255, 255)
clock = pygame.time.Clock()
x = 0
y = 0
dx = 0
dy = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
dx = 10
dy = 0
if event.key == pygame.K_LEFT:
dx = -10
dy = 0
if event.key == pygame.K_UP:
dy = -10
dx = 0
if event.key == pygame.K_DOWN:
dy = 10
dx = 0
if event.key == pygame.K_ESCAPE:
pygame.event.post(pygame.event.Event(pygame.QUIT))
x = (x + dx + width) % width
y = (y + dy + height) % height
screen.fill(background_color)
pygame.draw.rect(screen, dot_color, pygame.Rect(x, y, 10, 10))
pygame.display.flip()
clock.tick(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment