implemented state machine and option to switch display mode for second screen

This commit is contained in:
Simon Pirkelmann 2021-09-05 20:43:22 +02:00
parent e931085ca8
commit 945833c8ac

View File

@ -3,6 +3,7 @@ import random
import pygame import pygame
import time import time
import copy import copy
import os
BLACK = np.array([0, 0, 0], dtype=np.uint8) BLACK = np.array([0, 0, 0], dtype=np.uint8)
WHITE = np.array([255, 255, 255], dtype=np.uint8) WHITE = np.array([255, 255, 255], dtype=np.uint8)
@ -13,27 +14,31 @@ GREEN = np.array([0, 255, 0], dtype=np.uint8)
pygame.init() pygame.init()
#os.environ['SDL_VIDEO_WINDOW_POS'] = '1920, 280'
pygame.font.init() # you have to call this at the start, pygame.font.init() # you have to call this at the start,
# if you want to use this module. # if you want to use this module.
myfont = pygame.font.SysFont('Comic Sans MS', 55) myfont = pygame.font.SysFont('Comic Sans MS', 55)
myfont_small = pygame.font.SysFont('Comic Sans MS', 45)
P0_text = myfont.render('P0', False, (0, 0, 0)) P0_text = myfont.render('P0', False, (0, 0, 0))
game_over_text = myfont.render('GAME OVER', False, RED) game_over_text = myfont.render('GAME OVER', False, RED)
won_text = myfont.render('YOU WON', False, GREEN)
run_text = myfont.render('RUN', False, tuple(BLACK)) run_text = myfont.render('RUN', False, tuple(BLACK))
stop_text = myfont_small.render('STOP', False, tuple(BLACK))
random.seed(0) random.seed(42)
dimx = 5 dimx = 7
dimy = 10 dimy = 4
scale_fac = 50 scale_fac = 180
screen = pygame.display.set_mode((dimx*scale_fac+200,dimy*scale_fac+300)) screen = pygame.display.set_mode((dimx*scale_fac,dimy*scale_fac))
#screen = pygame.display.set_mode((dimx*scale_fac+int(0.1*scale_fac),dimy*scale_fac+300))
tiledt = np.dtype([('x', np.uint8), ('y', np.uint8), ('color', np.uint8, 3), ('star', np.bool)]) tiledt = np.dtype([('x', np.uint8), ('y', np.uint8), ('color', np.uint8, 3), ('star', np.bool)])
class Board: class Board:
valid_colors = [WHITE, RED, BLUE] valid_colors = [WHITE, RED, BLUE]
@ -160,16 +165,22 @@ class Program:
self.robot = robot self.robot = robot
self.board = board self.board = board
self.initial_pos = (self.robot.x, self.robot.y, self.robot.orientation)
self.state = 'input'
self.available_inputs = [Command('forward'), Command('left'), Command('right'), Command('P0'), self.available_inputs = [Command('forward'), Command('left'), Command('right'), Command('P0'),
Command('-', color=RED), Command('-', color=BLUE), Command('-', color=WHITE)] Command('-', color=RED), Command('-', color=BLUE), Command('-', color=WHITE)]
self.fullscreen = False
def input_program(self): def input_program(self):
self.state = 'input'
selected_cmd = 0 selected_cmd = 0
self.render(selected_cmd) self.render(selected_cmd)
start = False while self.state == 'input':
while not start:
# get all events # get all events
ev = pygame.event.get() ev = pygame.event.get()
@ -196,18 +207,34 @@ class Program:
self.cmds.append(copy.copy(chosen_input_cmd)) self.cmds.append(copy.copy(chosen_input_cmd))
if pos[0] >= 325 and pos[0] <= 400 and pos[1] >= 600 and pos[1] <= 650: if pos[0] >= 325 and pos[0] <= 400 and pos[1] >= 600 and pos[1] <= 650:
print(f"clicked at pos = {pos}") print(f"clicked at pos = {pos}")
return self.state = 'running'
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
if not self.fullscreen:
os.environ['SDL_VIDEO_WINDOW_POS'] = '1920, 280'
screen = pygame.display.set_mode((dimx * scale_fac, dimy * scale_fac),
pygame.NOFRAME)
self.fullscreen = True
else:
os.environ['SDL_VIDEO_WINDOW_POS'] = '0, 0'
screen = pygame.display.set_mode((dimx * scale_fac, dimy * scale_fac))
self.fullscreen = False
self.render(selected_cmd) self.render(selected_cmd)
pygame.time.wait(100)
self.run()
def run(self): def run(self):
running = True
prg_counter = 0 prg_counter = 0
self.render(prg_counter) self.render(prg_counter)
while running: self.state = 'running'
while self.state == 'running':
cmd = self.cmds[prg_counter] cmd = self.cmds[prg_counter]
prg_counter += 1 prg_counter += 1
@ -231,21 +258,48 @@ class Program:
else: else:
print("color not matching -> skipping command") print("color not matching -> skipping command")
# get all events
ev = pygame.event.get()
# proceed events
for event in ev:
# handle MOUSEBUTTONUP
if event.type == pygame.MOUSEBUTTONUP:
pos = pygame.mouse.get_pos()
if pos[0] >= 325 and pos[0] <= 400 and pos[1] >= 600 and pos[1] <= 650:
print(f"clicked at pos = {pos}")
self.state = 'input'
self.render(prg_counter) self.render(prg_counter)
if (not (0 <= r.x < self.board.tiles.shape[1])) or not (0 <= r.y < self.board.tiles.shape[0]): if (not (0 <= r.x < self.board.tiles.shape[1])) or not (0 <= r.y < self.board.tiles.shape[0]):
print("GAME OVER") print("GAME OVER")
screen.blit(game_over_text, (50, 00)) screen.blit(game_over_text, (50, 00))
pygame.display.update() pygame.display.update()
running = False pygame.time.wait(1500)
self.state = 'input'
else:
tile = self.board.tiles[r.y,r.x]
if tile['star']:
tile['star'] = False
time.sleep(0.1) if all([not t['star'] for t in self.board.tiles.flatten()]):
print("YOU WON")
screen.blit(won_text, (50, 00))
pygame.display.update()
pygame.time.wait(1500)
self.state = 'input'
pygame.time.wait(100)
self.robot.x = self.initial_pos[0]
self.robot.y = self.initial_pos[1]
self.robot.orientation = self.initial_pos[2]
self.input_program()
def render(self, prg_counter=None): def render(self, prg_counter=None):
dx = 50 dx = 0
dy = 50 dy = 0
screen.fill(tuple(BLACK)) screen.fill(tuple(BLACK))
board_surf = self.board.render() board_surf = self.board.render()
screen.blit(board_surf, (dx, dy, dx + dimx * scale_fac, dy + dimy * scale_fac)) screen.blit(board_surf, (dx, dy, dx + dimx * scale_fac, dy + dimy * scale_fac))
@ -256,10 +310,14 @@ class Program:
inp_surf = self.render_inputs() inp_surf = self.render_inputs()
screen.blit(inp_surf, (dx, board_surf.get_height()+4*dy, inp_surf.get_width(), inp_surf.get_height())) screen.blit(inp_surf, (dx, board_surf.get_height()+4*dy, inp_surf.get_width(), inp_surf.get_height()))
run_surf = pygame.Surface((80, 50)) btn_surf = pygame.Surface((80, 50))
run_surf.fill(tuple(GREEN)) if self.state == 'input':
run_surf.blit(run_text, (0,10)) btn_surf.fill(tuple(GREEN))
screen.blit(run_surf, (325, 600, run_surf.get_height(), run_surf.get_width())) btn_surf.blit(run_text, (0, 10))
elif self.state == 'running':
btn_surf.fill(tuple(RED))
btn_surf.blit(stop_text, (0, 10))
screen.blit(btn_surf, (325, board_surf.get_height()+2*scale_fac, btn_surf.get_height(), btn_surf.get_width()))
pygame.display.update() pygame.display.update()
@ -288,7 +346,9 @@ class Program:
r = Robot(x=1, y=1, orientation='v') r = Robot(x=1, y=1, orientation='v')
b = Board(dimx,dimy, r) b = Board(dimx,dimy, r)
b.tiles[3,4]['star'] = True b.tiles[3,3]['star'] = True
b.tiles[3,2]['star'] = True
print(b) print(b)