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
|
||||
# Pause simulation
|
||||
elif event.key == pygame.K_p:
|
||||
if update:
|
||||
update = False
|
||||
else:
|
||||
update = True
|
||||
update = not update
|
||||
# Display plot if simulation is not running
|
||||
elif event.key == pygame.K_g:
|
||||
if pendulum.fallen:
|
||||
pendulum.plot()
|
||||
# Toggle PID controller
|
||||
elif event.key == pygame.K_c:
|
||||
if pendulum.pid:
|
||||
pendulum.pid = False
|
||||
else:
|
||||
pendulum.pid = True
|
||||
pendulum.pid = not pendulum.pid
|
||||
|
||||
# Move cart
|
||||
keys = pygame.key.get_pressed()
|
||||
@ -93,14 +87,14 @@ while running:
|
||||
rt += dt
|
||||
pendulum.update(dt)
|
||||
else:
|
||||
ui.wasted()
|
||||
ui.gameover(rt)
|
||||
|
||||
# Update highscore
|
||||
if rt > highscore:
|
||||
highscore = rt
|
||||
|
||||
# Draw metadata
|
||||
ui.update()
|
||||
ui.update(dt)
|
||||
meta()
|
||||
|
||||
# Draw pendulum
|
||||
|
@ -3,7 +3,8 @@ import pygame
|
||||
# Constants
|
||||
C_GRID_L_VALUE = 200
|
||||
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)
|
||||
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.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):
|
||||
# Capture values to display status
|
||||
if desc == "Control":
|
||||
self.controlled = val
|
||||
if desc == "Paused":
|
||||
self.paused = val
|
||||
|
||||
# Print em
|
||||
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):
|
||||
# Clear the screen
|
||||
self.screen.fill("white")
|
||||
|
||||
# Drawing offsets so the grid aligns with the pole
|
||||
cXoff = self.pole.x % dist
|
||||
cYoff = self.pole.y % dist
|
||||
|
||||
# Draw the grid
|
||||
for i in range(0, 1280, dist):
|
||||
pygame.draw.line(
|
||||
self.screen,
|
||||
@ -46,6 +64,7 @@ class SimUI:
|
||||
1,
|
||||
)
|
||||
|
||||
# Draw the center lines darker
|
||||
pygame.draw.line(
|
||||
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))
|
||||
self.screen.blit(textObj, text_rect)
|
||||
|
||||
def wasted(self):
|
||||
def gameover(self, time):
|
||||
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 G to view nerd graphs", "black", 45)
|
||||
|
||||
def update(self):
|
||||
def update(self, dt):
|
||||
# Credits
|
||||
self.screen.blit(
|
||||
font_h.render("Pendulum simulator 4000", True, "black"), (10, 10)
|
||||
)
|
||||
self.screen.blit(
|
||||
font_m.render("Arne van Iterson, 2023", True, "black"), (1150, 700)
|
||||
)
|
||||
|
||||
# Reset meta printing
|
||||
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