""" ======================================== Introduction to Computer Programming ======================================== -------------------------------------------- Programming Assignment 6 (solution) Game, Set, Timer! (game with wall sets and timer events) Jack Heseltine -------------------------------------------- """ import random import time import turtle # ################################################################# # game parameters # --------------- DEFAULT_COLUMNS = 30 DEFAULT_ROWS = 20 DEFAULT_WALL_DENSITY = 0.25 PIXELS_PER_CELL = 22 BACKGROUND_COLOR = 'white' PLAYER_COLOR = 'blue' HARD_WALL_COLOR = 'black' SOFT_WALL_COLOR = 'gray' WALL_COLLISION_COLOR = 'orange' WALL_COLLISION_DELAY = 0.2 # miliseconds FLAG_COLOR = 'green' STATUS_COLOR = 'black' INITIAL_SCORE = 50 BLINKER_INTERVAL = 500 # miliseconds TIME_LIMIT = 30 * 1000 # expressed to indicate seconds # ################################################################# # global variables # ---------------- columns = None rows = None window = None game_on = None start_time = None hard_walls = None soft_walls = None wall_turtle = None player_pos = None player_turtle = None moves = None score = None flag_pos = None flag_turtle = None blinker_active = None blinker_pos = None blinker_turtle = None status_bar = None # ################################################################# # core game functions and initializations # --------------------------------------- def play_game(c, r, d): global columns, rows, game_on, start_time columns = c rows = r initialize_window() initialize_walls(d) initialize_player() initialize_flag() initialize_blinker() initialize_status_bar() setup_events() # ... display_status() game_on = True # ... window.listen() window.mainloop() def initialize_window(): global window (window_width, window_height) = convert((columns, rows + 3)) window = turtle.Screen() window.setup(window_width, window_height, None, None) window.setworldcoordinates(0, window_height, window_width, 0) window.bgcolor(BACKGROUND_COLOR) window.title('Game, Set, Timer!') def initialize_walls(d): global hard_walls, soft_walls, wall_turtle hard_walls = set() soft_walls = set() # ... for i in range(rows): hard_walls.add((0, i)) hard_walls.add((columns - 1, i)) # ... for i in range(columns): hard_walls.add((i, 0)) hard_walls.add((i, rows - 1)) # ... for i in range(rows - 1): hard_walls.add((columns // 2, i)) hard_walls.discard((columns // 2, rows // 2)) ## ... for r in range(rows): for c in range (columns): if d > random.random() and (c, r) not in hard_walls: soft_walls.add((c, r)) wall_turtle = turtle.Turtle() wall_turtle.hideturtle() wall_turtle.shape('square') wall_turtle.penup() wall_turtle.speed(0) wall_turtle.color(HARD_WALL_COLOR) # ... for r in range(rows): for c in range (columns): if (c, r) in hard_walls: wall_turtle.goto(convert((c, r))) wall_turtle.stamp() wall_turtle.color(SOFT_WALL_COLOR) # ... for r in range(rows): for c in range (columns): if (c, r) in soft_walls: wall_turtle.goto(convert((c, r))) wall_turtle.stamp() def initialize_player(): global player_pos, player_turtle, moves, score player_pos = (random.randrange(1, columns // 2), # ... # to the left random.randrange(1, rows - 1)) # ... while player_pos in hard_walls or player_pos in soft_walls: player_pos = (random.randrange(1, columns // 2), random.randrange(1, rows - 1)) player_turtle = turtle.Turtle() player_turtle.shape('square') player_turtle.color(PLAYER_COLOR) player_turtle.penup() player_turtle.speed(0) player_turtle.goto(convert(player_pos)) moves = 0 score = INITIAL_SCORE def initialize_flag(): global flag_pos, flag_turtle flag_pos = (random.randrange(columns // 2 + 1, columns - 1), # ... # to the right (+ 1 for middle wall) random.randrange(1, rows - 1)) # ... while flag_pos in hard_walls or player_pos in soft_walls: flag_pos = (random.randrange(columns // 2 + 1, columns - 1), random.randrange(1, rows - 1)) flag_turtle = turtle.Turtle() flag_turtle.shape('arrow') flag_turtle.hideturtle() flag_turtle.color(FLAG_COLOR) flag_turtle.penup() flag_turtle.speed(0) flag_turtle.goto(convert(flag_pos)) flag_turtle.showturtle() def initialize_status_bar(): global status_bar status_bar = turtle.Turtle() status_bar.hideturtle() status_bar.penup() status_bar.pencolor(STATUS_COLOR) status_bar.goto(convert((2, rows + 1))) status_bar.pendown() def initialize_blinker(): global blinker_active, blinker_pos, blinker_turtle pass # ... def setup_events(): window.onkey(quit, 'q') window.onkey(go_east, 'Right') window.onkey(go_north, 'Up') window.onkey(go_south, 'Down') window.onkey(go_west, 'Left') # ... # ################################################################# # event-triggered actions # ----------------------- def quit(): game_over('You quit.') def go_east(): go(1, 0) def go_north(): go(0, -1) def go_south(): go(0, 1) def go_west(): go(-1, 0) def remove_wall(x, y): pass # ... def blink(): global blinker_active pass # ... # ################################################################# # functions supporting actions # ---------------------------- def go(dx, dy): global player_pos, final_message, moves, score new_pos = (player_pos[0] + dx, player_pos[1] + dy) if new_pos not in hard_walls and new_pos not in soft_walls: # ... # "and" instead of "or"! moves += 1 player_pos = new_pos player_turtle.goto(convert(player_pos)) if player_pos == flag_pos: game_over('You win!') # ... else: display_status() else: wall_turtle.goto(convert(new_pos)) wall_turtle.color(WALL_COLLISION_COLOR) wall_turtle.stamp() time.sleep(WALL_COLLISION_DELAY) if new_pos in soft_walls: # ... wall_turtle.color(SOFT_WALL_COLOR) else: # ... wall_turtle.color(HARD_WALL_COLOR) wall_turtle.stamp() def game_over(message): global game_on game_on = False print(message) print('You played for {} moves.'.format(moves)) print('You final score was {}.'.format(score)) print('The game is now over.') window.bye() def show_wall(p): wall_turtle.goto(convert(p)) wall_turtle.stamp() # this function will need to be edited ... def display_status(): template = 'score: {} distance: {} moves: {}' d = distance(player_pos, flag_pos) message = template.format(score, d, moves) status_bar.clear() status_bar.write(message, font=('Courier', 14, 'normal')) # ################################################################# # more general helper functions [all from previous assignments] # ----------------------------- def convert(p): half_cell = PIXELS_PER_CELL // 2 return (half_cell + p[0] * PIXELS_PER_CELL, half_cell + p[1] * PIXELS_PER_CELL) def inverse_convert(p): x = int(p[0]) y = int(p[1]) return (x // PIXELS_PER_CELL, y // PIXELS_PER_CELL) def distance(p0, p1): """Returns the Manhattan distance between coordinate pair p0 and coordinate pair p1. >>> distance((0, 0), (3, 4)) 7 >>> distance((13, 3), (11, 5)) 4 >>> distance((9, 4), (4, 9)) 10 >>> distance((4, 9), (4, 9)) 0 """ return abs(p1[0] - p0[0]) + abs(p1[1] - p0[1]) # ################################################################## # for starting game automatically upon load (from command line/IDLE) if __name__ == '__main__': play_game(DEFAULT_COLUMNS, DEFAULT_ROWS, DEFAULT_WALL_DENSITY)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question