Very important status indicators
(math is still wrong dont worry about it)
This commit is contained in:
parent
026a42d64c
commit
dc233122a2
@ -1,2 +0,0 @@
|
|||||||
class PID:
|
|
||||||
def __init__:
|
|
@ -62,20 +62,14 @@ while running:
|
|||||||
rt = 0
|
rt = 0
|
||||||
# Pause simulation
|
# Pause simulation
|
||||||
elif event.key == pygame.K_p:
|
elif event.key == pygame.K_p:
|
||||||
if update:
|
update = not update
|
||||||
update = False
|
|
||||||
else:
|
|
||||||
update = True
|
|
||||||
# Display plot if simulation is not running
|
# Display plot if simulation is not running
|
||||||
elif event.key == pygame.K_g:
|
elif event.key == pygame.K_g:
|
||||||
if pendulum.fallen:
|
if pendulum.fallen:
|
||||||
pendulum.plot()
|
pendulum.plot()
|
||||||
# Toggle PID controller
|
# Toggle PID controller
|
||||||
elif event.key == pygame.K_c:
|
elif event.key == pygame.K_c:
|
||||||
if pendulum.pid:
|
pendulum.pid = not pendulum.pid
|
||||||
pendulum.pid = False
|
|
||||||
else:
|
|
||||||
pendulum.pid = True
|
|
||||||
|
|
||||||
# Move cart
|
# Move cart
|
||||||
keys = pygame.key.get_pressed()
|
keys = pygame.key.get_pressed()
|
||||||
@ -93,14 +87,14 @@ while running:
|
|||||||
rt += dt
|
rt += dt
|
||||||
pendulum.update(dt)
|
pendulum.update(dt)
|
||||||
else:
|
else:
|
||||||
ui.wasted()
|
ui.gameover(rt)
|
||||||
|
|
||||||
# Update highscore
|
# Update highscore
|
||||||
if rt > highscore:
|
if rt > highscore:
|
||||||
highscore = rt
|
highscore = rt
|
||||||
|
|
||||||
# Draw metadata
|
# Draw metadata
|
||||||
ui.update()
|
ui.update(dt)
|
||||||
meta()
|
meta()
|
||||||
|
|
||||||
# Draw pendulum
|
# Draw pendulum
|
||||||
|
@ -3,7 +3,8 @@ import pygame
|
|||||||
# Constants
|
# Constants
|
||||||
C_GRID_L_VALUE = 200
|
C_GRID_L_VALUE = 200
|
||||||
C_GRID_D_VALUE = 100
|
C_GRID_D_VALUE = 100
|
||||||
C_MPLOT_START = 50
|
C_MPLOT_START = 700
|
||||||
|
C_BLINK_TIME = 500
|
||||||
|
|
||||||
gridLight = pygame.Color(C_GRID_L_VALUE, C_GRID_L_VALUE, C_GRID_L_VALUE)
|
gridLight = pygame.Color(C_GRID_L_VALUE, C_GRID_L_VALUE, C_GRID_L_VALUE)
|
||||||
gridDark = pygame.Color(C_GRID_D_VALUE, C_GRID_D_VALUE, C_GRID_D_VALUE)
|
gridDark = pygame.Color(C_GRID_D_VALUE, C_GRID_D_VALUE, C_GRID_D_VALUE)
|
||||||
@ -17,19 +18,36 @@ class SimUI:
|
|||||||
self.screen = screen
|
self.screen = screen
|
||||||
self.pole = pole
|
self.pole = pole
|
||||||
|
|
||||||
self.metaPlotY = 50
|
self.metaPlotY = C_MPLOT_START
|
||||||
|
|
||||||
|
self.blink = False
|
||||||
|
self.blinkTimer = 0
|
||||||
|
|
||||||
|
self.controlled = False
|
||||||
|
self.paused = False
|
||||||
|
|
||||||
def meta(self, val, desc):
|
def meta(self, val, desc):
|
||||||
|
# Capture values to display status
|
||||||
|
if desc == "Control":
|
||||||
|
self.controlled = val
|
||||||
|
if desc == "Paused":
|
||||||
|
self.paused = val
|
||||||
|
|
||||||
|
# Print em
|
||||||
self.screen.blit(
|
self.screen.blit(
|
||||||
font_m.render(f"{desc} = {val}", True, "black"), (15, self.metaPlotY)
|
font_m.render(f"{desc} = {val}", True, "black"), (10, self.metaPlotY)
|
||||||
)
|
)
|
||||||
self.metaPlotY += 15
|
self.metaPlotY -= 15
|
||||||
|
|
||||||
def grid(self, dist, Xoff=0, Yoff=0):
|
def grid(self, dist, Xoff=0, Yoff=0):
|
||||||
|
# Clear the screen
|
||||||
self.screen.fill("white")
|
self.screen.fill("white")
|
||||||
|
|
||||||
|
# Drawing offsets so the grid aligns with the pole
|
||||||
cXoff = self.pole.x % dist
|
cXoff = self.pole.x % dist
|
||||||
cYoff = self.pole.y % dist
|
cYoff = self.pole.y % dist
|
||||||
|
|
||||||
|
# Draw the grid
|
||||||
for i in range(0, 1280, dist):
|
for i in range(0, 1280, dist):
|
||||||
pygame.draw.line(
|
pygame.draw.line(
|
||||||
self.screen,
|
self.screen,
|
||||||
@ -46,6 +64,7 @@ class SimUI:
|
|||||||
1,
|
1,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Draw the center lines darker
|
||||||
pygame.draw.line(
|
pygame.draw.line(
|
||||||
self.screen, gridDark, (self.pole.x + Xoff, 0), (self.pole.x + Xoff, 720), 1
|
self.screen, gridDark, (self.pole.x + Xoff, 0), (self.pole.x + Xoff, 720), 1
|
||||||
)
|
)
|
||||||
@ -62,17 +81,40 @@ class SimUI:
|
|||||||
text_rect = textObj.get_rect(center=(1280 / 2, 720 / 2 - y))
|
text_rect = textObj.get_rect(center=(1280 / 2, 720 / 2 - y))
|
||||||
self.screen.blit(textObj, text_rect)
|
self.screen.blit(textObj, text_rect)
|
||||||
|
|
||||||
def wasted(self):
|
def gameover(self, time):
|
||||||
font_g = pygame.font.SysFont(None, 128)
|
font_g = pygame.font.SysFont(None, 128)
|
||||||
self.centeredText(font_g, "WASTED", "red", 100)
|
self.centeredText(font_g, "WASTED", "red", 120)
|
||||||
|
self.centeredText(font_m, f"You controlled the pendulum for {time / 1000} seconds", "black", 80)
|
||||||
|
|
||||||
self.centeredText(font_m, "Press space to restart", "black", 60)
|
self.centeredText(font_m, "Press space to restart", "black", 60)
|
||||||
self.centeredText(font_m, "Press G to view nerd graphs", "black", 45)
|
self.centeredText(font_m, "Press G to view nerd graphs", "black", 45)
|
||||||
|
|
||||||
def update(self):
|
def update(self, dt):
|
||||||
|
# Credits
|
||||||
self.screen.blit(
|
self.screen.blit(
|
||||||
font_h.render("Pendulum simulator 4000", True, "black"), (10, 10)
|
font_h.render("Pendulum simulator 4000", True, "black"), (10, 10)
|
||||||
)
|
)
|
||||||
self.screen.blit(
|
self.screen.blit(
|
||||||
font_m.render("Arne van Iterson, 2023", True, "black"), (1150, 700)
|
font_m.render("Arne van Iterson, 2023", True, "black"), (1150, 700)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Reset meta printing
|
||||||
self.metaPlotY = C_MPLOT_START
|
self.metaPlotY = C_MPLOT_START
|
||||||
|
|
||||||
|
# Draw current status blinking
|
||||||
|
if self.blink:
|
||||||
|
if self.paused:
|
||||||
|
text = "Paused"
|
||||||
|
elif self.controlled:
|
||||||
|
text = "Auto mode"
|
||||||
|
else:
|
||||||
|
text = "Manual mode"
|
||||||
|
|
||||||
|
textObj = font_h.render(text, True, "black")
|
||||||
|
text_rect = textObj.get_rect(right=1270, top=10)
|
||||||
|
self.screen.blit(textObj, text_rect)
|
||||||
|
|
||||||
|
if self.blinkTimer > C_BLINK_TIME:
|
||||||
|
self.blink = not self.blink
|
||||||
|
self.blinkTimer = 0
|
||||||
|
self.blinkTimer += dt
|
||||||
|
Loading…
Reference in New Issue
Block a user