Created
April 28, 2017 07:50
-
-
Save diunko/5cefa6c5efee89ef9bf73b4c16f45e88 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
logging.basicConfig(level=logging.DEBUG) | |
from time import sleep | |
import pygame | |
import snake_step | |
log = logging.getLogger("snake") | |
log_events = logging.getLogger("snake.events") | |
log_events.setLevel(logging.INFO) | |
X, Y = 10, 10 | |
CELL_SIZE = 30 | |
WIDTH, HEIGHT = CELL_SIZE*X, CELL_SIZE*Y | |
def make_field(): | |
field = pygame.Surface((WIDTH, HEIGHT)) | |
field.fill((255,255,150)) | |
dx = 0 | |
while dx <= WIDTH: | |
pygame.draw.line(field, (0,0,0), (dx, 0), (dx, HEIGHT)) | |
dx += CELL_SIZE | |
dy = 0 | |
while dy <= HEIGHT: | |
pygame.draw.line(field, (0,0,0), (0,dy), (WIDTH, dy)) | |
dy += CELL_SIZE | |
return field | |
def pause(t=0.2, name="<>"): | |
log.debug("pause[%s] %s", t, name) | |
sleep(t) | |
#log.debug("wake from %s", name) | |
def draw_map(field, Map): | |
y = 0 | |
while y < Y: | |
x = 0 | |
while x < X: | |
if Map[y][x] == 1: | |
pygame.draw.rect(field, (0,125,0), | |
[x*CELL_SIZE, y*CELL_SIZE, | |
CELL_SIZE, CELL_SIZE]) | |
elif Map[y][x] != 0: | |
pygame.draw.rect(field, (0,200,0), | |
[x*CELL_SIZE, y*CELL_SIZE, | |
CELL_SIZE, CELL_SIZE]) | |
x = x + 1 | |
y = y+1 | |
RIGHT, DOWN, LEFT, UP = 1, 2, 3, 4 | |
def main(): | |
Map = [ | |
[0,0,0,0,8,0,0,0,0,0], | |
[0,0,0,0,7,0,0,0,0,0], | |
[0,0,0,0,6,0,0,0,0,0], | |
[0,0,0,0,5,0,0,0,0,0], | |
[0,0,0,0,4,3,2,1,0,0], | |
[0,0,0,0,0,0,0,0,0,0], | |
[0,0,14,0,0,0,0,0,0,0], | |
[0,0,13,0,0,0,0,0,0,0], | |
[0,0,12,11,10,0,0,0,0,0], | |
[0,0,0,0,9,0,0,0,0,0] | |
] | |
pygame.init() | |
screen = pygame.display.set_mode((WIDTH*2+5, HEIGHT*2+5)) | |
escape = False | |
d = RIGHT | |
while not escape: | |
field = make_field() | |
draw_map(field, Map) | |
screen.blit(field, (0,0)) | |
screen.blit(field, (WIDTH+5,0)) | |
screen.blit(field, (0, HEIGHT+5)) | |
screen.blit(field, (WIDTH+5, HEIGHT+5)) | |
pygame.display.update() | |
pause(0.2) | |
for e in pygame.event.get(): | |
log_events.debug("event: %s", e) | |
if e.type == pygame.KEYDOWN: | |
if e.key == pygame.K_UP: d=UP | |
if e.key == pygame.K_RIGHT: d=RIGHT | |
if e.key == pygame.K_DOWN: d=DOWN | |
if e.key == pygame.K_LEFT: d=LEFT | |
if e.key == pygame.K_ESCAPE: | |
escape = True | |
Map = snake_step.step(Map, d) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment