# start with a blank game # change window color # add timer (counting down) # add a character (from color) # check which arrow key is pressed, move the character in that direction #--------------------# # 1. beginning import pygame import gamebox # create a game window, # assign the instance to a variable called camera camera = gamebox.Camera(800, 600) # width=800, height=600 #--------------------# # 2. prep time = 9000 # 9000 / 60 seconds / 30 ticks per second = 5 minutes # create a character from color # make this guy appear at (camera.x, camera.y) adjustable coordinate # (camera.x, camera.y) result in the middle of the screen # in green with 20 pixels width, 20 pixels height # from_color(x, y, color, width, height) myguy = gamebox.from_color(camera.x, camera.y, 'green', 20, 20) #--------------------# # 3. method that will be called every frame. # named "tick" with a parameter "keys" def tick(keys): ''' This is where the game goes :param keys: recognizes what the user does with :return: doesn't return anything, it is actively running ''' # make time global so that we can access what's outside the function global time # time goes down every time tick runs, i.e., 30 times per second time -= 1 # set background color of the window camera.clear("darkblue") # prepare timer as a concatenation of minutes:seconds:frac # set fraction of a second as a string (counting 0,1,2,...,9) frac = str(int((time % ticks_per_second) / ticks_per_second*10)) # set seconds portion of the timer (counting 0,1,2,...,59) # seconds = str(int((time/ticks_per_second)%60)) # use .zfill(2) to fill a leading zero to make the seconds two digits seconds = str(int((time / ticks_per_second) % 60)).zfill(2) # set minutes minutes = str(int((time/ticks_per_second)/60)) # concate minutes, seconds, and fraction to form a string representation of a timer concat_timer = minutes + ":" + seconds + ":" + frac # create a gamebox thing from text timer = gamebox.from_text(100, 100, concat_timer, 30, "yellow") # from_text(x, y, text, fontsize, color, bold=False, italic=False) # draw a timer on the screen camera.draw(timer) # check which arrow key is pressed if pygame.K_RIGHT in keys: # if right arrow is pressed myguy.x += 5 # move the character to the right 5 pixels if pygame.K_LEFT in keys: # if left arrow is pressed myguy.x -= 5 # move the character to the left 5 pixels if pygame.K_UP in keys: # if up arrow is pressed myguy.y -= 5 # move the character up 5 pixels if pygame.K_DOWN in keys: # if down arrow is pressed myguy.y += 5 # move the character down 5 pixels # draw a character on the screen camera.draw(myguy) camera.display() # make that screen appear # set how many times the tick method is called # (i.e., # times to refresh the screen) ticks_per_second = 30 # keep this line the last one in your program gamebox.timer_loop(ticks_per_second, tick) # keep running the game until the window is closed