Last active
December 29, 2015 07:39
-
-
Save sykire/7637721 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 game | |
import pygame | |
import sys | |
from itertools import product | |
from operator import mul | |
from functools import partial | |
NORMAL = 0 | |
OBSTACLE = 1 | |
FLOOR = 2 | |
SPAWN = 4 | |
ENEMY = 8 | |
ALLY = 16 | |
ALL = ENEMY | ALLY | |
types = { | |
"obstacle": OBSTACLE, | |
"spawn": SPAWN, | |
"normal": NORMAL, | |
"invalid": OBSTACLE, | |
"enemy": ENEMY, | |
"floor": FLOOR, | |
"ally": ALLY | |
} | |
colors = { | |
OBSTACLE:(25,25,25,25), | |
FLOOR: (255,255,255,0), | |
ENEMY: (255,0,0,0), | |
ALLY: (0,255,0,0), | |
SPAWN: (240,240,240,0) | |
} | |
class Render: | |
def __init__(self, game_inst, settings, block_size=25): | |
self._settings = settings | |
self._blocksize = block_size | |
self._boardsize = self._settings.board_size | |
self._winsize = block_size * self._boardsize | |
self._game = game_inst | |
#self._colors = game.Field(self._settings.board_size) | |
self.paused = True | |
self._b = {} | |
self.generate_map() | |
self.bot_pan_size = block_size*4 | |
self.log_pan_size = 250 | |
pygame.init() | |
pygame.font.init() | |
self.text = pygame.font.SysFont("monospace", 15) | |
self.current = (0,0) | |
self.screen = pygame.display.set_mode( | |
(self._winsize+self.log_pan_size, | |
self._winsize)) | |
pygame.display.set_caption("Robot Game") | |
self.turn = 0 | |
#import pprint | |
#pprint.pprint(self._b) | |
mul_block = partial(mul, self._blocksize) | |
empty_map = product(range(self._boardsize),repeat=2) | |
self.rects = map(lambda x: map(mul_block,x), empty_map) | |
self.turns_ally = dict(enumerate(game_inst.history[0])) | |
self.turns_enemy = dict(enumerate(game_inst.history[1])) | |
#path = dict( | |
# filter(lambda z: z[1], | |
# enumerate( | |
# map(lambda x: filter(lambda y: y[2] == 60, x), | |
# game_inst.history[0]) | |
# ))) | |
#self._master = Tkinter.Tk() | |
#self._master.title('robot game') | |
#width = self._winsize | |
#height = self._winsize + self._blocksize * 7/4 | |
#self._win = Tkinter.Canvas(self._master, width=width, height=height) | |
# self._win.pack() | |
# self.prepare_backdrop(self._win) | |
# self._label = self._win.create_text( | |
# self._blocksize/2, self._winsize + self._blocksize/2, | |
# anchor='nw', font='TkFixedFont', fill='white') | |
#self.create_controls(self._win, width, height) | |
#self._turn = 1 | |
#self._texts = [] | |
#self._squares = {} | |
# self.callback() | |
# self.update() | |
# self._win.mainloop() | |
self.mainloop() | |
def generate_map(self): | |
self._b.update(dict.fromkeys(self._settings.spawn_coords, SPAWN | FLOOR)) | |
self._b.update(dict.fromkeys(self._settings.obstacles, OBSTACLE)) | |
for i, j in product(range(self._boardsize), repeat=2): | |
self._b.setdefault((i,j), FLOOR) | |
def mainloop(self): | |
while 1: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
pygame.quit() | |
sys.exit() | |
if event.type == pygame.KEYDOWN: | |
if event.key == pygame.K_SPACE: | |
self.paused = False if self.paused else True | |
if self.paused: | |
if event.key == pygame.K_RIGHT: | |
if self.turn < self._settings.max_turns-1: | |
self.turn +=1 | |
elif event.key == pygame.K_LEFT: | |
if self.turn > 0: | |
self.turn -=1 | |
if not self.paused: | |
if self.turn < self._settings.max_turns-1: | |
self.turn +=1 | |
else: | |
self.pause = True | |
pygame.time.wait(100) | |
if pygame.mouse.get_pos()[0] < self._winsize: | |
self.current = [x/self._blocksize for x in pygame.mouse.get_pos()] | |
self.label = self.text.render(str(self.current), 1, (255,255,0)) | |
self.redraw() | |
def redraw(self): | |
self.screen.fill((0,0,0,0)) | |
a = 0 | |
for rect in self.rects: | |
loc = tuple(map(lambda x: x/self._blocksize,rect)) | |
_b = self._b | |
color = (0,0,0,0) | |
if _b[loc] == FLOOR: | |
color = colors[FLOOR] | |
elif _b[loc] == OBSTACLE: | |
color = colors[OBSTACLE] | |
elif _b[loc] & SPAWN: | |
color = colors[SPAWN] | |
rect = pygame.Rect(rect + 2*[self._blocksize]).inflate(-1,-1) | |
pygame.draw.rect(self.screen, color, rect) | |
for player in self.turns_ally[self.turn]: | |
pos = [x*self._blocksize for x in player[0]] | |
hp = player[1] | |
print hp | |
rect = pygame.Rect(pos + 2*[self._blocksize]).inflate(-1,-1) | |
hp_label = self.text.render(str(hp), 1, (0,0,0)) | |
pygame.draw.rect(self.screen, colors[ALLY], rect) | |
self.screen.blit(hp_label, rect.topleft) | |
for player in self.turns_enemy[self.turn]: | |
pos = [x*self._blocksize for x in player[0]] | |
rect = pygame.Rect(pos + 2*[self._blocksize]).inflate(-1,-1) | |
hp_label = self.text.render(str(hp), 1, (0,0,0)) | |
pygame.draw.rect(self.screen, colors[ENEMY], rect) | |
self.screen.blit(hp_label, rect.topleft) | |
#if _b[loc] & ENEMY: | |
# color = colors[ENEMY] | |
#elif _b[loc] & SPAWN: | |
# color = colors[SPAWN] | |
#self.screen.fill(color,rect) | |
self.screen.blit(self.label, (400, 25)) | |
pygame.display.flip() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment