Created
February 27, 2016 15:09
-
-
Save samorajp/66bbc1760b854e8ff3f1 to your computer and use it in GitHub Desktop.
Wizualizacja w PyGame do programu https://gist.github.com/samorajp/a182fb22eb59fe89b620
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
# encoding: utf-8 | |
import pygame | |
from pygame.draw import circle, line | |
from pygame.locals import * | |
import misjonarze | |
# Initialize the game engine | |
pygame.init() | |
# Constants | |
BLACK = (0, 0, 0) | |
WHITE = (255, 255, 255) | |
RED = (255, 0, 0) | |
BLUE = (0, 0, 255) | |
GREEN = (0, 255, 0) | |
RADIUS = 10 | |
STEP = 25 | |
LEFT_POS = (100, 150) | |
RIGHT_POS = (400, 150) | |
SIZE = (600, 300) | |
# Setup | |
screen = pygame.display.set_mode(SIZE, HWSURFACE | DOUBLEBUF | RESIZABLE) | |
pygame.display.set_caption(u"Misjonarze i kanibale") | |
# Pre-logic | |
clock = pygame.time.Clock() | |
missionaries = cannibals = 3 | |
boat_size = 2 | |
move_number = 0 | |
solution = misjonarze.main(missionaries, cannibals, boat_size) | |
# Pre-drawing | |
def draw_state(state): | |
def modify(pair, diff): | |
x, y = pair | |
dx, dy = diff | |
return x + dx, y + dy | |
def draw_dots(dots, side, offset): | |
start_pos = LEFT_POS if side == "L" else RIGHT_POS | |
for i, dot_color in enumerate(dots): | |
new_pos = modify(start_pos, (offset, i * STEP)) | |
circle(screen, dot_color, new_pos, RADIUS) | |
def draw_boat(side): | |
start_pos = modify(LEFT_POS if side == "L" else RIGHT_POS, | |
(-RADIUS, -STEP)) | |
end_pos = modify(start_pos, (5 * RADIUS, 0)) | |
line(screen, BLACK, start_pos, end_pos, RADIUS) | |
ml, kl, boat, mr, kr = state | |
draw_dots([BLUE] * ml, "L", 0) | |
draw_dots([RED] * kl, "L", 3 * RADIUS) | |
draw_dots([BLUE] * mr, "R", 0) | |
draw_dots([RED] * kr, "R", 3 * RADIUS) | |
draw_boat(boat) | |
def draw_text(solution_to_draw): | |
moves = len(solution_to_draw) - 1 | |
info_m = u"Misjonarzy: %s" % missionaries | |
info_k = u"Kanibali: %s" % cannibals | |
info_l = u"Pojemność łódki: %s" % boat_size | |
info_bottom = (u"ruchów: %s / %s" % (move_number, moves) | |
if moves else u"Brak rozwiązania") | |
info_sterowanie = u"Sterowanie: W, S, strzałki" | |
font = pygame.font.Font(None, 48) | |
text_m = font.render(info_m, 1, BLUE) | |
text_k = font.render(info_k, 1, RED) | |
text_l = font.render(info_l, 1, BLACK) | |
text_state = font.render(info_bottom, 1, (10, 10, 10)) | |
text_sterowanie = font.render(info_sterowanie, 1, BLACK) | |
screen_pos = screen.get_rect() | |
text_m_pos = text_m.get_rect() | |
text_m_pos.topleft = screen_pos.topleft | |
text_k_pos = text_k.get_rect() | |
text_k_pos.topleft = text_m_pos.bottomleft | |
text_l_pos = text_l.get_rect() | |
text_l_pos.topright = screen_pos.topright | |
text_state_pos = text_state.get_rect() | |
text_state_pos.topright = text_l_pos.bottomright | |
text_sterowanie_pos = text_sterowanie.get_rect() | |
text_sterowanie_pos.bottomright = screen_pos.bottomright | |
for text, pos in zip( | |
(text_m, text_k, text_state, text_l, text_sterowanie), | |
(text_m_pos, text_k_pos, text_state_pos, text_l_pos, | |
text_sterowanie_pos)): | |
screen.blit(text, pos) | |
while True: | |
for event in pygame.event.get(): # User did something | |
if event.type == pygame.QUIT: # If user clicked close | |
break | |
if event.type == VIDEORESIZE: | |
screen = pygame.display.set_mode( | |
event.dict['size'], HWSURFACE | DOUBLEBUF | RESIZABLE) | |
if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT: | |
move_number = (move_number + 1) % len(solution) | |
if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT: | |
move_number = (move_number - 1) % len(solution) | |
if event.type == pygame.KEYDOWN and event.key == pygame.K_UP: | |
move_number = 0 | |
missionaries, cannibals = missionaries + 1, cannibals + 1 | |
solution = misjonarze.main(missionaries, cannibals, boat_size) | |
if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN: | |
move_number = 0 | |
missionaries, cannibals = missionaries - 1, cannibals - 1 | |
solution = misjonarze.main(missionaries, cannibals, boat_size) | |
if event.type == pygame.KEYDOWN and event.key == pygame.K_w: | |
move_number = 0 | |
boat_size += 1 | |
solution = misjonarze.main(missionaries, cannibals, boat_size) | |
if event.type == pygame.KEYDOWN and event.key == pygame.K_s: | |
move_number = 0 | |
boat_size -= 1 | |
solution = misjonarze.main(missionaries, cannibals, boat_size) | |
# Set the screen background | |
screen.fill(WHITE) | |
draw_state(solution[move_number]) | |
draw_text(solution) | |
# Go ahead and update the screen with what we've drawn. | |
pygame.display.flip() | |
clock.tick(10) | |
# Be IDLE friendly. If you forget this line, the program will 'hang' | |
# on exit. | |
pygame.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment