04_views.py Diff
04_views.py
--- /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/latest/doc/tutorials/views/03_views.py
+++ /home/docs/checkouts/readthedocs.org/user_builds/arcade-library/checkouts/latest/doc/tutorials/views/04_views.py
@@ -21,17 +21,58 @@
# Reset the viewport, necessary if we have a scrolling game and we need
# to reset the viewport back to the start so we can see what we draw.
self.window.default_camera.use()
+ self.title_text = arcade.Text(
+ "Instructions Screen",
+ x=self.window.width / 2,
+ y=self.window.height / 2,
+ color=arcade.color.WHITE,
+ font_size=50,
+ anchor_x="center",
+ )
+ self.instruction_text = arcade.Text(
+ "Click to advance",
+ x=self.window.width / 2,
+ y=self.window.height / 2-75,
+ color=arcade.color.WHITE,
+ font_size=20,
+ anchor_x="center",
+ )
def on_draw(self):
""" Draw this view """
self.clear()
- arcade.draw_text("Instructions Screen", self.window.width / 2, self.window.height / 2,
- arcade.color.WHITE, font_size=50, anchor_x="center")
- arcade.draw_text("Click to advance", self.window.width / 2, self.window.height / 2-75,
- arcade.color.WHITE, font_size=20, anchor_x="center")
+ self.title_text.draw()
+ self.instruction_text.draw()
def on_mouse_press(self, _x, _y, _button, _modifiers):
""" If the user presses the mouse button, start the game. """
+ game_view = GameView()
+ game_view.setup()
+ self.window.show_view(game_view)
+
+
+class GameOverView(arcade.View):
+ """ View to show when game is over """
+
+ def __init__(self):
+ """ This is run once when we switch to this view """
+ super().__init__()
+ self.texture = arcade.load_texture("game_over.png")
+
+ # Reset the viewport, necessary if we have a scrolling game and we need
+ # to reset the viewport back to the start so we can see what we draw.
+ self.window.default_camera.use()
+
+ def on_draw(self):
+ """ Draw this view """
+ self.clear()
+ arcade.draw_texture_rect(
+ self.texture,
+ rect=arcade.LBWH(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT),
+ )
+
+ def on_mouse_press(self, _x, _y, _button, _modifiers):
+ """ If the user presses the mouse button, re-start the game. """
game_view = GameView()
game_view.setup()
self.window.show_view(game_view)
@@ -57,7 +98,7 @@
# Don't show the mouse cursor
self.window.set_mouse_visible(False)
- self.window.background_color = arcade.color.AMAZON
+ self.background_color = arcade.color.AMAZON
def setup(self):
""" Set up the game and initialize the variables. """
@@ -103,7 +144,6 @@
self.score_text.text = output
self.score_text.draw()
-
def on_mouse_motion(self, x, y, dx, dy):
""" Handle Mouse Motion """
@@ -126,6 +166,12 @@
coin.remove_from_sprite_lists()
self.score += 1
+ # Check length of coin list. If it is zero, flip to the
+ # game over view.
+ if len(self.coin_list) == 0:
+ view = GameOverView()
+ self.window.show_view(view)
+
def main():
""" Main function """