def draw_lives(num_lives): first_x = camera.right - 20 y = camera.top + 20 for i in range(num_lives): life = gamebox.from_circle(first_x - i * 40, y, 'red', 15) # life = life_boxes[i] # life.x = first_x - i*40 camera.draw(life)
def draw_bloops(): ''' draws all those bloops :return: none ''' global score global w global h for coordinate in bloop_list: camera.draw( gamebox.from_circle(coordinate[0], coordinate[1], bloop_color, int(w / 3))) if game_status == "play": for bloop in bloop_list: if abs(bloop[0] - wackman[0].x) < w / 2: if abs(bloop[1] - wackman[0].y) < h / 2: bloop_list.pop(bloop_list.index(bloop)) score += 100
def __init__(self, kind=1, start=None): """Robot() makes a robot in the corer of a square grid, as does Robot(1) Robot(2) makes a robot in the corer of a rectangular grid Robot(3) makes a robot randomly placed in a rectangular grid Robot(4) makes a robot randomly placed in a random mess of rooms Robot(''' xxxxx xxxxxxx xx xxx xx xxxx xxxx xxxx xxxx xx xxx xx xxx xxx xxxxxxx xxxxx ''') makes a robot inside a smiley face All versions have an optional "start position" parameter; if given it should be an x,y coordinate, with (0,0) the top left corner with x increasing to right and y to bottom""" import pygame self.scale = 35 try: self.scale = min(pygame.display.Info().current_w, pygame.display.Info().current_h) // 24 except: pass surf = pygame.surface.Surface((self.scale * 2, self.scale * 2), pygame.SRCALPHA, 32) pygame.draw.polygon(surf, (95, 47, 0), [(0, 0), (self.scale, self.scale), (0, self.scale * 2)]) pygame.draw.polygon(surf, (191, 255, 0), [(0, 0), (self.scale, self.scale), (self.scale * 2, 0)]) pygame.draw.polygon(surf, (63, 0, 63), [(self.scale * 2, 0), (self.scale, self.scale), (self.scale * 2, self.scale * 2)]) pygame.draw.polygon(surf, (207, 191, 63), [(0, self.scale * 2), (self.scale, self.scale), (self.scale * 2, self.scale * 2)]) pygame.draw.rect( surf, (191, 191, 191), pygame.rect.Rect( self.scale // 4, self.scale // 4, 6 * self.scale // 4, 6 * self.scale // 4, )) self.room = gamebox.SpriteBox(0, 0, surf, None) self.door = gamebox.from_color(0, 0, (0, 127, 127), 1, 1) self.b = gamebox.from_circle(self.scale * 3, self.scale * 3, 'white', self.scale // 2, 'black', 3 * self.scale // 8) self.eye = gamebox.from_circle(self.scale * 3, self.scale * 3, 'black', self.scale // 2, 'white', 7 * self.scale // 16) self.pupil = gamebox.from_circle(self.scale * 3, self.scale * 3, 'black', self.scale // 4) self.ok = True self.look = (0, 0) if kind == 1: # square, corner s = random.randrange(2, 10) self.open = {(a, b) for a in range(s) for b in range(s)} self.c = gamebox.Camera(self.scale * s * 2, self.scale * s * 2) if start not in self.open: start = (0, 0) self.b.center = (start[0] * 2 + 1) * self.scale, (start[1] * 2 + 1) * self.scale elif kind == 2: # rectangle, corner w, h = random.randrange(2, 10), random.randrange(2, 10) self.open = {(a, b) for a in range(w) for b in range(h)} self.c = gamebox.Camera(self.scale * w * 2, self.scale * h * 2) if start not in self.open: start = (0, 0) self.b.center = (start[0] * 2 + 1) * self.scale, (start[1] * 2 + 1) * self.scale elif kind == 3: # rectangle, not corner w, h = random.randrange(2, 10), random.randrange(2, 10) self.open = {(a, b) for a in range(w) for b in range(h)} self.c = gamebox.Camera(self.scale * w * 2, self.scale * h * 2) if start not in self.open: start = random.choice(list(self.open)) self.b.center = (start[0] * 2 + 1) * self.scale, (start[1] * 2 + 1) * self.scale elif kind == 4: # diamond, top s = random.randrange(1, 5) self.open = set() for i in range(s * 2 + 1): dist = s - abs(i - s) for k in range(s - dist, s + dist + 1): self.open.add((i, k)) self.c = gamebox.Camera(self.scale * (2 * s + 1) * 2, self.scale * (2 * s + 1) * 2) if start not in self.open: start = (s, 0) self.b.center = (start[0] * 2 + 1) * self.scale, (start[1] * 2 + 1) * self.scale elif kind == 5: # diamond, random s = random.randrange(1, 5) self.open = set() for i in range(s * 2 + 1): dist = s - abs(i - s) for k in range(s - dist, s + dist + 1): self.open.add((i, k)) self.c = gamebox.Camera(self.scale * (2 * s + 1) * 2, self.scale * (2 * s + 1) * 2) if start not in self.open: start = random.choice(list(self.open)) self.b.center = (start[0] * 2 + 1) * self.scale, (start[1] * 2 + 1) * self.scale elif kind == 6: # spiral s = random.randrange(3, 7) rot = random.randrange(4) self.open = set() p = [0, 0] for side in range(s): dx, dy = 0, 0 side += rot if side & 1: dx = (side & 2) - 1 else: dy = (side & 2) - 1 side -= rot for k in range(side + 1): self.open.add(tuple(p)) p[0] += dx p[1] += dy mx, Mx = min(_[0] for _ in self.open), max(_[0] for _ in self.open) my, My = min(_[1] for _ in self.open), max(_[1] for _ in self.open) w = Mx - mx + 1 h = My - my + 1 self.open = {(_[0] - mx, _[1] - my) for _ in self.open} self.c = gamebox.Camera(self.scale * w * 2, self.scale * h * 2) if start not in self.open: start = (-mx, -my) self.b.center = (start[0] * 2 + 1) * self.scale, (start[1] * 2 + 1) * self.scale elif kind == 7: # spiral, random placement s = random.randrange(3, 7) rot = random.randrange(4) self.open = set() p = [0, 0] for side in range(s): dx, dy = 0, 0 side += rot if side & 1: dx = (side & 2) - 1 else: dy = (side & 2) - 1 side -= rot for k in range(side + 1): self.open.add(tuple(p)) p[0] += dx p[1] += dy mx, Mx = min(_[0] for _ in self.open), max(_[0] for _ in self.open) my, My = min(_[1] for _ in self.open), max(_[1] for _ in self.open) w = Mx - mx + 1 h = My - my + 1 self.open = {(_[0] - mx, _[1] - my) for _ in self.open} self.c = gamebox.Camera(self.scale * w * 2, self.scale * h * 2) if start not in self.open: start = random.choice(list(self.open)) self.b.center = (start[0] * 2 + 1) * self.scale, (start[1] * 2 + 1) * self.scale elif kind == 8: # random cells = [(0, 0)] w, h = 1, 1 while w < 10 and h < 10: x, y = random.choice(cells) if random.randrange(2) == 0: x += random.randrange(-1, 2, 2) else: y += random.randrange(-1, 2, 2) if (x, y) not in cells: cells.append((x, y)) w = 1 + max(_[0] for _ in cells) - min(_[0] for _ in cells) h = 1 + max(_[1] for _ in cells) - min(_[1] for _ in cells) mx, my = min(_[0] for _ in cells), min(_[1] for _ in cells) self.open = {(a - mx, b - my) for (a, b) in cells} self.c = gamebox.Camera(self.scale * w * 2, self.scale * h * 2) if start not in self.open: start = random.choice(list(self.open)) self.b.center = (start[0] * 2 + 1) * self.scale, (start[1] * 2 + 1) * self.scale elif type(kind) is str: cells = set() for y in range(len(kind.split('\n'))): row = kind.split('\n')[y] for x in range(len(row)): cell = row[x] if not cell.isspace(): cells.add((x, y)) w = 1 + max(_[0] for _ in cells) - min(_[0] for _ in cells) h = 1 + max(_[1] for _ in cells) - min(_[1] for _ in cells) mx, my = min(_[0] for _ in cells), min(_[1] for _ in cells) self.open = {(a - mx, b - my) for (a, b) in cells} self.c = gamebox.Camera(self.scale * w * 2, self.scale * h * 2) if start not in self.open: start = random.choice(list(self.open)) self.b.center = (start[0] * 2 + 1) * self.scale, (start[1] * 2 + 1) * self.scale else: raise ValueError('Invalid room kind: ' + repr(kind)) self.moves = 0 self.looks = 0 print('Correct answer:', len(self.open))
# - The game will have enemies that move. Being hit by an enemy will result in the loss of a life # - There will be collectibles: key on level 2 and coins on level 3 # - There are 3 levels # - We have a save point in the game after completing level 2 # - Level 3 is a scrolling level import gamebox import pygame camera = gamebox.Camera(800, 600) # Level 1 scenery player1 = gamebox.from_color(400, 475, 'blue', 20, 20) target = gamebox.from_circle(400, 125, 'purple', 7) fence1 = gamebox.from_color(400, 100, 'black', 400, 10) fence2 = gamebox.from_color(200, 300, 'black', 10, 400) fence3 = gamebox.from_color(600, 300, 'black', 10, 400) fence4 = gamebox.from_color(400, 500, 'black', 400, 10) course_obstacles_1 = [fence1, fence2, fence3, fence4] enemy1 = gamebox.from_circle(250, 150, 'red', 8) enemy2 = gamebox.from_circle(250, 210, 'red', 8) enemy3 = gamebox.from_circle(250, 270, 'red', 8) enemy4 = gamebox.from_circle(250, 330, 'red', 8) enemy5 = gamebox.from_circle(250, 390, 'red', 8) enemy6 = gamebox.from_circle(550, 180, 'red', 8) enemy7 = gamebox.from_circle(550, 240, 'red', 8) enemy8 = gamebox.from_circle(550, 300, 'red', 8)
global active_index active_index = (active_index + 1) % len(platforms) # physics gravity = 1 switch_time = 0 # lives lives = 3 life_boxes = [ gamebox.from_circle(camera.right - 20, camera.top + 20, 'red', 15), gamebox.from_circle(camera.right - 60, camera.top + 20, 'red', 15), gamebox.from_circle(camera.right - 100, camera.top + 20, 'red', 15), gamebox.from_circle(camera.right - 140, camera.top + 20, 'red', 15) ] extra_life = gamebox.from_circle(2500, 300, 'red', 15) extra_life_collected = False def draw_lives(num_lives): first_x = camera.right - 20 y = camera.top + 20 for i in range(num_lives): life = gamebox.from_circle(first_x - i * 40, y, 'red', 15) # life = life_boxes[i]
def tick(keys): global game_state, dice_1, dice_2, dice_3, dice_4, dice_5, roll_tri, total_score, dice_1_state, dice_2_state, \ dice_3_state, dice_4_state, dice_5_state, dice_1_score, dice_2_score, dice_3_score, dice_4_score, dice_5_score camera.clear('green') camera.draw(average_display) roll_tri = pygame.mouse.get_pressed() if game_state == 'start_screen': camera.draw(start_screen) if len(high_scores_list) >= 10: for score in high_scores: camera.draw(score) if pygame.K_SPACE in keys: game_state = 'gametime' if game_state == 'gametime': for thing in outlines: camera.draw(thing) space_color() for number in die_numbers: camera.draw(number) camera.draw(hands) camera.draw(instructions) if roll_tri[0] == 1: roll() game_state = 'choose_die' if game_state == 'choose_die': for thing in outlines: camera.draw(thing) space_color() for number in die_numbers: camera.draw(number) camera.draw(hands) camera.draw(instructions_2) camera.draw(instructions_2_1) camera.draw(instructions_2_2) if pygame.K_1 in keys: dice_1_state = 'done' if pygame.K_2 in keys: dice_2_state = 'done' if pygame.K_3 in keys: dice_3_state = 'done' if pygame.K_4 in keys: dice_4_state = 'done' if pygame.K_5 in keys: dice_5_state = 'done' if pygame.K_SPACE in keys: game_state = 'gametime' if dice_1_state == 'done' and dice_2_state == 'done' and dice_3_state == 'done' and dice_4_state == 'done' and \ dice_5_state == 'done': dice_1_score = dice_1 dice_2_score = dice_2 dice_3_score = dice_3 dice_4_score = dice_4 dice_5_score = dice_5 if dice_1_score == 3: dice_1_score = 0 if dice_2_score == 3: dice_2_score = 0 if dice_3_score == 3: dice_3_score = 0 if dice_4_score == 3: dice_4_score = 0 if dice_5_score == 3: dice_5_score = 0 game_state = 'game_finished' if game_state == 'game_finished': for thing in outlines: camera.draw(thing) space_color() total_score = dice_1_score + dice_2_score + dice_3_score + dice_4_score + dice_5_score camera.draw( gamebox.from_text(400, 75, 'Total Score: ' + str(total_score), 32, 'white')) if game_state != 'start_screen': if dice_1 == 1: camera.draw(gamebox.from_circle(400, 250, 'black', 5)) if dice_1 == 2: camera.draw(gamebox.from_circle(390, 240, 'black', 5)) camera.draw(gamebox.from_circle(410, 260, 'black', 5)) if dice_1 == 3: camera.draw(gamebox.from_circle(400, 250, 'black', 5)) camera.draw(gamebox.from_circle(390, 240, 'black', 5)) camera.draw(gamebox.from_circle(410, 260, 'black', 5)) if dice_1 == 4: camera.draw(gamebox.from_circle(390, 240, 'black', 5)) camera.draw(gamebox.from_circle(410, 260, 'black', 5)) camera.draw(gamebox.from_circle(390, 260, 'black', 5)) camera.draw(gamebox.from_circle(410, 240, 'black', 5)) if dice_1 == 5: camera.draw(gamebox.from_circle(400, 250, 'black', 5)) camera.draw(gamebox.from_circle(390, 240, 'black', 5)) camera.draw(gamebox.from_circle(410, 260, 'black', 5)) camera.draw(gamebox.from_circle(390, 260, 'black', 5)) camera.draw(gamebox.from_circle(410, 240, 'black', 5)) if dice_1 == 6: camera.draw(gamebox.from_circle(410, 250, 'black', 5)) camera.draw(gamebox.from_circle(390, 250, 'black', 5)) camera.draw(gamebox.from_circle(390, 240, 'black', 5)) camera.draw(gamebox.from_circle(410, 260, 'black', 5)) camera.draw(gamebox.from_circle(390, 260, 'black', 5)) camera.draw(gamebox.from_circle(410, 240, 'black', 5)) if dice_2 == 1: camera.draw(gamebox.from_circle(300, 250, 'black', 5)) if dice_2 == 2: camera.draw(gamebox.from_circle(290, 240, 'black', 5)) camera.draw(gamebox.from_circle(310, 260, 'black', 5)) if dice_2 == 3: camera.draw(gamebox.from_circle(300, 250, 'black', 5)) camera.draw(gamebox.from_circle(290, 240, 'black', 5)) camera.draw(gamebox.from_circle(310, 260, 'black', 5)) if dice_2 == 4: camera.draw(gamebox.from_circle(290, 240, 'black', 5)) camera.draw(gamebox.from_circle(310, 260, 'black', 5)) camera.draw(gamebox.from_circle(290, 260, 'black', 5)) camera.draw(gamebox.from_circle(310, 240, 'black', 5)) if dice_2 == 5: camera.draw(gamebox.from_circle(300, 250, 'black', 5)) camera.draw(gamebox.from_circle(290, 240, 'black', 5)) camera.draw(gamebox.from_circle(310, 260, 'black', 5)) camera.draw(gamebox.from_circle(290, 260, 'black', 5)) camera.draw(gamebox.from_circle(310, 240, 'black', 5)) if dice_2 == 6: camera.draw(gamebox.from_circle(290, 250, 'black', 5)) camera.draw(gamebox.from_circle(310, 250, 'black', 5)) camera.draw(gamebox.from_circle(290, 240, 'black', 5)) camera.draw(gamebox.from_circle(310, 260, 'black', 5)) camera.draw(gamebox.from_circle(290, 260, 'black', 5)) camera.draw(gamebox.from_circle(310, 240, 'black', 5)) if dice_3 == 1: camera.draw(gamebox.from_circle(500, 250, 'black', 5)) if dice_3 == 2: camera.draw(gamebox.from_circle(490, 240, 'black', 5)) camera.draw(gamebox.from_circle(510, 260, 'black', 5)) if dice_3 == 3: camera.draw(gamebox.from_circle(500, 250, 'black', 5)) camera.draw(gamebox.from_circle(490, 240, 'black', 5)) camera.draw(gamebox.from_circle(510, 260, 'black', 5)) if dice_3 == 4: camera.draw(gamebox.from_circle(490, 240, 'black', 5)) camera.draw(gamebox.from_circle(510, 260, 'black', 5)) camera.draw(gamebox.from_circle(490, 260, 'black', 5)) camera.draw(gamebox.from_circle(510, 240, 'black', 5)) if dice_3 == 5: camera.draw(gamebox.from_circle(500, 250, 'black', 5)) camera.draw(gamebox.from_circle(490, 240, 'black', 5)) camera.draw(gamebox.from_circle(510, 260, 'black', 5)) camera.draw(gamebox.from_circle(490, 260, 'black', 5)) camera.draw(gamebox.from_circle(510, 240, 'black', 5)) if dice_3 == 6: camera.draw(gamebox.from_circle(510, 250, 'black', 5)) camera.draw(gamebox.from_circle(490, 250, 'black', 5)) camera.draw(gamebox.from_circle(490, 240, 'black', 5)) camera.draw(gamebox.from_circle(510, 260, 'black', 5)) camera.draw(gamebox.from_circle(490, 260, 'black', 5)) camera.draw(gamebox.from_circle(510, 240, 'black', 5)) if dice_4 == 1: camera.draw(gamebox.from_circle(350, 350, 'black', 5)) if dice_4 == 2: camera.draw(gamebox.from_circle(340, 340, 'black', 5)) camera.draw(gamebox.from_circle(360, 360, 'black', 5)) if dice_4 == 3: camera.draw(gamebox.from_circle(350, 350, 'black', 5)) camera.draw(gamebox.from_circle(340, 340, 'black', 5)) camera.draw(gamebox.from_circle(360, 360, 'black', 5)) if dice_4 == 4: camera.draw(gamebox.from_circle(340, 340, 'black', 5)) camera.draw(gamebox.from_circle(360, 360, 'black', 5)) camera.draw(gamebox.from_circle(340, 360, 'black', 5)) camera.draw(gamebox.from_circle(360, 340, 'black', 5)) if dice_4 == 5: camera.draw(gamebox.from_circle(350, 350, 'black', 5)) camera.draw(gamebox.from_circle(340, 340, 'black', 5)) camera.draw(gamebox.from_circle(360, 360, 'black', 5)) camera.draw(gamebox.from_circle(340, 360, 'black', 5)) camera.draw(gamebox.from_circle(360, 340, 'black', 5)) if dice_4 == 6: camera.draw(gamebox.from_circle(340, 350, 'black', 5)) camera.draw(gamebox.from_circle(360, 350, 'black', 5)) camera.draw(gamebox.from_circle(340, 340, 'black', 5)) camera.draw(gamebox.from_circle(360, 360, 'black', 5)) camera.draw(gamebox.from_circle(340, 360, 'black', 5)) camera.draw(gamebox.from_circle(360, 340, 'black', 5)) if dice_5 == 1: camera.draw(gamebox.from_circle(450, 350, 'black', 5)) if dice_5 == 2: camera.draw(gamebox.from_circle(440, 340, 'black', 5)) camera.draw(gamebox.from_circle(460, 360, 'black', 5)) if dice_5 == 3: camera.draw(gamebox.from_circle(450, 350, 'black', 5)) camera.draw(gamebox.from_circle(440, 340, 'black', 5)) camera.draw(gamebox.from_circle(460, 360, 'black', 5)) if dice_5 == 4: camera.draw(gamebox.from_circle(440, 340, 'black', 5)) camera.draw(gamebox.from_circle(460, 360, 'black', 5)) camera.draw(gamebox.from_circle(440, 360, 'black', 5)) camera.draw(gamebox.from_circle(460, 340, 'black', 5)) if dice_5 == 5: camera.draw(gamebox.from_circle(450, 350, 'black', 5)) camera.draw(gamebox.from_circle(440, 340, 'black', 5)) camera.draw(gamebox.from_circle(460, 360, 'black', 5)) camera.draw(gamebox.from_circle(440, 360, 'black', 5)) camera.draw(gamebox.from_circle(460, 340, 'black', 5)) if dice_5 == 6: camera.draw(gamebox.from_circle(440, 350, 'black', 5)) camera.draw(gamebox.from_circle(460, 350, 'black', 5)) camera.draw(gamebox.from_circle(440, 340, 'black', 5)) camera.draw(gamebox.from_circle(460, 360, 'black', 5)) camera.draw(gamebox.from_circle(440, 360, 'black', 5)) camera.draw(gamebox.from_circle(460, 340, 'black', 5)) camera.display()
def setup(level): """sets all of the needed variables, starts the level corresponding to the given level argument""" global level1, level2, level3, level1screen, gameover, checkpoint_reached global boundary, left_angled_bumpers, right_angled_bumpers, diagonal_bumpers, square_bumpers, moving_bumpers global targets, target_dict, coins, coin_1, coin_2, score, j, k, c, up_arrow, ball, paddle, balls, board, background # input tells function which level we are in if level == 1: level1 = True if level == 2: level2 = True if level == 3: level3 = True level1screen = True gameover = False score = 0 j = 0 k = 0 c = 0 up_arrow = 0 # on the first level the player gets 3 balls, only 1 additional on subsequent levels if level == 1: balls += 3 else: balls += 1 # ball ball = gamebox.from_circle( r.randrange(300, 511, 70), 120, 'dark blue', 4) # places the ball randomly at 300, 370, 440, or 510 ball.speedx = r.randrange( 0, 3, 2) - 1 # gives the ball a random speed of either -1 and 1 ball.speedy = 1 # paddle paddle = gamebox.from_color(camera_x / 2, 565, 'black', 60, 20) # checkpoint accomplished text checkpoint_reached = gamebox.from_text(400, 300, 'CHECKPOINT REACHED', 40, 'black') # used in level 2 # head of the board backbox = gamebox.from_image(camera_x / 2, 0, 'backbox_lvl_1.png') backbox.scale_by(.30) backbox.top = camera.top # board board = gamebox.from_image(camera_x / 2, 0, 'board.png') board.scale_by(.55) board.top = backbox.bottom - 20 background = [backbox, board] # boundary boundary_left = gamebox.from_color(board.left + 20, camera_y / 2, 'black', 17, 410) boundary_right = gamebox.from_color(board.right - 20, camera_y / 2, 'black', 17, 410) boundary_top = gamebox.from_color( camera_x / 2, 0, 'black', boundary_right.left - boundary_left.right + 31, 17) boundary_top.bottom = boundary_left.top + 15 boundary = [boundary_left, boundary_right, boundary_top] # diagonal bumpers exit_bumper_left = create_diagonal_45(board.left + 13, boundary_left.bottom - 12, board.left + 45, 26, 'black') exit_bumper_right = create_diagonal_45(board.right - 13, boundary_right.bottom - 12, board.right - 45, 26, 'black') right_angled_bumper_1 = create_diagonal_45(camera_x / 2 + 10, 250, camera_x / 2 + 50, 15, 'yellow') left_angled_bumper_1 = create_diagonal_45(camera_x / 2 - 10, 250, camera_x / 2 - 50, 15, 'yellow') # separate lists for different angled bumpers because they have a different effect on the ball left_angled_bumpers = [exit_bumper_right, left_angled_bumper_1] right_angled_bumpers = [exit_bumper_left, right_angled_bumper_1] # we can put all of the diagonals in the same list to draw them later: this is a list of lists diagonal_bumpers = [ exit_bumper_left, exit_bumper_right, right_angled_bumper_1, left_angled_bumper_1 ] # square bumpers square_bumper1 = gamebox.from_color(camera_x / 3 - 20, camera_y / 2, 'red', 30, 30) square_bumper2 = gamebox.from_color(camera_x * (2 / 3) + 20, camera_y / 2, 'red', 30, 30) square_bumpers = [square_bumper1, square_bumper2] # OPTIONAL FEATURES 1. Moving Enemies # bumpers that perpetually move side to side or up and down moving_bumper_1 = gamebox.from_color(camera_x / 3 + 60, 400, 'black', 50, 15) moving_bumper_2 = gamebox.from_color(camera_x * (2 / 3) - 60, 400, 'black', 50, 15) moving_bumper_3 = gamebox.from_color(camera_x / 2 + 45, 185, 'black', 75, 15) moving_bumpers = [moving_bumper_1, moving_bumper_2, moving_bumper_3] # targets target50_1 = gamebox.from_image(250, 200, 'target50_1.png') target50_2 = gamebox.from_image(550, 200, 'target50_1.png') target100_1 = gamebox.from_image(400, 210, 'target100_1.png') targets = [target50_1, target50_2, target100_1] for each in targets: each.scale_by(.1) # make a dictionary with 'targets' indices as keys and their corresponding points values as values # makes it easier to call the correct point values later target_dict = {0: 50, 1: 50, 2: 100} # OPTIONAL FEATURES 2. Collectibles # coins that give the user an extra ball coin_1 = gamebox.from_image(camera_x / 2, 250, 'gold_coin.png') coin_2 = gamebox.from_image(boundary_left.right + 10, boundary_top.bottom + 10, 'gold_coin.png') coins = [coin_1, coin_2] for each in coins: each.scale_by(.1)
shark = gamebox.from_color(1300, 400, 'orange', 40, 30) shark_sheet = gamebox.load_sprite_sheet( 'https://orig00.deviantart.net/bd28/f/2014/048/4/b/pating_triangle_by_ottojoy-d76ywd4.png', 2, 2) shark = gamebox.from_image(shark.x, shark.y, shark_sheet[1]) shark.scale_by(0.7) crab = gamebox.from_color(800, 300, 'red', 40, 30) crab_sheet = gamebox.load_sprite_sheet( 'https://d3tv7e6jdw6zbr.cloudfront.net/items/2012-12-06/npc_crab__x1_walk_png_1354831183.png', 4, 6) crab = gamebox.from_image(crab.x, crab.y, crab_sheet[1]) crab.scale_by(0.5) star = gamebox.from_circle(1050, 100, 'yellow', 10) star_sheet = gamebox.load_sprite_sheet( 'http://3.bp.blogspot.com/-eo9bbSi5gmo/Uz6ylKl5hUI/AAAAAAAAAjo/OhG0bdh9xSo/s1600/Ster+animatie.png', 3, 8) star = gamebox.from_image(star.x, star.y, star_sheet[3]) star.scale_by(0.8) start = False score = 0 count_tick = 0 speed = 3 lvl = 1 def random_boosts(): """
def draw_lives_p2(num_lives): first_x = camera.right - 20 y = camera.top + 20 for i in range(num_lives): life = gamebox.from_circle(first_x - i * 40, y, 'lawngreen', 15) camera.draw(life)
# Names: Emmanuel Ogunjirin | AJ Givens # Computing ID: EAO5XC | (Insert AJ Given's Computing ID here # ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- # # ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- # # ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- # # ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- # import pygame # Gets the pygame library import gamebox """ This is the classic snake game - Eat as many apples as you can without hitting/eating yourself :-) """ camera = gamebox.Camera(800, 600) snakeItem = gamebox.from_circle(400, 400, 'white', 5) def snake(keys): """ .... :param keys: :return: """ camera.clear('black') # Gets the dark background of the game platform. if pygame.K_LEFT in keys or pygame.K_a in keys: # Sets the key to do the following action snakeItem.x -= 5 # move snakeItem left if pygame.K_RIGHT in keys or pygame.K_d in keys: # Sets the key to do the following action snakeItem.x += 5 # move snakeItem right if pygame.K_UP in keys or pygame.K_w in keys: # Sets the key to do the following action snakeItem.y -= 5 # move snakeItem up if pygame.K_DOWN in keys or pygame.K_s in keys: # Sets the key to do the following action
def draw_lives_p1(num_lives): first_x = camera.left + 20 y = camera.top + 20 for i in range(num_lives): life = gamebox.from_circle(first_x + i * 40, y, 'cyan', 15) camera.draw(life)
# ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- # import pygame # Gets the pygame library import gamebox # Gets the gamebox library """ This is a Tron like game made as an assignment for CS-1111 Game Project. This project was done using the gamebox module developed by Professor Luther Tychonievich. This is the classic Tron game - Try to corner your opponent into hitting you while trying not to hit them :-) """ width = 800 # This is the width of the window height = 600 # This is the height of the window camera = gamebox.Camera(width, height) # Draws the window redPlayer = gamebox.from_circle(750, 550, 'red', 5) # This is the red player bluePlayer = gamebox.from_circle(50, 50, 'blue', 5) # This is the blue player redPlayer_instruction = gamebox.from_text(600, 550, 'This is the red player ---> ', 30, 'red', False) # Draws Instructions bluePlayer_instruction = gamebox.from_text(250, 50, '<--- This is the blue player', 30, 'blue', False) # Draws Instructions redPlayer_instruction1 = gamebox.from_text(400, 500, 'Use the arrows to control the red player', 30, 'red', False) # Draws Instructions bluePlayer_instruction1 = gamebox.from_text(400, 100, 'Use the "A,W,S,D" keys to control the blue player', 30, 'blue', False) # Draws Instructions general_instructions = gamebox.from_text(400, 300, 'Press Space to Start!', 80, 'white', True) # Draws Instructions general_instructions1 = gamebox.from_text(400, 350, 'Try to make your opponent hit you', 40, 'white', True) # Draws Instructions redPlayerPositions = [] # Keeps a running list of the position of the red player bluePlayerPositions = [] # Keeps a running list of the position of the blue player game_on = False # Freezes the screen to begin number = 0 # Sets the start to 0
# ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- # # ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- # # ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- # # This game project was designed as an assignment for the Introductory to Computer Engineering (CS-1111) course. # This game consists of a collection of games put that was designed and created by the names listed above for the final game project of the class. import pygame # Gets the pygame library import gamebox # Gets the gamebox library width = 800 # Sets the width to the size height = 600 # Sets the height to the size. camera = gamebox.Camera(width, height) # This is the camera that pans the screen. main_screen_character = gamebox.from_circle( width / 2, height / 2, 'white', 10) # This is the main screen character. main_screen_character_speed = 10 # This is the speed of the white bob. square_width = 50 # This is how wide the squares are square_height = 50 # This is how high the squares are first_square_spacing = 400 # This is where the first squares are and the others are based off this one. snakeOption = gamebox.from_color(width / 3, height - first_square_spacing, 'green', square_width, square_height) # Snake game asteroidsOption = gamebox.from_color(width - width / 3, height - first_square_spacing, 'red', square_width, square_height) # Asteroid game bricksOption = gamebox.from_color(width / 3, height - first_square_spacing + 150, 'blue',
def tick(keys): global currentangle, measureticks, numberOfTicksToShoot, arrayOfCoins, game, hp, delayForEnemies, numberOfCoins, \ score, interfaceForUpgrades, delayForInterface, StartOfTheGame, gun_price, hp_price, bullet_speed, Lost, view_hs if StartOfTheGame: if pygame.mouse.get_pressed()[0] and ( pygame.mouse.get_pos()[0] > 275) and (pygame.mouse.get_pos()[0] < 525) and (pygame.mouse.get_pos()[1] < 234) and (pygame.mouse.get_pos()[1] > 166): StartOfTheGame = False game = True reset() if pygame.mouse.get_pressed()[0] and ( pygame.mouse.get_pos()[0] > 275) and (pygame.mouse.get_pos()[0] < 525) and (pygame.mouse.get_pos()[1] < 434) and (pygame.mouse.get_pos()[1] > 366): sys.exit() if pygame.mouse.get_pressed()[0] and ( pygame.mouse.get_pos()[0] > 275) and (pygame.mouse.get_pos()[0] < 525) and (pygame.mouse.get_pos()[1] < 334) and (pygame.mouse.get_pos()[1] > 266): view_hs = True camera.draw( gamebox.from_image(400, 300, "StartInterfaceBackground.png")) camera.draw(gamebox.from_image(400, 200, 'StartGameButton.png')) camera.draw(gamebox.from_image(400, 300, "HighScoresButton.png")) camera.draw(gamebox.from_image(400, 400, "ExitButton.png")) camera.draw( gamebox.from_text(150, 40, "Defend flag from zombies!", 30, "red")) camera.draw( gamebox.from_text(180, 560, "Use W, A, S, D to move character.", 30, "red")) camera.draw( gamebox.from_text(620, 40, "Use the mouse to rotate player", 30, "red")) camera.draw( gamebox.from_text(670, 560, "Press LMB to shoot", 30, "red")) if view_hs: camera.draw( gamebox.from_image(400, 300, "StartInterfaceBackground.png")) if pygame.K_SPACE in keys: view_hs = False camera.draw( gamebox.from_text(300, 25, "Press Space to go back", 40, "red")) stream = open('HighScores.txt', 'r') scores = stream.readlines() delta = 40 for i in scores: camera.draw( gamebox.from_text(400, 100 + delta, i[0:len(i) - 1], 25, 'blue')) delta += 30 camera.display() if game: measureticks += 1 if pygame.K_w in keys: player.move(0, -5) if pygame.K_s in keys: player.move(0, 5) if pygame.K_a in keys: player.move(-5, 0) if pygame.K_d in keys: player.move(5, 0) if delayForInterface < 30: delayForInterface += 1 if (pygame.K_SPACE in keys) and (delayForInterface >= 30): interfaceForUpgrades = True delayForInterface = 0 if interfaceForUpgrades: game = False x = -(player.x - pygame.mouse.get_pos()[0]) y = -(player.y - pygame.mouse.get_pos()[1]) if delayForEnemies <= 30: delayForEnemies += 1 for i in enemies: if i.touches(other=player): if delayForEnemies >= 30: hp -= 1 delayForEnemies = 0 if x == 0: x = 0.0001 angle = int((math.atan2(x, y)) * 360 / (math.pi * 2)) - 90 player.rotate((angle - currentangle)) currentangle = angle if pygame.mouse.get_pressed()[0] == 1: vx = bullet_speed * (x / (math.sqrt(abs(x * x + y * y)))) vy = bullet_speed * (y / (math.sqrt(abs(x * x + y * y)))) if measureticks > numberOfTicksToShoot: arrayOfBullets.append([ gamebox.from_image(player.x, player.y, 'bullet.png'), vx, vy ]) measureticks = 0 for i in arrayOfBullets: i[0].move(i[1], i[2]) for i in arrayOfBullets: for j in enemies: if i[0].touches(other=j): arrayOfCoins.append( gamebox.from_circle(j.x, j.y, 'yellow', 5)) j.x = -2000000 j.y = -2000000 i[0].x = -1000000 i[0].y = -1000000 score += 10 for i in arrayOfCoins: if i.touches(other=player): i.x = -1000000 i.y = -1000000 numberOfCoins += 1 if hp == 0: game = False Lost = True for i in enemies: if i.touches(other=flag): Lost = True game = False if measureticks % 50 == 0: if random.randint(1, 8) == 1: enemies.append( gamebox.from_image(random.randrange(-200, 0), random.randrange(-200, 0), 'New Piskel.png')) enemies[-1].rotate( int((math.atan2(400 - enemies[-1].x, 300 - enemies[-1].y)) * 360 / (math.pi * 2)) - 90) elif random.randint(1, 8) == 2: enemies.append( gamebox.from_image(random.randrange(-200, 0), random.randrange(200, 400), 'New Piskel.png')) enemies[-1].rotate( int((math.atan2(400 - enemies[-1].x, 300 - enemies[-1].y)) * 360 / (math.pi * 2)) - 90) elif random.randint(1, 8) == 3: enemies.append( gamebox.from_image(random.randrange(-200, 0), random.randrange(600, 800), 'New Piskel.png')) enemies[-1].rotate( int((math.atan2(400 - enemies[-1].x, 300 - enemies[-1].y)) * 360 / (math.pi * 2)) - 90) elif random.randint(1, 8) == 4: enemies.append( gamebox.from_image(random.randrange(300, 500), random.randrange(-200, 0), 'New Piskel.png')) enemies[-1].rotate( int((math.atan2(400 - enemies[-1].x, 300 - enemies[-1].y)) * 360 / (math.pi * 2)) - 90) elif random.randint(1, 8) == 5: enemies.append( gamebox.from_image(random.randrange(800, 1000), random.randrange(-200, 0), 'New Piskel.png')) enemies[-1].rotate( int((math.atan2(400 - enemies[-1].x, 300 - enemies[-1].y)) * 360 / (math.pi * 2)) - 90) elif random.randint(1, 8) == 6: enemies.append( gamebox.from_image(random.randrange(800, 1000), random.randrange(200, 400), 'New Piskel.png')) enemies[-1].rotate( int((math.atan2(400 - enemies[-1].x, 300 - enemies[-1].y)) * 360 / (math.pi * 2)) - 90) elif random.randint(1, 8) == 7: enemies.append( gamebox.from_image(random.randrange(800, 1000), random.randrange(600, 800), 'New Piskel.png')) enemies[-1].rotate( int((math.atan2(400 - enemies[-1].x, 300 - enemies[-1].y)) * 360 / (math.pi * 2)) - 90) elif random.randint(1, 8) == 8: enemies.append( gamebox.from_image(random.randrange(300, 500), random.randrange(600, 800), 'New Piskel.png')) enemies[-1].rotate( int((math.atan2(400 - enemies[-1].x, 300 - enemies[-1].y)) * 360 / (math.pi * 2)) - 90) for i in enemies: i.move( 5 * (400 - i.x) / (math.sqrt((400 - i.x) * (400 - i.x) + (300 - i.y) * (300 - i.y))), 5 * (300 - i.y) / (math.sqrt((400 - i.x) * (400 - i.x) + (300 - i.y) * (300 - i.y)))) camera.draw(background) camera.draw(player) for i in arrayOfBullets: camera.draw(i[0]) for i in enemies: camera.draw(i) for i in arrayOfCoins: camera.draw(i) if hp == 1: camera.draw(health[0]) if hp == 2: camera.draw(health[0]) camera.draw(health[1]) if hp == 3: camera.draw(health[0]) camera.draw(health[1]) camera.draw(health[2]) camera.draw( gamebox.from_text(600, 30, "Coins: " + str(numberOfCoins), 30, 'Yellow')) camera.draw( gamebox.from_text(700, 30, "Score: " + str(score), 30, 'green')) camera.draw(flag) camera.draw( gamebox.from_text(500, 570, "Press Space to upgrade gun or restore hp", 30, "red")) camera.display() if (game == False) and (Lost == True): camera.draw( gamebox.from_text(400, 300, 'You lost! Press Space to exit.', 40, "red")) camera.draw( gamebox.from_text(400, 350, 'Score: ' + str(score), 40, "green")) camera.display() if pygame.K_SPACE in keys: Lost = False StartOfTheGame = True stream = open("HighScores.txt", "r") highscores = stream.readlines() scores_to_compare = [] for i in highscores: scores_to_compare.append(i.split()) NewHS = False try: for i in range(0, len(scores_to_compare)): if (score > int(scores_to_compare[i][2])) and (NewHS == False): for j in range(len(scores_to_compare) - 1, i, -1): scores_to_compare[j][2] = scores_to_compare[j - 1][2] scores_to_compare[i][2] = str(score) NewHS = True except: x = None stream.close() if NewHS == True: stream = open("HighScores.txt", "w") for i in range(0, len(scores_to_compare)): scores_to_compare[ i] = scores_to_compare[i][0] + ' ' + scores_to_compare[ i][1] + ' ' + scores_to_compare[i][2] stream.write(scores_to_compare[i] + '\n') stream.close() NewHS = False if (game == False) and (interfaceForUpgrades == True): if delayForInterface < 30: delayForInterface += 1 if (pygame.K_SPACE in keys) and (delayForInterface >= 30): game = True interfaceForUpgrades = False delayForInterface = 0 if (pygame.K_e in keys) and (numberOfCoins >= gun_price): bullet_speed *= 2 numberOfCoins = numberOfCoins - gun_price gun_price *= 2 camera.draw( gamebox.from_text( 400, 150, "Speed of the bullet was doubled! Press Space to exit.", 20, "red")) if pygame.K_r in keys: if hp < 3 and numberOfCoins >= hp_price: hp += 1 numberOfCoins = numberOfCoins - hp_price hp_price *= 2 camera.draw( gamebox.from_text( 400, 150, "1 hp was restored! Press Space to exit.", 20, "red")) if hp >= 3 and numberOfCoins >= hp_price: camera.draw( gamebox.from_text( 400, 150, "You already have maximum hp! Press Space to exit.", 20, "red")) camera.draw(gamebox.from_image(400, 300, 'interfaceForUpgrades.png')) camera.draw(gamebox.from_text(300, 375, str(gun_price), 20, "yellow")) camera.draw(gamebox.from_text(450, 375, str(hp_price), 20, "yellow")) camera.display()
def draw_ball_interactions(level): """draws the ball and sets all the interactions it has with other objects on the game board""" global ball, balls, paddle, moving_bumpers, boundary, right_angled_bumpers, left_angled_bumpers, square_bumpers global targets, score, target_dict, player_ready, j, coins, gameover # speeds of the ball get changed by move_to_stop_overlapping, so if we capture the speed before that, we have it # available for use local_speedx = ball.speedx local_speedy = ball.speedy ball.speedy += .1 # gravity ball.move_speed() # interaction with paddle ball.move_to_stop_overlapping(paddle) if ball.bottom_touches(paddle) or ball.left_touches( paddle) or ball.right_touches(paddle): ball.speedx = local_speedx + ( paddle.speedx / (level + 4) ) # we add a little bit of the paddle's speed to the ball, a lesser fraction as the paddle speed increases between levels ball.speedy = -6.5 # paddle will cause a consistent upward force on the ball # interaction with moving bumpers for each in moving_bumpers: ball.move_to_stop_overlapping(each) if ball.bottom_touches(each): ball.speedy = -local_speedy * .9 ball.speedx = local_speedx if ball.top_touches(each): ball.speedy = -local_speedy * .75 # slow it down so the ball doesn't kick down really fast ball.speedx = local_speedx if ball.left_touches(each): ball.speedx = -local_speedx * .9 ball.x += 1 # makes sure ball doesn't ride along the side if ball.right_touches(each): ball.speedx = -local_speedx * .9 ball.x -= 1 # interaction with boundaries for each in boundary: ball.move_to_stop_overlapping(each) for i in range(2): if ball.touches(boundary[i]): # sides of the boundary ball.speedx = -local_speedx * .75 # slow it down in the x direction a little # no change in y because we already have gravity if ball.touches(boundary[2]): # top of the boundary ball.speedy = -local_speedy * .75 # interaction with diagonal bumpers # have to go through left/right separately because of their angles for each in right_angled_bumpers: for i in range(len(each)): ball.move_to_stop_overlapping(each[i]) if ball.bottom_touches(each[i]) or ball.left_touches(each[i]): ball.speedy = -local_speedy * .85 if local_speedx > 0: # because it is angled right, if the ball is coming from the left, it # continues that way ball.speedx = local_speedx else: ball.speedx = -local_speedx # otherwise it gets shot back in the opposite direction if ball.top_touches(each[i]) or ball.right_touches( each[i] ): # now we are looking at collisions from underneath ball.speedy = -local_speedy * .85 if local_speedx < 0: # if the ball is moving to the left ball.speedx = local_speedx # since the underside is angled left it continues that way else: ball.speedx = -local_speedx for each in left_angled_bumpers: for i in range(len(each)): ball.move_to_stop_overlapping(each[i]) if ball.bottom_touches(each[i]) or ball.right_touches(each[i]): ball.speedy = -local_speedy * .85 if local_speedx < 0: # if the ball is moving left ball.speedx = local_speedx # since top is angled left it continues that way else: ball.speedx = -local_speedx if ball.top_touches(each[i]) or ball.left_touches(each[i]): ball.speedy = -local_speedy * .85 if local_speedx > 0: # if the ball is moving right ball.speedx = local_speedx # since the underside is angled right it continues that way else: ball.speedx = -local_speedx # interaction with square bumpers for each in square_bumpers: ball.move_to_stop_overlapping(each) if ball.bottom_touches(each) or ball.top_touches(each): ball.speedy = -local_speedy * .75 ball.speedx = local_speedx if local_speedx > 0: # move the ball left or right based on incoming direction of ball ball.x += 1 # makes sure ball doesn't get stuck on top of a bumper else: ball.x -= 1 if ball.left_touches(each): ball.speedx = -local_speedx * .75 ball.speedy = local_speedy ball.x += 1 # makes sure ball doesn't get stuck on the side if ball.right_touches(each): ball.speedx = -local_speedx * .75 ball.speedy = local_speedy ball.x -= 1 # makes sure ball doesn't get stuck on the side # interaction with targets for i in range(len(targets)): if i == 4: # for the 500 point bumper if ball.touches(targets[i]): score += target_dict[i] ball.move_to_stop_overlapping(targets[i]) ball.speedx = -local_speedx * 1.5 # want it to kick off extra fast ball.x += 2 # makes sure ball doesn't rack up too many points on it if ball.bottom_touches(targets[i]) or ball.top_touches(targets[i]): score += target_dict[i] ball.move_to_stop_overlapping(targets[i]) ball.speedy = -local_speedy ball.speedx = local_speedx if local_speedx > 0: # if the ball is moving right move it right + 2 ball.x += 2 # makes sure ball doesn't get stuck on top else: ball.x -= 2 if ball.left_touches(targets[i]): score += target_dict[i] ball.move_to_stop_overlapping(targets[i]) ball.speedy = local_speedy ball.speedx = -local_speedx ball.x += 1 # makes sure ball doesn't get stuck to side if ball.right_touches(targets[i]): score += target_dict[i] ball.move_to_stop_overlapping(targets[i]) ball.speedy = local_speedy ball.speedx = -local_speedx ball.x -= 1 ball.move_speed() # OPTIONAL FEATURES 2. Collectibles for each in coins: if ball.touches(each): balls += 1 # if the ball touches a coin, the user gets an extra ball and the coin is removed coins.remove(each) # stop drawing the coin if ball.top > camera.bottom: # if the ball falls below the bottom of the screen balls -= 1 # subtract 1 from the balls total if balls > 0: # if there are still balls left we will redraw a new one ball = gamebox.from_circle(r.randrange(300, 501, 70), 120, 'dark blue', 4) ball.speedx = r.randrange( 0, 3, 2) - 1 # gives the ball a random speed between -1 and 1 else: # if there are no balls left gameover gameover = True camera.draw(ball)
import gamebox camera = gamebox.Camera(800, 600) # scenery background = gamebox.from_image(400, 300, 'uva_court.jpg') background.scale_by(0.8) court = gamebox.from_color(400, 500, 'blue', 300, 50) backboard = gamebox.from_color(475, 400, 'white', 30, 150) hoop = gamebox.from_image(445, 350, 'hoop.png') hoop.scale_by(0.1) scenery = [background, court, backboard, hoop] # Things that are stationary obstacles = [court, backboard] # things that are "solid" # interactive ball = gamebox.from_circle(300, 465, 'orangered', 10) # stats score = 0 lives = 3 # physics gravity = 0.75 jump_speed = 20 # other scored_since_landed = False def draw_scenery(): """Draw all the stationary items"""
import pygame import gamebox camera = gamebox.Camera(600, 600) # player player1 = gamebox.from_color(300, 300, 'green', 20, 20) player2 = gamebox.from_color(300, 300, 'magenta', 20, 20) # ball ball = gamebox.from_circle(100, 100, 'red', 5) ball.speedx = 8 ball.speedy = 7 # arena arena = [ gamebox.from_color(0, 300, 'black', 100, 600), gamebox.from_color(600, 300, 'black', 100, 600), gamebox.from_color(300, 0, 'black', 600, 100), gamebox.from_color(300, 600, 'black', 600, 100), gamebox.from_color(400, 400, 'black', 50, 50) ] score1 = 3 score2 = 3 winner = None def touching(box1, box2): '''ww a return true if the boxes are touching ''' if box1.right_touches(box2):