The code dislayed above is written in javascript, which performs the python code below by clicking on the cavas. Press "J" to go to js-canvas-lesson and get the script.
import pygame from pygame.locals import * import time class Snake: def __init__(self,parent_surface): self.parent_surface = parent_surface self.block = pygame.image.load('images/block.jpg').convert() self.x = 100 self.y = 100 self.direction = '' def draw(self): self.parent_surface.fill((255,255,255)) self.parent_surface.blit(self.block,(self.x,self.y)) pygame.display.flip() def move_down(self): self.direction = 'down' def move_up(self): self.direction = 'up' def move_left(self): self.direction = 'left' def move_right(self): self.direction = 'right' def walk(self): if self.direction == 'down': self.y += 10 if self.direction == 'up': self.y -= 10 if self.direction == 'left': self.x -= 10 if self.direction == 'right': self.x += 10 self.draw() class Game: def __init__(self): pygame.init() self.surface = pygame.display.set_mode((500,400)) self.surface.fill((255,255,255)) self.snake = Snake(self.surface) self.snake.draw() def run(self): running = True while running: for event in pygame.event.get(): if event.type == KEYDOWN: if event.key == K_ESCAPE or event.key == K_q: running = False if event.key == K_DOWN or event.key == K_s: self.snake.move_down() if event.key == K_UP or event.key == K_w: self.snake.move_up() if event.key == K_LEFT or event.key == K_a: self.snake.move_left() if event.key == K_RIGHT or event.key == K_d: self.snake.move_right() if event.type == QUIT: running = False self.snake.walk() time.sleep(.3) if __name__ == '__main__': game = Game() game.run()