Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ def main():

logging.basicConfig(level=getattr(logging, args.log.upper()))

loop = asyncio.get_event_loop()
loop.run_until_complete(
asyncio.run(
server.run(
args.port,
args.listen,
Expand Down
16 changes: 16 additions & 0 deletions rose/engine/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self, name, car, lane):
self.breaks = None
self.jumps = None
self.collisions = None
self.pinguin_cnt = 0
self.reset()

def reset(self):
Expand All @@ -53,6 +54,7 @@ def reset(self):
self.breaks = 0
self.collisions = 0
self.jumps = 0
self.pinguin_cnt = 0

def __cmp__(self, other):
x = self.score
Expand All @@ -66,6 +68,19 @@ def in_lane(self):
current_lane = self.x // config.cells_per_player
return current_lane == self.lane

def adjacent_lanes(self):
"""
Return the absolute x-coordinates of all other routes within my
current lane-zone (my lane's cells_per_player cells), ordered by
distance from my current position (nearest first).
"""
current_lane = self.x // config.cells_per_player
lane_start = current_lane * config.cells_per_player
lane_end = lane_start + config.cells_per_player

others = [x for x in range(lane_start, lane_end) if x != self.x]
return sorted(others, key=lambda x: abs(x - self.x))

def state(self):
"""Return read only serialize-able state for sending to client"""
return {
Expand All @@ -84,4 +99,5 @@ def state(self):
"breaks": self.breaks,
"jumps": self.jumps,
"collisions": self.collisions,
"pinguin_cnt": self.pinguin_cnt,
}
25 changes: 20 additions & 5 deletions rose/engine/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ def process(players, track):
if obstacle == obstacles.NONE:
# Move forward, leaving the obstacle on the track.
player.score += config.score_move_forward
adj_lanes = player.adjacent_lanes()
for i in adj_lanes:
obs = track.get(i, player.y)
if obs == obstacles.PENGUIN:
player.pinguin_cnt = 0

log.debug(
"player %s hit no obstacle: got %d points",
Expand All @@ -70,6 +75,7 @@ def process(players, track):
player.y += 1
player.score += config.score_move_backward
player.hits += 1
player.pinguin_cnt = 0

log.debug(
"player %s hit %s: lost %d points, moved back to %d,%d",
Expand All @@ -86,7 +92,7 @@ def process(players, track):
points = config.score_move_forward + config.score_jump
player.score += points
player.jumps += 1

player.pinguin_cnt = 0
log.debug(
"player %s avoided %s: got %d points",
player.name,
Expand All @@ -99,6 +105,7 @@ def process(players, track):
player.y += 1
player.score += config.score_move_backward
player.hits += 1
player.pinguin_cnt = 0

log.debug(
"player %s hit %s: lost %d points, moved back to %d,%d",
Expand All @@ -115,7 +122,7 @@ def process(players, track):
points = config.score_move_forward + config.score_brake
player.score += points
player.breaks += 1

player.pinguin_cnt = 0
log.debug(
"player %s avoided %s: got %d points",
player.name,
Expand All @@ -128,6 +135,7 @@ def process(players, track):
player.y += 1
player.score += config.score_move_backward
player.hits += 1
player.pinguin_cnt = 0

log.debug(
"player %s hit %s: lost %d points, moved back to %d,%d",
Expand All @@ -142,10 +150,10 @@ def process(players, track):
if player.action == actions.PICKUP:
# Move forward and collect an aquatic bird
track.clear(player.x, player.y)
points = config.score_move_forward + config.score_pickup
player.pinguin_cnt += 1
points = config.score_move_forward + calc_pinguin_combo(config.score_pickup , player.pinguin_cnt)
player.score += points
player.pickups += 1

log.debug(
"player %s picked up %s: got %d points",
player.name,
Expand All @@ -155,7 +163,7 @@ def process(players, track):
else:
# Move forward leaving the obstacle on the track
player.score += config.score_move_forward

player.pinguin_cnt = 0
log.debug("player %s missed %s", player.name, obstacle)

# Here we can end the game when player gets out of
Expand All @@ -174,6 +182,7 @@ def process(players, track):
player.x -= 1
elif player.x < config.matrix_width - 1:
player.x += 1
player.pinguin_cnt = 0

log.debug(
"player %s collision at %d,%d: lost %d points, " "moved to %d,%d",
Expand All @@ -200,3 +209,9 @@ def process(players, track):
player.score,
player.response_time,
)


def calc_pinguin_combo(base, pcnt):
if pcnt < 1:
return base
return base + pcnt - 1
4 changes: 0 additions & 4 deletions rose/engine/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ async def admin_handler(request):
Returns:
aiohttp.web.Response: A response indicating the new game rate or an error message.
"""
global state

rate = request.rel_url.query.get("rate")
if rate:
try:
Expand Down Expand Up @@ -110,8 +108,6 @@ async def run(
theme (str): Path to the static them resources directory.
track_type (str): Type of track can be "random" or "same".
"""
global state

state["rate"] = initial_rate
state["running"] = 1 if initial_running else 0
state["drivers"] = initial_drivers
Expand Down