# Author: Prof. Mark Sherriff # Modified from example by Prof. Mark Sherriff # goal # feedback - achieve the goal # behavior import pygame import gamebox import random camera = gamebox.Camera(800, 600) character = gamebox.from_color(camera.x, camera.y, "red", 20, 40) # coins = [gamebox.from_color(300, 450, "yellow", 12, 12)] # create coin at random location every time a tick function is called coins = [gamebox.from_color(random.randint(50, 750), -10, "yellow", 12, 12)] bonus = gamebox.from_color(camera.x-100, camera.y, "green", 40, 40) walls = [ gamebox.from_color(400, 600, "black", 1000, 100) # ground ] # platforms = [ # gamebox.from_color(random.randrange(0, 800), random.randrange(0,600), "black", # random.randrange(70, 400), 20) # obstacle # ] platforms = [] for y in range(0, 600, 150): platforms.append(gamebox.from_color(random.randrange(0, 800), y, 'black', random.randrange(100, 500), 20)) character.yspeed = 0 time = 900 # 9000 # 5mins * 60seconds * 30ticks score = 0 def tick(keys): global time, score # clear display and set color camera.clear("blue") # -----------------------# # decrease timer (update the state of the game ) time -= 1 # calculate minutes, seconds, fractions of seconds frac = str(int((time%ticks_per_second)/ticks_per_second*10)) seconds = str(int((time/ticks_per_second)%60)).zfill(2) minutes = str(int((time/ticks_per_second)/60)) timer = gamebox.from_text(650,100, "Time remaining " + minutes+":"+seconds+":"+frac, 24, "white") # -----------------------# # handle player's movement if pygame.K_RIGHT in keys: # if a right arrow is pressed, do something character.x += 10 if pygame.K_LEFT in keys: character.x -= 10 character.yspeed += 1 character.y = character.y + character.yspeed # -----------------------# # reset our character to ensure it's on the screen if character.x < 0: character.x = 800 elif character.x > 800: character.x = 0 # -----------------------# # let's move platforms for platform in platforms: platform.y += 3 # if a platform goes below the screen, reuse it if platform.top > camera.bottom + 20: # 20 to give a bit space platform.y = -50 # move this wall above the camera so we can reuse it platform.x = random.randrange(0, 800) # randomly pick x platform.size = random.randrange(70, 400), 20 # randomly pick width # if the bottom of the character touches the platform # allow it to jump if character.bottom_touches(platform): character.yspeed = 0 if pygame.K_SPACE in keys: # if a spacebar is pressed, jump character.yspeed = -15 # handle collision detection if character.touches(platform): character.move_to_stop_overlapping(platform) camera.draw(platform) #-----------------------# # draw ground for wall in walls: # if the bottom of the character touches a wall, make it stays on the wall # and allows it to jump if character.bottom_touches(wall): character.yspeed = 0 if pygame.K_SPACE in keys: # if a spacebar is pressed, jump character.yspeed = -15 if character.touches(wall): # handle collision detection character.move_to_stop_overlapping(wall) camera.draw(wall) # -----------------------# # draw coins (collectible objects) for coin in coins: coin.speedy += 0.03 # acceleration on horizontal direction if character.touches(coin): score += 1 # if a character touches a coin, increment scores coins.remove(coin) # remove that coin from the list to avoid repeatedly increase score else: coin.move_speed() # move the coin with the current speed camera.draw(coin) # -----------------------# # to make sure that we won't run out of coins if len(coins) < 2: new_coin = gamebox.from_color(random.randint(50,750), -10, "yellow", 12, 12) coins.append(new_coin) score_box = gamebox.from_text(100, 100, "Score " + str(score), 24, "white") # -----------------------# # let's say this is a big prize, a character needs to jump up till it reaches this camera.draw(bonus) # -----------------------# camera.draw(character) camera.draw(timer) camera.draw(score_box) # -----------------------# # set time limit if time <= 0: camera.draw(gamebox.from_text(400, 300, "Game over !!!", "Arial", 70, "white")) gamebox.pause() # if a character touches the bonus prize, win the game if character.touches(bonus): camera.draw(gamebox.from_text(camera.x, camera.y, "You won !!!", "Arial", 70, "green")) gamebox.pause() # -----------------------# camera.display() # usually, keep this line at the end of the function # -----------------------# ticks_per_second = 30 gamebox.timer_loop(ticks_per_second, tick) # timer_loop makes the game run