Skip to content

Commit

Permalink
Added status panel with score, corrected level completion.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nameless committed Oct 5, 2012
1 parent 2eb1ab1 commit 9a2a91f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
30 changes: 29 additions & 1 deletion levelui.py
Expand Up @@ -32,6 +32,15 @@ def run(self):
self.canvas.focus_set()
self.canvas.pack()

# create the status panel
self.status_panel = Frame(self.root, width=800, height=30)
self.status_panel.pack()

self.score_text = StringVar()
self.score_text.set("Score: 0")
score_label = Label(self.status_panel, textvariable=self.score_text)
score_label.pack()

# create the sea
self.sea = self.canvas.create_rectangle(0, 200, 800, 600, fill="blue")
# and the sky
Expand All @@ -46,7 +55,8 @@ def run(self):
# these keypresses are forwarded to steer, the rest go to the game loop
self.directions = ["Left", "Right", "Up", "Down"]

self.stopped = True
self.stopped = True # submarine starts stopped
self.completed = False # level not completed

self.check_input()

Expand All @@ -65,9 +75,23 @@ def check_input(self):
self.destroy_ship(int(m.group(1)))
except ValueError:
pass
elif re.match(r'score (\d+)', event):
m = re.match(r'score (\d+)', event)
try:
self.set_score(int(m.group(1)))
except ValueError:
pass
elif event == "complete":
self.completed = True
except Empty:
pass
self.root.after(5, self.check_input)

def set_score(self, score):
self.score_text.set("Score: %d" % score)

def level_complete(self):
self.canvas.create_text(400,300, text="Level complete!", fill="white", font=("Arial", 16, "bold"))

def destroy_confirm(self):
if tkMessageBox.askokcancel("Quit", "Do you really wish to quit?"):
Expand Down Expand Up @@ -147,6 +171,8 @@ def move_ship(self, ship_id, speed):

if ship_id not in self.canvas.find_overlapping(*self.canvas.coords(self.sky)):
self.canvas.delete(ship_id)
if self.completed and not self.canvas.find_withtag("ship"):
self.level_complete()
else:
self.root.after(30, self.move_ship, ship_id, speed)

Expand Down Expand Up @@ -197,3 +223,5 @@ def ship(self):

def destroy_ship(self, ship_id):
self.canvas.delete(ship_id)
if self.completed and not self.canvas.find_withtag("ship"):
self.level_complete()
22 changes: 18 additions & 4 deletions main.py
Expand Up @@ -23,6 +23,7 @@ def __init__(self):
self.states = {
"menu": [Menu],
"level1": [Level, 1],
"level2": [Level, 2],
"exit": [Exit]
}

Expand All @@ -31,7 +32,13 @@ def __init__(self):
"start": "level1",
"quit": "exit"
},
"level1": { "quit": "exit" }
"level1": {
"complete": "level2",
"quit": "exit"
},
"level2": {
"quit": "exit"
}
}

self.total_score = 0
Expand All @@ -58,6 +65,7 @@ def run(self):
event = self.in_queue.get_nowait()

if event in self.current_transitions():
self.out_queue.put("close")
self.total_score += self._current_state.score
self.change_state(event)
self.build_ui()
Expand Down Expand Up @@ -102,12 +110,16 @@ def handle(self, event):
# for now, we simply notify the UI to launch it
self.out_queue.put(event)
elif re.match(r'hit (\d+)', event):
self.score += self.score_per_ship
print "Score: %d" % self.score
self.increase_score()
self.out_queue.put(event)
else:
print event

def increase_score(self):
self.score += self.score_per_ship
self.out_queue.put("score %d" % self.score)


def ship_appearance(self):
self.out_queue.put("ship")

Expand All @@ -120,8 +132,10 @@ def ship_appearance(self):
self.ship_timer = Timer(nextTime, self.ship_appearance, ())
self.ship_timer.start()
else:
print "Level complete!"
self.level_complete()

def level_complete(self):
self.out_queue.put("complete")

class Exit:
# dummy method, will exit
Expand Down

0 comments on commit 9a2a91f

Please sign in to comment.