def tutorial(screen): offset = 0 start_position = (1105, 400) car = Car('images/f1sprite.png', start_position) car.direction = 0 car_group = pygame.sprite.Group(car) in_opts = True while in_opts: deltat = clock.tick(30) screen.fill(BLACK) if offset == 99: offset = 0 else: offset = offset + 1 drawroad(screen, offset) car_group.update(deltat) car_group.draw(screen) text('Tutorial', YELLOW, screen, 20, 20) image = _load_image('./images/tutorial.png') screen.blit(image, (30, 100)) for event in pygame.event.get(): # User did something if event.type == pygame.QUIT: # If user clicked close pygame.quit() # Flag that we are done so we exit this loop if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: in_opts = False pygame.display.update() clock.tick(60)
def pick_track_AI(screen): offset = 0 start_position = (1105, 400) car = Car('images/f1sprite.png', start_position) car.direction = 0 car_group = pygame.sprite.Group(car) click = False in_opts = True while in_opts: deltat = clock.tick(30) screen.fill(BLACK) if offset == 99: offset = 0 else: offset = offset + 1 drawroad(screen, offset) car_group.update(deltat) car_group.draw(screen) text('Pick Track', YELLOW, screen, 20, 20) mx, my = pygame.mouse.get_pos() for event in pygame.event.get(): # User did something if event.type == pygame.QUIT: # If user clicked close pygame.quit() # Flag that we are done so we exit this loop if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: in_opts = False if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: click = True trackCollide = pygame.Rect(40, 70, 120, 120) track_select_col = BLACK trackCollide2 = pygame.Rect(190, 70, 120, 120) track_select_col2 = BLACK if trackCollide.collidepoint((mx, my)): track_select_col = YELLOW if click: mixer.music.stop() track1_AI.T1_AI(screen) in_opts = False return True if trackCollide2.collidepoint((mx, my)): track_select_col2 = YELLOW if click: mixer.music.stop() track2_AI.T2_AI(screen) in_opts = False return True draw.rect(screen, track_select_col, trackCollide) screen.blit(track, (50, 80)) draw.rect(screen, track_select_col2, trackCollide2) screen.blit(track1, (200, 80)) pygame.display.update() clock.tick(60) return False
def RaceCars(display_surface): # window = screen.Screen() track1 = track.Track() white = (0, 128, 0) start_car1 = (1010, 144) start_car2 = (1010, 75) clock = pygame.time.Clock() t0 = time.time() car = Car('./images/f1sprite.png', start_car1) car_group = pygame.sprite.Group(car) car2 = Car('./images/f1sprite.png', start_car2) car_group2 = pygame.sprite.Group(car2) pad_group = track1.getPads() finish_line = track1.getFinishLine() checkpoint = (960, 845, 10, 125) lap_car1 = 0 checkpoint_car1 = 0 lap_car2 = 0 checkpoint_car2 = 0 # Music for countdown sound current_path = os.path.abspath(os.path.dirname(__file__)) absolute_image_path = os.path.join(current_path, './sounds/race_coundown.mp3') mixer.init() mixer.music.load(absolute_image_path) mixer.music.set_volume(0.7) mixer.music.play() while True: # Draw the Track display_surface.fill(white) pad_group.draw(display_surface) track.checkpoint(display_surface) delta_t = clock.tick(30) font = _load_font('./fonts/American Captain.ttf', 32) for event in pygame.event.get(): if not hasattr(event, 'key'): continue getEvent1(car, event, display_surface) getEvent2(car2, event, display_surface) # Update car and draw track carlap1 = font.render("Car 1 Laps completed: " + str(lap_car1) + "/5", True, (255, 255, 255)) carlap2 = font.render("Car 2 Laps completed: " + str(lap_car2) + "/5", True, (255, 255, 255)) display_surface.blit(carlap1, (0, 0)) display_surface.blit(carlap2, (0, 30)) car_group.update(delta_t) car_group.draw(display_surface) pygame.draw.rect(display_surface, (255, 0, 0), car.hitbox, 2) car_group2.update(delta_t) car_group2.draw(display_surface) pygame.draw.rect(display_surface, (255, 0, 0), car2.hitbox, 2) # Check if car is on track SetCarMaxSpeed(car, pad_group, car_group) SetCarMaxSpeed(car2, pad_group, car_group2) pygame.display.flip() collision(car, car2, display_surface) if checkOutOfBounds(car): car.reset(start_car1) if checkOutOfBounds(car2): car2.reset(start_car2) checkpoint_car1 = checkpoint1(car, checkpoint, checkpoint_car1) checkpoint_car2 = checkpoint1(car2, checkpoint, checkpoint_car2) if checkpoint_car1 >= 1: previouslapcar1 = lap_car1 lap_car1 = carLap(car, finish_line, lap_car1, "Lap finished for car 1!") if lap_car1 > previouslapcar1: if lap_car1 == 5: win(display_surface, "Car 1 Wins!") checkpoint_car1 = 0 if checkpoint_car2 >= 1: previouslapcar2 = lap_car2 lap_car2 = carLap(car2, finish_line, lap_car2, "Lap finished for car 2!") if lap_car2 > previouslapcar2: if lap_car2 == 5: win(display_surface, "Car 2 Wins!") checkpoint_car2 = 0 # pygame.draw.rect(display_surface, (255, 255, 255), (960, 0, 30, 125)) pygame.display.update()
def main_menu(screen): pygame.font.init() click = False offset = 0 start_position = (1105, 400) car = Car('images/f1sprite.png', start_position) car.direction = 0 car_group = pygame.sprite.Group(car) current_path = os.path.abspath(os.path.dirname(__file__)) absolute_path = os.path.join(current_path, './sounds/menu.mp3') mixer.init() mixer.music.load(absolute_path) mixer.music.set_volume(0.7) mixer.music.play(-1) while True: # --- Main event loop deltat = clock.tick(30) mx, my = pygame.mouse.get_pos() button1col = YELLOW button2col = YELLOW button3col = YELLOW button4col = YELLOW button5col = YELLOW button1 = pygame.Rect(50, 100, 500, 50) button2 = pygame.Rect(50, 200, 500, 50) button3 = pygame.Rect(50, 300, 500, 50) button4 = pygame.Rect(50, 400, 500, 50) button5 = pygame.Rect(50, 500, 500, 50) if button1.collidepoint((mx, my)): button1col = RED if click: if pick_track(screen): break if button2.collidepoint((mx, my)): button2col = RED if click: if pick_track_2player(screen): break if button3.collidepoint((mx, my)): button3col = RED if click: if pick_track_AI(screen): break if button4.collidepoint((mx, my)): button4col = RED if click: tutorial(screen) if button5.collidepoint((mx, my)): button5col = RED if click: options(screen) #screen.blit(background, (0, 0)) screen.fill(BLACK) if offset == 99: offset = 0 else: offset = offset + 1 drawroad(screen, offset) car_group.update(deltat) car_group.draw(screen) text('KARTING PROS', YELLOW, screen, 310, 20) pygame.draw.rect(screen, button1col, button1) pygame.draw.rect(screen, button2col, button2) pygame.draw.rect(screen, button3col, button3) pygame.draw.rect(screen, button4col, button4) pygame.draw.rect(screen, button5col, button5) text('Time Trial', BLACK, screen, 80, 120) text('Two-Player', BLACK, screen, 80, 220) text('AI_Versus', BLACK, screen, 80, 320) text('Tutorial', BLACK, screen, 80, 420) text('Options', BLACK, screen, 80, 520) click = False for event in pygame.event.get(): # User did something if event.type == pygame.QUIT: # If user clicked close sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: click = True pygame.display.update() clock.tick(60)
def options(screen): offset = 0 start_position = (1105, 400) car = Car('images/f1sprite.png', start_position) car.direction = 0 car_group = pygame.sprite.Group(car) in_opts = True JSON_FILE_NAME = "./settings.json" current_path = os.path.abspath(os.path.dirname(__file__)) absolute_settings_path = os.path.join(current_path, JSON_FILE_NAME) set_file = open(Path(absolute_settings_path), "r") set_json = json.load(set_file) set_file.close() # print(set_json) max_speed = int(set_json["max_forward_speed"]) rev_speed = int(set_json["max_reverse_speed"]) acc = int(set_json["acceleration"]) turn_speed = int(set_json["turn_speed"]) ot_speed = int(set_json["off_track_speed"]) ot_speed = int(set_json["off_track_speed"]) collision = bool(set_json["collision"]) hitbox = bool(set_json["draw_hitbox"]) aiDifficulty = bool(set_json["ai_difficulty_hard"]) while in_opts: deltat = clock.tick(30) screen.fill(BLACK) if offset == 99: offset = 0 else: offset = offset + 1 drawroad(screen, offset) car_group.update(deltat) car_group.draw(screen) click = False # screen.blit(BLACK, (0, 0)) text('Options', YELLOW, screen, 20, 20) text("Max Forward Speed: ", YELLOW, screen, 20, 100) max_speed_up = pygame.Rect(300, 75, 30, 20) max_speed_down = pygame.Rect(300, 125, 30, 20) text("Max Reverse Speed: ", YELLOW, screen, 20, 200) rev_speed_up = pygame.Rect(300, 175, 30, 20) rev_speed_down = pygame.Rect(300, 225, 30, 20) text("Acceleration Speed: ", YELLOW, screen, 20, 300) acc_up = pygame.Rect(300, 275, 30, 20) acc_down = pygame.Rect(300, 325, 30, 20) text("Turn Speed: ", YELLOW, screen, 20, 400) ts_up = pygame.Rect(300, 375, 30, 20) ts_down = pygame.Rect(300, 425, 30, 20) text("Off-Track Speed: ", YELLOW, screen, 20, 500) ot_up = pygame.Rect(300, 475, 30, 20) ot_down = pygame.Rect(300, 525, 30, 20) text("Collisions: ", YELLOW, screen, 20, 600) col_toggle = pygame.Rect(300, 585, 50, 50) text("Draw Hitboxes: ", YELLOW, screen, 20, 700) hb_toggle = pygame.Rect(300, 685, 50, 50) text("AI Difficulty Hard?: ", YELLOW, screen, 20, 800) ai_toggle = pygame.Rect(300, 785, 67, 50) for event in pygame.event.get(): # User did something if event.type == pygame.QUIT: # If user clicked close pygame.quit() # Flag that we are done so we exit this loop if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: set_json["max_forward_speed"] = max_speed set_json["max_reverse_speed"] = rev_speed set_json["acceleration"] = acc set_json["turn_speed"] = turn_speed set_json["off_track_speed"] = ot_speed set_json["collision"] = collision set_json["draw_hitbox"] = hitbox set_json["ai_difficulty_hard"] = aiDifficulty JSON_FILE_NAME = "./settings.json" current_path = os.path.abspath(os.path.dirname(__file__)) absolute_settings_path = os.path.join( current_path, JSON_FILE_NAME) set_file = open(absolute_settings_path, "w") json.dump(set_json, set_file) set_file.close() in_opts = False if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: click = True mx, my = pygame.mouse.get_pos() speedupcol = YELLOW speeddowncol = YELLOW revupcol = YELLOW revdowncol = YELLOW accupcol = YELLOW accdowncol = YELLOW tsupcol = YELLOW tsdowncol = YELLOW otupcol = YELLOW otdowncol = YELLOW if (collision): coll_col = RED else: coll_col = YELLOW if (hitbox): hb_col = RED else: hb_col = YELLOW if (aiDifficulty): ai_col = RED else: ai_col = YELLOW if max_speed_up.collidepoint((mx, my)): speedupcol = RED if click: if max_speed < 99: pygame.draw.rect(screen, BLACK, (300, 75, 50, 50)) max_speed = max_speed + 1 if max_speed_down.collidepoint((mx, my)): speeddowncol = RED if click: if max_speed > 1: pygame.draw.rect(screen, BLACK, (300, 75, 50, 50)) max_speed = max_speed - 1 if rev_speed_up.collidepoint((mx, my)): revupcol = RED if click: if rev_speed < 99: pygame.draw.rect(screen, BLACK, (300, 175, 50, 50)) rev_speed = rev_speed + 1 if rev_speed_down.collidepoint((mx, my)): revdowncol = RED if click: if rev_speed > 1: pygame.draw.rect(screen, BLACK, (300, 175, 50, 50)) rev_speed = rev_speed - 1 if acc_up.collidepoint((mx, my)): accupcol = RED if click: if acc < 99: pygame.draw.rect(screen, BLACK, (300, 275, 50, 50)) acc = acc + 1 if acc_down.collidepoint((mx, my)): accdowncol = RED if click: if acc > 1: pygame.draw.rect(screen, BLACK, (300, 275, 50, 50)) acc = acc - 1 if ts_up.collidepoint((mx, my)): tsupcol = RED if click: if turn_speed < 99: pygame.draw.rect(screen, BLACK, (300, 375, 50, 50)) turn_speed = turn_speed + 1 if ts_down.collidepoint((mx, my)): tsdowncol = RED if click: if turn_speed > 1: pygame.draw.rect(screen, BLACK, (300, 375, 50, 50)) turn_speed = turn_speed - 1 if ot_up.collidepoint((mx, my)): otupcol = RED if click: if ot_speed < 99: pygame.draw.rect(screen, BLACK, (300, 475, 50, 50)) ot_speed = ot_speed + 1 if ot_down.collidepoint((mx, my)): otdowncol = RED if click: if ot_speed > 1: pygame.draw.rect(screen, BLACK, (300, 475, 50, 50)) ot_speed = ot_speed - 1 if col_toggle.collidepoint((mx, my)): if click: collision = not collision if hb_toggle.collidepoint((mx, my)): if click: hitbox = not hitbox if ai_toggle.collidepoint((mx, my)): if click: aiDifficulty = not aiDifficulty pygame.draw.rect(screen, speedupcol, max_speed_up) pygame.draw.rect(screen, speeddowncol, max_speed_down) text("^", BLACK, screen, 307, 77) text("v", BLACK, screen, 307, 122) pygame.draw.rect(screen, revupcol, rev_speed_up) pygame.draw.rect(screen, revdowncol, rev_speed_down) text("^", BLACK, screen, 307, 177) text("v", BLACK, screen, 307, 222) pygame.draw.rect(screen, accupcol, acc_up) pygame.draw.rect(screen, accdowncol, acc_down) text("^", BLACK, screen, 307, 277) text("v", BLACK, screen, 307, 322) pygame.draw.rect(screen, tsupcol, ts_up) pygame.draw.rect(screen, tsdowncol, ts_down) text("^", BLACK, screen, 307, 377) text("v", BLACK, screen, 307, 422) pygame.draw.rect(screen, otupcol, ot_up) pygame.draw.rect(screen, otdowncol, ot_down) text("^", BLACK, screen, 307, 477) text("v", BLACK, screen, 307, 522) pygame.draw.rect(screen, coll_col, col_toggle) pygame.draw.rect(screen, hb_col, hb_toggle) pygame.draw.rect(screen, ai_col, ai_toggle) text(str(max_speed), YELLOW, screen, 300, 100) text(str(rev_speed), YELLOW, screen, 300, 200) text(str(acc), YELLOW, screen, 300, 300) text(str(turn_speed), YELLOW, screen, 300, 400) text(str(ot_speed), YELLOW, screen, 300, 500) if (collision): text("ON", BLACK, screen, 300, 600) else: text("OFF", BLACK, screen, 300, 600) if (hitbox): text("ON", BLACK, screen, 300, 700) else: text("OFF", BLACK, screen, 300, 700) if (aiDifficulty): text("HARD", BLACK, screen, 300, 800) else: text("EASY", BLACK, screen, 300, 800) pygame.display.update() clock.tick(60)
def T2_AI(display_surface): track1 = track2.Track2() white = (0, 128, 0) track = _load_image('./images/track2.png') # Official timer clock = pygame.time.Clock() t0 = time.time() # Setup car objects start_car1 = (1010, 75) car = Car('./images/f1sprite.png', start_car1) car_group = pygame.sprite.Group(car) car.setDefaultValues() # AI Car start_car2 = (1010, 144) car2 = Car('./images/f1sprite2.png', start_car2) car_group2 = pygame.sprite.Group(car2) car2.setDefaultValues() # AI Moves current_path = os.path.abspath(os.path.dirname(__file__)) if settings.getSetting('ai_difficulty_hard') == False: absolute_image_path = os.path.join( current_path, './ArtificalIntelligence/track2_easy.npy') else: absolute_image_path = os.path.join( current_path, './ArtificalIntelligence/track2_hard.npy') moves = np.load(absolute_image_path) moveNum = 0 # Groups for pads and finish line pad_group = track1.getPads() finish_line = track1.getFinishLine() # Setup lap logic checkpoint = (960, 845, 10, 125) lap_car1 = 0 checkpoint_car1 = 0 lap_car2 = 0 checkpoint_car2 = 0 # Countdown timer logic countdownTimerStart = time.time() countdownFinished = False collisions = settings.getSetting('collision') draw_hitbox = settings.getSetting('draw_hitbox') # Music for countdown sound current_path = os.path.abspath(os.path.dirname(__file__)) absolute_image_path = os.path.join( current_path, './sounds/race_coundown.mp3') mixer.init() mixer.music.load(absolute_image_path) mixer.music.set_volume(0.7) mixer.music.play() crowd = mixer.Sound(os.path.join(current_path, './sounds/crowd.wav')) right_press, left_press, up_press, down_press = 0, 0, 0, 0 while True: # Draw the Track display_surface.fill(white) display_surface.blit(track, (0, 0)) track2.checkpoint(display_surface) delta_t = clock.tick(30) font = _load_font('./fonts/American Captain.ttf', 32) for event in pygame.event.get(): if event.type == QUIT: sys.exit(0) if not hasattr(event, 'key'): continue if event.key == K_RIGHT: right_press = 1 elif event.key == K_SPACE: car.speed = 0 elif event.key == K_LEFT: left_press = 1 elif event.key == K_UP: mixer.music.load(os.path.join(os.path.abspath(os.path.dirname(__file__)), './sounds/rev.mp3')) mixer.music.play(-1) up_press = 1 elif event.key == K_DOWN: down_press = 1 elif event.key == K_ESCAPE: mixer.music.stop() mixer.Sound.stop(crowd) mainmenu.main_menu(display_surface) if event.type == KEYUP: if event.key == pygame.K_RIGHT: right_press = 0 elif event.key == pygame.K_LEFT: left_press = 0 elif event.key == pygame.K_UP: mixer.music.stop() up_press = 0 elif event.key == pygame.K_DOWN: down_press = 0 # Human Movement System car.k_right = right_press * -5 car.k_left = left_press * 5 car.k_up = up_press * 2 car.k_down = down_press * -2 # Friction component if up_press == 0 and down_press == 0 and int(car.speed) != 0: car.k_down = -.2 car.k_up = 0 # AI Movement System car2.k_right = moves[moveNum][0] * -5 car2.k_left = moves[moveNum][1] * 5 car2.k_up = moves[moveNum][2] * 2 car2.k_down = moves[moveNum][3] * -2 moveNum += 1 # Update car and draw track carlap1 = font.render("Car 1 Laps completed: " + str(lap_car1) + "/5", True, (255, 255, 255)) carlap2 = font.render("Car 2 Laps completed: " + str(lap_car2) + "/5", True, (255, 255, 255)) # Lap count for Cars display_surface.blit(carlap1, (0, 0)) display_surface.blit(carlap2, (0, 30)) # Update and draw car car_group.update(delta_t) car_group.draw(display_surface) # Update and draw car car_group2.update(delta_t) car_group2.draw(display_surface) if draw_hitbox: pygame.draw.rect(display_surface, (255, 0, 0), car.hitbox, 2) pygame.draw.rect(display_surface, (255, 0, 0), car2.hitbox, 2) # Check if car is on track SetCarMaxSpeed(car, pad_group, car_group) SetCarMaxSpeed(car2, pad_group, car_group2) pygame.display.flip() if collisions: collision(car, car2, display_surface) if checkOutOfBounds(car): car.reset(start_car1) if checkOutOfBounds(car2): car2.reset(start_car2) checkpoint_car1 = checkpoint1(car, checkpoint, checkpoint_car1) checkpoint_car2 = checkpoint1(car2, checkpoint, checkpoint_car2) if checkpoint_car1 >= 1: previouslapcar1 = lap_car1 lap_car1 = carLap(car, finish_line, lap_car1, "Lap finished for car 1!") if lap_car1 > previouslapcar1: mixer.Sound.play(crowd) if lap_car1 == 5: mixer.music.stop() mixer.Sound.stop(crowd) win(display_surface, "car1") checkpoint_car1 = 0 if checkpoint_car2 >= 1: previouslapcar2 = lap_car2 lap_car2 = carLap(car2, finish_line, lap_car2, "Lap finished for car 2!") if lap_car2 > previouslapcar2: if lap_car2 == 5: mixer.music.stop() mixer.Sound.stop(crowd) win(display_surface, "ai") checkpoint_car2 = 0 while (time.time() - countdownTimerStart < 4): image = _load_image('./images/starting_lights/lights' + str(int(time.time() - countdownTimerStart) + 1) + '.png') display_surface.blit(image, ((1920 / 2) - (768 / 2), 50)) fontBig = _load_font('./fonts/American Captain.ttf', 64) countdown_text = font.render( "Time: " + str(4 - t0), True, (255, 255, 255)) t0 = time.time() t1 = time.time() dt = t1 - t0 countdownFinished = True pygame.display.update() # pygame.draw.rect(display_surface, (255, 255, 255), (960, 0, 30, 125)) pygame.display.update()
def RaceCars(display_surface): track1 = track2.Track2() white = (0, 128, 0) track = _load_image('./images/track2.png') # Official timer clock = pygame.time.Clock() t0 = time.time() # Setup car objects start_car1 = (1010, 144) car = Car('./images/f1sprite.png', start_car1) car_group = pygame.sprite.Group(car) start_car2 = (1010, 75) car2 = Car('./images/f1sprite2.png', start_car2) car_group2 = pygame.sprite.Group(car2) # Groups for pads and finish line pad_group = track1.getPads() finish_line = track1.getFinishLine() # Setup lap logic checkpoint = (960, 845, 10, 125) lap_car1 = 0 checkpoint_car1 = 0 lap_car2 = 0 checkpoint_car2 = 0 # Collision Check collisions = settings.getSetting('collision') draw_hitbox = settings.getSetting('draw_hitbox') # Countdown timer logic countdownTimerStart = time.time() countdownFinished = False # Music for countdown sound current_path = os.path.abspath(os.path.dirname(__file__)) absolute_image_path = os.path.join(current_path, './sounds/race_coundown.mp3') mixer.init() mixer.music.load(absolute_image_path) mixer.music.set_volume(0.7) mixer.music.play() crowd = mixer.Sound(os.path.join(current_path, './sounds/crowd.wav')) # Movement System right_press1, left_press1, up_press1, down_press1 = 0, 0, 0, 0 right_press2, left_press2, up_press2, down_press2 = 0, 0, 0, 0 while True: # Draw the Track display_surface.fill(white) display_surface.blit(track, (0, 0)) track2.checkpoint(display_surface) delta_t = clock.tick(30) font = _load_font('./fonts/American Captain.ttf', 32) for event in pygame.event.get(): if event.type == QUIT: sys.exit(0) if not hasattr(event, 'key'): continue # Player 1 Movement if event.key == K_RIGHT: right_press1 = 1 elif event.key == K_SPACE: car.speed = 0 elif event.key == K_LEFT: left_press1 = 1 elif event.key == K_UP: mixer.music.load(os.path.join(current_path, './sounds/rev.mp3')) mixer.music.play(-1) up_press1 = 1 elif event.key == K_DOWN: down_press1 = 1 elif event.key == K_ESCAPE: mixer.music.stop() mixer.Sound.stop(crowd) mainmenu.main_menu(display_surface) if event.type == KEYUP: if event.key == pygame.K_RIGHT: right_press1 = 0 elif event.key == pygame.K_LEFT: left_press1 = 0 elif event.key == pygame.K_UP: mixer.music.stop() up_press1 = 0 elif event.key == pygame.K_DOWN: down_press1 = 0 # Player 2 Movement if event.key == K_d: right_press2 = 1 elif event.key == K_LSHIFT: car.speed = 0 elif event.key == K_a: left_press2 = 1 elif event.key == K_w: mixer.music.load(os.path.join(current_path, './sounds/rev.mp3')) mixer.music.play(-1) up_press2 = 1 elif event.key == K_s: down_press2 = 1 elif event.key == K_ESCAPE: mixer.Sound.stop(crowd) mixer.music.stop() mainmenu.main_menu(display_surface) if event.type == KEYUP: if event.key == pygame.K_d: right_press2 = 0 elif event.key == pygame.K_a: left_press2 = 0 elif event.key == pygame.K_w: mixer.music.stop() up_press2 = 0 elif event.key == pygame.K_s: down_press2 = 0 car.k_right = right_press1 * -5 car.k_left = left_press1 * 5 car.k_up = up_press1 * 2 car.k_down = down_press1 * -2 if up_press1 == 0 and down_press1 == 0 and int(car.speed) != 0: car.k_down = -.2 car.k_up = 0 car2.k_right = right_press2 * -5 car2.k_left = left_press2 * 5 car2.k_up = up_press2 * 2 car2.k_down = down_press2 * -2 if up_press2 == 0 and down_press2 == 0 and int(car2.speed) != 0: car2.k_down = -.2 car2.k_up = 0 # Update car and draw track carlap1 = font.render("Car 1 Laps completed: " + str(lap_car1) + "/5", True, (255, 255, 255)) carlap2 = font.render("Car 2 Laps completed: " + str(lap_car2) + "/5", True, (255, 255, 255)) # Lap count for Cars display_surface.blit(carlap1, (0, 0)) display_surface.blit(carlap2, (0, 30)) # Update and draw car car_group.update(delta_t) car_group.draw(display_surface) # Update and draw car car_group2.update(delta_t) car_group2.draw(display_surface) if draw_hitbox: pygame.draw.rect(display_surface, (255, 0, 0), car.hitbox, 2) pygame.draw.rect(display_surface, (255, 0, 0), car2.hitbox, 2) # Check if car1 is on track on_track = pygame.sprite.groupcollide(car_group, pad_group, False, False) # Slow down car1 if not on track if not on_track: car.setOffTrackSpeed() else: car.setRegularSpeed() # Slow down car2 if not on track on_track = pygame.sprite.groupcollide(car_group2, pad_group, False, False) # Slow down car2 if not on track if not on_track: car2.setOffTrackSpeed() else: car2.setRegularSpeed() pygame.display.flip() # Check for collisions only if enabled if collisions: collision(car, car2, display_surface) if checkOutOfBounds(car): car.reset(start_car1) if checkOutOfBounds(car2): car2.reset(start_car2) checkpoint_car1 = checkpoint1(car, checkpoint, checkpoint_car1) checkpoint_car2 = checkpoint1(car2, checkpoint, checkpoint_car2) if checkpoint_car1 >= 1: previouslapcar1 = lap_car1 lap_car1 = carLap(car, finish_line, lap_car1, "Lap finished for car 1!") if lap_car1 > previouslapcar1: mixer.Sound.play(crowd) if lap_car1 == 5: mixer.music.stop() mixer.Sound.stop(crowd) win(display_surface, "p1") checkpoint_car1 = 0 if checkpoint_car2 >= 1: previouslapcar2 = lap_car2 lap_car2 = carLap(car2, finish_line, lap_car2, "Lap finished for car 2!") if lap_car2 > previouslapcar2: mixer.Sound.play(crowd) if lap_car2 == 5: mixer.music.stop() mixer.Sound.stop(crowd) win(display_surface, "p2") checkpoint_car2 = 0 while (time.time() - countdownTimerStart < 4): image = _load_image( './images/starting_lights/lights' + str(int(time.time() - countdownTimerStart) + 1) + '.png') display_surface.blit(image, ((1920 / 2) - (768 / 2), 50)) fontBig = _load_font('./fonts/American Captain.ttf', 64) countdown_text = font.render("Time: " + str(4 - t0), True, (255, 255, 255)) t0 = time.time() t1 = time.time() dt = t1 - t0 countdownFinished = True pygame.display.update() # pygame.draw.rect(display_surface, (255, 255, 255), (960, 0, 30, 125)) pygame.display.update()
def timeTrial(display_surface): best_lap_time = 30000 track = _load_image('./images/track2.png') track1 = track2.Track2() white = (0, 128, 0) clock = pygame.time.Clock() t0 = time.time() # Car Setup start_position = (1010, 144) car = Car('./images/f1sprite.png', start_position) car_group = pygame.sprite.Group(car) # Lap logic checkpoint_check = 0 pad_group = track1.getPads() finish_line = (960, 50, 20, 125) checkpoint = (960, 845, 10, 125) # Countdown timer logic countdownTimerStart = time.time() countdownFinished = False # Music for countdown sound current_path = os.path.abspath(os.path.dirname(__file__)) absolute_path = os.path.join( current_path, './sounds/race_coundown.mp3') print(absolute_path) mixer.init() mixer.music.load(absolute_path) mixer.music.set_volume(0.7) mixer.music.play() crowd = mixer.Sound(os.path.join(current_path, './sounds/crowd.wav')) data_collection = settings.getSetting('collect_data_for_AI') draw_hitbox = settings.getSetting('draw_hitbox') i = 0 if data_collection: # Data collection for machine learning features = [] labels = [] right_press, left_press, up_press, down_press = 0, 0, 0, 0 while True: if data_collection: # Machine Learning Features # Direction (%360), Position.X, Position.Y feature = [] # Label(right,left,up,down)(1 or 0 for all) label = [] # Draw the Track display_surface.fill(white) display_surface.blit(track, (0, 0)) font = _load_font('./fonts/American Captain.ttf', 32) if data_collection: feature.append(car.direction % 360) feature.append(int(car.position[0])) feature.append(int(car.position[1])) feature = np.array(feature) feature = feature / feature.max(axis=0) features.append(feature) track2.checkpoint(display_surface) deltat = clock.tick(30) # Update Car and draw car_group.update(deltat) car_group.draw(display_surface) t1 = time.time() dt = t1-t0 for event in pygame.event.get(): if event.type == QUIT: sys.exit(0) if not hasattr(event, 'key'): continue if event.key == K_RIGHT: right_press = 1 elif event.key == K_SPACE: car.speed = 0 elif event.key == K_LEFT: left_press = 1 elif event.key == K_UP: mixer.music.load(os.path.join( current_path, './sounds/rev.mp3')) mixer.music.play(-1) up_press = 1 elif event.key == K_DOWN: down_press = 1 elif event.key == K_ESCAPE: mixer.music.stop() mixer.Sound.stop(crowd) if data_collection: np.save('features.npy', np.array(features)) np.save('labels.npy', np.array(labels)) mainmenu.main_menu(display_surface) if event.type == KEYUP: if event.key == pygame.K_RIGHT: right_press = 0 elif event.key == pygame.K_LEFT: left_press = 0 elif event.key == pygame.K_UP: mixer.music.stop() up_press = 0 elif event.key == pygame.K_DOWN: down_press = 0 car.k_right = right_press * -5 car.k_left = left_press * 5 car.k_up = up_press * 2 car.k_down = down_press * -2 if up_press == 0 and down_press == 0 and int(car.speed) != 0: car.k_down = -.2 car.k_up = 0 if data_collection: labels.append([right_press, left_press, up_press, down_press]) # Check if car is on track on_track = pygame.sprite.groupcollide( car_group, pad_group, False, False) # Slow down car if not on track if not on_track: car.setOffTrackSpeed() else: car.setRegularSpeed() if draw_hitbox: pygame.draw.rect(display_surface, (255, 0, 0), car.hitbox, 2) pygame.display.flip() checkpoint_check = checkpoint1(car, checkpoint, checkpoint_check) # Countdown Timer Logic (program does not move forward until this is finished) while(time.time()-countdownTimerStart < 4): image = _load_image('./images/starting_lights/lights' + str(int(time.time()-countdownTimerStart)+1)+'.png') display_surface.blit(image, ((1920/2)-(768/2), 50)) fontBig = _load_font('./fonts/American Captain.ttf', 64) t0 = time.time() t1 = time.time() dt = t1-t0 countdownFinished = True pygame.display.update() if(countdownFinished): # Timer timer_text = font.render( "Time: " + str(round(dt, 3)), True, (255, 255, 255)) display_surface.blit(timer_text, (0, 0)) # Time to Beat if best_lap_time != 30000: best_lap_text = font.render( "Time to Beat: "+str(best_lap_time), True, (255, 255, 255)) display_surface.blit(best_lap_text, (0, 30)) if checkpoint_check >= 1: if completeLap(car, finish_line): mixer.Sound.play(crowd) if dt < best_lap_time: best_lap_time = round(dt, 3) t0, t1 = time.time(), time.time() checkpoint_check = 0 # If car is out of screen if checkOutOfBounds(car): car.reset(start_position) pygame.display.update()