Skip to content

Commit

Permalink
developer mode with pause button
Browse files Browse the repository at this point in the history
  • Loading branch information
itayfree committed Aug 10, 2023
1 parent 4589e77 commit e9941d1
Show file tree
Hide file tree
Showing 8 changed files with 764 additions and 2 deletions.
76 changes: 76 additions & 0 deletions mydriver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""
This driver does not do any action.
"""
from rose.common import obstacles, actions # NOQA

driver_name = "me"
bad = [obstacles.BIKE, obstacles.BARRIER, obstacles.TRASH]
good = [obstacles.WATER, obstacles.PENGUIN, obstacles.CRACK]
better = [obstacles.PENGUIN, obstacles.CRACK]
best = [obstacles.NONE, obstacles.PENGUIN]


def betterlane(world, x, y, lane):
obstacle = world.get((x, y - 1))
if lane == 0:
if world.get((x - 1, y - 2)) in good and world.get((x - 1, y - 1)) in best and world.get(
(x, y - 3)) != obstacles.PENGUIN:
return actions.LEFT
elif world.get((x + 1, y - 2)) in good and world.get((x + 1, y - 1)) in best and world.get(
(x, y - 3)) != obstacles.PENGUIN:
return actions.RIGHT
elif world.get((x, y - 1)) in bad:
for i in range(3, 6):
if world.get((x - 1, y - i)) in good and world.get((x + 1, y - i)) != obstacles.PENGUIN:
return actions.LEFT
else:
return actions.RIGHT
else:
return actions.NONE
if lane == 1:
if world.get((x - 1, y - 1)) in best and world.get((x, y - 2)) not in good:
return actions.LEFT
else:
return actions.NONE
if lane == 2:
if world.get((x + 1, y - 1)) in best and world.get((x, y - 2)) not in good:
return actions.RIGHT
else:
return actions.NONE


def drive(world):
x = world.car.x
y = world.car.y
obstacle = world.get((x, y - 1))
if x == 1 or x == 4:
lane = 0 # 0=in middle, 1=in right, 2= in left
elif x == 2 or x == 5:
lane = 1
else:
lane = 2
if obstacle == obstacles.PENGUIN:
return actions.PICKUP
if obstacle == obstacles.CRACK:
if lane < 2 and world.get((x - 1, y - 2)) == obstacles.PENGUIN:
return actions.LEFT
elif lane % 2 == 0 and world.get((x + 1, y - 2)) in obstacles.PENGUIN:
return actions.RIGHT
else:
return actions.JUMP
if obstacle == obstacles.WATER:
if lane < 2 and world.get((x - 1, y - 2)) in better:
return actions.LEFT
elif lane % 2 == 0 and world.get((x + 1, y - 2)) in better:
return actions.RIGHT
else:
return actions.BRAKE

if obstacle in bad:
if lane == 0:
return betterlane(world, x, y, lane)
if lane == 1:
return actions.LEFT
if lane == 2:
return actions.RIGHT
return betterlane(world, x, y, lane)
2 changes: 1 addition & 1 deletion rose/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
game_duration = 60
number_of_cars = 4
is_track_random = True

is_dev_mode = False
# Matrix

matrix_height = 9
Expand Down
5 changes: 4 additions & 1 deletion rose/server/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ def rate(self, value):
self._rate = value
if self.started:
self.looper.stop()
self.looper.start(1.0 / self._rate)
if self._rate == 0:
self.looper.start(3600 * 24)
else:
self.looper.start(1.0 / self._rate)
else:
self.update_clients()

Expand Down
11 changes: 11 additions & 0 deletions rose/server/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import socket
import logging
import argparse
import os

from twisted.internet import reactor
from twisted.web import server, static
Expand All @@ -20,6 +21,8 @@ def main():
default="random", choices=["random", "same"],
help="Definition of driver tracks: random or same."
"If not specified, random will be used.")
parser.add_argument("--dev", "-d", dest="dev_definition", action='store_true',
help="Set development mode")

args = parser.parse_args()
"""
Expand All @@ -32,6 +35,14 @@ def main():
else:
config.is_track_random = True

install_dir = os.path.dirname(__file__)
if args.dev_definition:
config.web_root = os.path.join(install_dir, "../web-dev")
else:
config.web_root = os.path.join(install_dir, "../web")

config.is_dev_mode = args.dev_definition

log.info('starting server')
g = game.Game()
h = net.Hub(g)
Expand Down
137 changes: 137 additions & 0 deletions rose/web-dev/game.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
body {
font-family: sans-serif;
background: #444;
color: #ccc;
margin: 0;
}

#content {
margin: 0 auto;
width: 910px;
}

#control {
position: relative;
margin: 10px 0;
font-weight: bold;
}

#control button {
font-family: helvetica, sans-serif;
font-size: 20px;
line-height: 24px;
font-weight: bold;
border: 1px solid #666;
color: #ccc;
background-color: #333;
}

#control button:disabled, #control button:disabled:hover {
color: #666;
background-color: #333;
}

#control button:hover {
background-color: #222;
}

#start, #stop, #music_ctl {
width: 120px;
padding: 8px 24px;
margin: 0;
}

#pausegame {
width: 120px;
padding: 8px 0px;
margin: 0;
}

#rate_ctl {
position: absolute;
top: 0;
right: 0;
}

#rate_ctl label {
margin: 0 10px;
}

#rate_ctl .group {
display: inline-block;
font-size: 0;
border: 1px solid #666;
}

#rate_ctl button {
height: 40px;
padding: 0;
margin: 0;
}

#inc_rate, #dec_rate {
width: 50px;
border: 0;
}

#cur_rate {
width: 120px;
border-top: 0;
border-bottom: 0;
}

#players {
background-image: url('../res/dashboard/dashboard.png');
width: 910px;
height: 150px;
font-weight: bold;
font-family: sans-serif;
color: rgb(153, 153, 153);
position: relative;
}

.player {
width: 350px;
height: 100px;
top: 20px;
position: absolute;
}

#left.player {
left: 10px;
}

#right.player {
right: 10px;
}

.player .name {
font-size: 30px;
width: 100%;
height: 50px;
text-align: center;
position: absolute;
overflow: hidden;
top: 7px;
}

.score {
width: 100%;
text-align:center;
position: absolute;
font-size: 40px;
top: 50px;
}

#time_left {
width: 105px;
font-size: 50px;
text-align:center;
position: absolute;
top: 40px;
left: 400px;
}

#game {
border: 1px #666 solid;
}
Loading

0 comments on commit e9941d1

Please sign in to comment.