Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.1 kB
1
Indexable
Never
import pygame
import time

cell_size = 20

pygame.init()
win = pygame.display.set_mode((600,400))
color = (255,255,255)

def clear():
    win.fill(color)
    for i in range(0, 600, cell_size):
        pygame.draw.line(win, (0,0,0), (i,0), (i,400))

    for j in range(0, 400, cell_size):
        pygame.draw.line(win, (0,0,0), (0,j), (600,j))

pygame.display.update()

snake_dir = 'right'
x = 0
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                snake_dir = 'left'
            elif event.key == pygame.K_RIGHT:
                snake_dir = 'right'

    if snake_dir == 'right':   
        x += cell_size
    elif snake_dir == 'left':
        x -= cell_size

    if x >= 600:
        x = 0
    elif x<=0:
        x = 600

    
    
    clear()
    pygame.draw.rect(win,(0,0,0),(x,0, cell_size, cell_size))
    time.sleep(0.1)
    pygame.display.update()









    
Leave a Comment