Created
January 29, 2016 23:38
-
-
Save iminurnamez/7c96a996e30513ac03b7 to your computer and use it in GitHub Desktop.
This file contains 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 pygame | |
import sys | |
from pygame.locals import * | |
data = [] | |
matrix = [] | |
with open("data.txt") as file: | |
for line in file: | |
data.append(line) | |
ymatrix = int(data[0][0:2]) | |
xmatrix = int(data[0][3:5]) | |
del data[0] | |
starty = int(data[0][0:2]) | |
startx = int(data[0][3:5]) | |
del data[0] | |
charx = startx | |
chary = starty | |
pygame.init() | |
setdisplay = pygame.display.set_mode((xmatrix*16, ymatrix*16)) | |
pygame.display.set_caption ("game") | |
background = pygame.image.load("background.png") | |
char = pygame.image.load("char.png") | |
fps = 30 | |
fpsTime = pygame.time.Clock() | |
for line in data: | |
matrix.append(line.strip().split(" ")) | |
move_keys = {K_UP: "up", | |
K_DOWN: "down", | |
K_LEFT: "left", | |
K_RIGHT: "right"} | |
directs = {"up": (0, -1), | |
"down": (0, 1), | |
"left": (-1, 0), | |
"right": (1, 0)} | |
def move(x, y, direction, matrix): | |
steps = [0, 0] | |
vx = directs[direction][0] | |
vy = directs[direction][1] | |
try: | |
cell = matrix[y + vy][x + vx] | |
if cell == "f": | |
steps[0] += vx | |
steps[1] += vy | |
elif cell == "i": | |
while matrix[y + steps[1] + vy][x + steps[0] + vx] == "i": | |
steps[0] += vx | |
steps[1] += vy | |
else: | |
if matrix[y + steps[1] + vy][x + steps[0] + vx] == "f": | |
steps[0] -= vx | |
steps[1] -= vy | |
except IndexError: | |
return [0, 0] | |
return steps | |
while True: | |
setdisplay.blit(background, (0, 0)) | |
setdisplay.blit(char, (charx * 16, chary * 16)) | |
for event in pygame.event.get(): | |
if event.type == QUIT: | |
pygame.quit() | |
sys.exit() | |
if event.type == KEYDOWN: | |
if event.key in move_keys: | |
direction = move_keys[event.key] | |
steps = move(charx, chary, direction, matrix) | |
charx += steps[0] | |
chary += steps[1] | |
pygame.display.update() | |
fpsTime.tick(fps) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment