Files
flippr-game/GameState.py
Jonas Zeunert bfdae47e8b kp
2022-09-03 19:03:56 +02:00

165 lines
5.2 KiB
Python

import logging
import config
from lamps.Lamp import CREDIT
from itertools import cycle
from lamps.LampGroup import *
import asyncio
import threading
from Sound import Sound
from sounds.YoureChamp import YoureChamp
from sounds.YoureGood import YoureGood
class GameState:
playerCount = 0
players = None
playerList = []
credits = config.BALLS_PER_GAME
isPlaying = False
isStarted = False
isIdle = True
idleThread = None
def __init__(self, playerStateFactory, highscore, specialDisplay, currentPlayerID = 0):
CREDIT.activate()
self.highscore = highscore
self.playerStateFactory = playerStateFactory
self.dummyPlayer = playerStateFactory.createDummyPlayerState()
self._currentPlayer = self.dummyPlayer
self.gameStateID = currentPlayerID
self.highscore = highscore
self.specialDisplay = specialDisplay
self.specialDisplay.printCredits(self.credits)
self.startIdleLoop()
def nextPlayer(self):
if not self.isStarted:
return
self.checkHighscore()
self.currentPlayer = next(self.players)
Lamp("Highest Score").deactivate()
if self.currentPlayer.hasHighScore:
Lamp("Highest Score").activate()
def checkHighscore(self):
highestScorePlayer = None
for player in self.playerList:
if player.points > self.highscore:
if not highestScorePlayer:
highestScorePlayer = player
else:
if highestScorePlayer.points < player.points:
highestScorePlayer = player
if highestScorePlayer:
self.highscore = highestScorePlayer.points
for player in self.playerList:
player.hasHighscore = False
highestScorePlayer.hasHighscore = True
logging.info("New Highscore from Player" + str(highestScorePlayer.id))
def startGame(self, playerCount):
self.isStarted = True
self.createPlayers(playerCount)
self.nextPlayer()
#self.isPlaying = True
logging.info("Game Started")
def endGame(self):
for player in self.playerList:
if player.hasHighscore:
YoureChamp().play()
self.checkHighscore()
self.writeHighscore()
self.players = None
self.playerList = []
self.playerCount = 0
self._currentPlayer = self.dummyPlayer
self.isPlaying = False
self.isStarted = False
logging.info("Game ended")
self.startIdleLoop()
def startIdleLoop(self):
logging.info("Starting Idle Loop")
for display in self.playerStateFactory.displays:
display.printScore(self.highscore)
self.playerStateFactory.specialDisplay.printContent("00000000")
PLAYER_LAMPS.deactivate()
self.isIdle = True
self.idleThread = threading.Thread(target=self.idleLoop)
self.idleThread.start()
def stopIdleLoop(self):
logging.info("Stopping Idle Loop")
self.isIdle = False
self.idleThread.join()
self.idleThread = None
self.deactivate_all()
def deactivate_all(self):
all_cabinet_lamps = [BONUS_LAMPS, BONUS_MULTIPLIER_LAMPS, CHAMP_LAMPS,
UPPER_PLAYFIELD_TIME_LAMPS, TUNNEL_NUMBER_LAMPS,
TUNNEL_LAMPS, TUNNEL_SCORE_LAMPS]
for lamp_group in all_cabinet_lamps:
lamp_group.deactivate()
def idleLoop(self):
all_cabinet_lamps = [BONUS_LAMPS, BONUS_MULTIPLIER_LAMPS, CHAMP_LAMPS,
UPPER_PLAYFIELD_TIME_LAMPS, TUNNEL_NUMBER_LAMPS,
TUNNEL_LAMPS, TUNNEL_SCORE_LAMPS, ORANGE_SPECIAL_LAMPS]
YoureGood().play()
while True:
for _ in range(20):
random_group = random.choice(all_cabinet_lamps)
random_group.toggleRandom(0.2)
if not self.isIdle:
return
for lamps in all_cabinet_lamps:
for lamp in lamps.lamps:
lamp.activate()
if not self.isIdle:
return
time.sleep(0.1)
self.deactivate_all()
def createPlayers(self, playerCount):
self.playerCount = playerCount
players = []
for i in range(playerCount):
player = self.playerStateFactory.createPlayerState(i)
players.append(player)
self.playerList = players
self.players = cycle(players)
@property
def currentPlayer(self):
return self._currentPlayer
@currentPlayer.setter
def currentPlayer(self, player):
logging.info("Set active player to player " + str(player.id))
self._currentPlayer = self.dummyPlayer
player.activate()
self._currentPlayer = player
def writeHighscore(self):
for player in self.playerList:
if player.hasHighscore:
with open(config.HIGHSCORE_FILE, "w+") as file:
file.write(str(player.points))
file.close()
self.highscore = player.points