def main(): os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (640, 50) pygame.init() screen = pygame.display.set_mode( (int(pygame.display.Info().current_w / 3), int(pygame.display.Info().current_h / 2))) background = pygame.Surface(screen.get_size()) background = background.convert_alpha() background.fill((39, 174, 96)) CENTER_W = int(pygame.display.Info().current_w / 2) CENTER_H = int(pygame.display.Info().current_h / 2) GREEN = 174 clock = pygame.time.Clock() network = Network() players = network.get_players() car = pygame_classes.from_state(players[1]) car2 = pygame_classes.from_state(players[0]) cam = pygame_classes.Camera() cup = pygame_classes.Cup(3, 9) bound_alert = pygame_classes.BoundsAlert() win_alert = pygame_classes.WinAlert() map_s = pygame.sprite.Group() cup_s = pygame.sprite.Group() bound_alert_s = pygame.sprite.Group() win_alert_s = pygame.sprite.Group() map_tile = [ 'sand8.png', 'sand1.png', 'sand2.png', 'sand3.png', 'sand4.png', 'sand5.png', 'sand6.png', 'grass.png', 'tribune.png', 'tree.png' ] map = [[7, 2, 3, 3, 3, 4, 7, 2, 3, 4], [7, 1, 7, 7, 7, 1, 7, 1, 7, 1], [8, 7, 8, 8, 7, 1, 7, 1, 7, 1], [2, 3, 3, 4, 9, 1, 7, 1, 7, 1], [1, 2, 3, 6, 9, 1, 8, 1, 7, 1], [1, 1, 8, 8, 7, 5, 3, 6, 9, 1], [1, 5, 3, 3, 4, 7, 7, 7, 9, 1], [1, 7, 7, 7, 1, 7, 9, 9, 7, 1], [1, 8, 8, 8, 5, 3, 3, 3, 3, 6], [5, 3, 3, 0, 7, 7, 7, 7, 7, 7]] pygame_classes.map_files.clear() for tile_num in range(0, len(map_tile)): pygame_classes.map_files.append( pygame_classes.load_image(map_tile[tile_num], False)) for x in range(0, 10): for y in range(0, 10): map_s.add(pygame_classes.Map(map[x][y], x * 500, y * 500)) cup_s.add(cup) bound_alert_s.add(bound_alert) win_alert_s.add(win_alert) cam.set_position(car.x, car.y) is_out_of_road = False win = False running = True while running: players = network.receive() car = pygame_classes.from_state(players[1]) # Reset if car.x == 420.0 and car.y == 270.0: is_out_of_road = False win = False car.rect.topleft = 320, 270 car2 = pygame_classes.from_state(players[0]) car.steer_left() car2.steer_left() for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: running = False break cam.set_position(car.x, car.y) screen.blit(background, (0, 0)) map_s.update(cam.x, cam.y) map_s.draw(screen) cup_s.update(cam.x, cam.y) cup_s.draw(screen) car.update(cam.x, cam.y) car.draw_additional_car(screen) car2.update_additional_car(cam.x, cam.y) car2.draw_additional_car(screen) if pygame_classes.is_out_of_map( car.x + CENTER_W, car.y + CENTER_H) or car.is_out_of_road( screen.get_at( (int(CENTER_W), int(CENTER_H))).g, GREEN, GREEN): car.speed = 0 is_out_of_road = True bound_alert_s.draw(screen) if pygame.sprite.spritecollide(car, cup_s, True): car.speed = 0 win = True if win: win_alert_s.draw(screen) flags = [is_out_of_road, win] network.send_flags(flags) pygame.display.flip() clock.tick(64)
def main(): screen = pygame.display.set_mode((pygame.display.Info().current_w, pygame.display.Info().current_h), pygame.FULLSCREEN) background = pygame.Surface(screen.get_size()) background = background.convert_alpha() background.fill((39, 174, 96)) CENTER_W = int(pygame.display.Info().current_w / 2) CENTER_H = int(pygame.display.Info().current_h / 2) GRASS = 174 clock = pygame.time.Clock() font = pygame.font.Font(None, 50) # Initialize objects. car = pygame_classes.Player(car_customization.change_color(), CENTER_W, CENTER_H) car.x -= 200 car.dir = 180 car.steer_left() cam = pygame_classes.Camera() target = pygame_classes.Finish(8, 1) bound_alert = pygame_classes.BoundsAlert() time_alert = pygame_classes.TimeAlert() win_alert = pygame_classes.WinAlert() # Create sprite groups. map_s = pygame.sprite.Group() player_s = pygame.sprite.Group() tracks_s = pygame.sprite.Group() target_s = pygame.sprite.Group() timer_alert_s = pygame.sprite.Group() bound_alert_s = pygame.sprite.Group() win_alert_s = pygame.sprite.Group() map_tile = ['asphalt0.png', 'asphalt1.png', 'asphalt2.png', 'asphalt3.png', 'asphalt4.png', 'race.png', 'tree.png', 'tribune.png', 'grass.png', 'band.png'] # Constructing map from tiles map = [ [5, 8, 5, 6, 4, 8, 8, 8, 8, 8], [5, 8, 5, 6, 7, 7, 7, 7, 0, 5], [5, 8, 5, 1, 5, 3, 3, 5, 1, 5], [8, 8, 5, 1, 5, 9, 9, 5, 1, 5], [8, 8, 5, 1, 5, 3, 3, 5, 1, 5], [8, 8, 5, 1, 5, 8, 7, 5, 1, 5], [5, 6, 5, 1, 8, 8, 2, 8, 1, 5], [5, 6, 5, 1, 7, 7, 7, 7, 1, 5], [5, 6, 5, 2, 3, 3, 3, 3, 4, 9], [5, 6, 5, 8, 8, 8, 8, 8, 8, 9] ] pygame_classes.map_files.clear() # Generate tiles for tile_num in range(0, len(map_tile)): pygame_classes.map_files.append(pygame_classes.load_image(map_tile[tile_num], False)) for x in range(0, 10): for y in range(0, 10): map_s.add(pygame_classes.Map(map[x][y], x * 500, y * 500)) # Load tracks pygame_classes.initialize_tracks() # Load finish target_s.add(target) # Load alerts timer_alert_s.add(time_alert) bound_alert_s.add(bound_alert) win_alert_s.add(win_alert) # Load car player_s.add(car) # Load camera cam.set_position(car.x, car.y) # Conditions for winning race and collisions with trophy win = None collided = False running = True while running: for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: car.reset() car.dir = 180 car.steer_left() car.x -= 200 target.reset() win = None collided = False elif event.key == pygame.K_ESCAPE: running = False break keys = pygame.key.get_pressed() if target.time_left > 0 and win == None: if keys[K_LEFT]: car.steer_left() if keys[K_RIGHT]: car.steer_right() if keys[K_UP]: car.accelerate() else: car.soften() if keys[K_DOWN]: car.deaccelerate() cam.set_position(car.x, car.y) text_timer = font.render( 'Timer: ' + str(int((target.time_left / 60) / 60)) + ":" + str(int((target.time_left / 60) % 60)), 1, (255, 255, 255)) # Render Scene. screen.blit(background, (0, 0)) map_s.update(cam.x, cam.y) map_s.draw(screen) # Conditional renders/effects collision(car) car.slow_down_on_grass(screen.get_at((int(CENTER_W - 5), int(CENTER_H - 5))).g, GRASS) if car.tracks: tracks_s.add(pygame_classes.Track(cam.x + CENTER_W, cam.y + CENTER_H, car.dir)) # Just render tracks_s.update(cam.x, cam.y) tracks_s.draw(screen) player_s.update(cam.x, cam.y) player_s.draw(screen) target_s.update(cam.x, cam.y) target_s.draw(screen) # Conditional renders if pygame_classes.is_out_of_map(car.x + CENTER_W, car.y + CENTER_H): bound_alert_s.update() bound_alert_s.draw(screen) if target.time_left == 0: timer_alert_s.draw(screen) car.speed = 0 win = False if pygame.sprite.spritecollide(car, target_s, True): car.speed = 0 win = True collided = True if collided: win_alert_s.draw(screen) running = False # Blit screen.blit(text_timer, (CENTER_W - 600, CENTER_H - 300)) pygame.display.flip() clock.tick(64) pygame.time.wait(1000) level2.main()
def main(): screen = pygame.display.set_mode((pygame.display.Info().current_w, pygame.display.Info().current_h), pygame.FULLSCREEN) background = pygame.Surface(screen.get_size()) background = background.convert_alpha() background.fill((39, 174, 96)) CENTER_W = int(pygame.display.Info().current_w / 2) CENTER_H = int(pygame.display.Info().current_h / 2) BARRIER_BLUE = 187 BARRIER_WHITE = 238 ROAD_BRIGHT_COLOR = 177 ROAD_DARK_COLOR = 187 GRASS = 174 clock = pygame.time.Clock() font = pygame.font.Font(None, 50) car = pygame_classes.Player(car_customization.change_color(), CENTER_W, CENTER_H) car.x -= 200 cam = pygame_classes.Camera() target = pygame_classes.Finish(9, 7) bound_alert = pygame_classes.BoundsAlert() time_alert = pygame_classes.TimeAlert() win_alert = pygame_classes.WinAlert() map_s = pygame.sprite.Group() player_s = pygame.sprite.Group() tracks_s = pygame.sprite.Group() target_s = pygame.sprite.Group() timer_alert_s = pygame.sprite.Group() bound_alert_s = pygame.sprite.Group() win_alert_s = pygame.sprite.Group() map_tile = ['mud1.png', 'mud2.png', 'mud3.png', 'mud4.png', 'mud5.png', 'mud6.png', 'mud7.png', 'race.png', 'tree.png', 'tribune.png', 'grass.png'] map = [ [9, 9, 9, 2, 0, 0, 0, 0, 3, 8], [2, 0, 3, 1, 10, 10, 10, 10, 1, 8], [1, 10, 1, 1, 10, 2, 0, 3, 1, 8], [1, 10, 1, 10, 8, 1, 8, 1, 1, 8], [1, 10, 1, 8, 8, 1, 10, 1, 1, 8], [1, 10, 1, 10, 10, 1, 8, 4, 5, 9], [1, 10, 1, 10, 8, 1, 8, 8, 7, 10], [1, 10, 1, 8, 10, 1, 10, 8, 7, 6], [1, 10, 4, 0, 0, 5, 9, 9, 7, 1], [4, 0, 0, 0, 0, 0, 0, 0, 0, 5] ] pygame_classes.map_files.clear() for tile_num in range(0, len(map_tile)): pygame_classes.map_files.append(pygame_classes.load_image(map_tile[tile_num], False)) for x in range(0, 10): for y in range(0, 10): map_s.add(pygame_classes.Map(map[x][y], x * 500, y * 500)) pygame_classes.initialize_tracks() target_s.add(target) timer_alert_s.add(time_alert) bound_alert_s.add(bound_alert) win_alert_s.add(win_alert) player_s.add(car) cam.set_position(car.x, car.y) win = None current_crashes_number = 0 crashes_limit = 5 collided = False crash = False running = True while running: for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: car.reset() car.x -= 200 target.reset() current_crashes_number = 0 win = None collided = False crash = False elif event.key == pygame.K_ESCAPE: running = False break keys = pygame.key.get_pressed() if target.time_left > 0 and win is None: if keys[K_LEFT]: car.steer_left() if keys[K_RIGHT]: car.steer_right() if keys[K_UP]: car.accelerate() else: car.soften() if keys[K_DOWN]: car.deaccelerate() cam.set_position(car.x, car.y) text_timer = font.render( 'Timer: ' + str(int((target.time_left / 60) / 60)) + ":" + str(int((target.time_left / 60) % 60)), 1, (255, 255, 255)) text_crashes_limit = font.render("Actual limit of crashes: " + str(crashes_limit - current_crashes_number), 1, (255, 255, 255)) screen.blit(background, (0, 0)) map_s.update(cam.x, cam.y) map_s.draw(screen) car.slow_down_on_grass(screen.get_at((int(CENTER_W - 5), int(CENTER_H - 5))).g, GRASS) if car.tracks: tracks_s.add(pygame_classes.Track(cam.x + CENTER_W, cam.y + CENTER_H, car.dir)) tracks_s.update(cam.x, cam.y) tracks_s.draw(screen) player_s.update(cam.x, cam.y) player_s.draw(screen) target_s.update(cam.x, cam.y) target_s.draw(screen) if car.is_collision(screen, BARRIER_WHITE, BARRIER_BLUE): if current_crashes_number == crashes_limit: car.speed = 0 win = False crash = True else: car.speed = 0 if car.is_out_of_road(screen.get_at((car.rect.left + car.rect.width, car.rect.top - car.rect.height)).r, ROAD_BRIGHT_COLOR, ROAD_DARK_COLOR): car.x = car.x + car.rect.width car.y = car.y - 0.5 * car.rect.height elif car.is_out_of_road( screen.get_at((car.rect.right - car.rect.width, car.rect.top - car.rect.height)).r, ROAD_BRIGHT_COLOR, ROAD_DARK_COLOR): car.x = car.x - car.rect.width car.y = car.y - 0.5 * car.rect.height elif car.is_out_of_road( screen.get_at((car.rect.left + car.rect.width, car.rect.bottom + car.rect.height)).r, ROAD_BRIGHT_COLOR, ROAD_DARK_COLOR): car.x = car.x + car.rect.width car.y = car.y + car.rect.height elif car.is_out_of_road( screen.get_at((car.rect.right - car.rect.width, car.rect.bottom + car.rect.height)).r, ROAD_BRIGHT_COLOR, ROAD_DARK_COLOR): car.x = car.x - car.rect.width car.y = car.y + car.rect.height current_crashes_number += 1 if crash: bound_alert_s.update() bound_alert_s.draw(screen) if target.time_left == 0: timer_alert_s.draw(screen) car.speed = 0 win = False if pygame.sprite.spritecollide(car, target_s, True): car.speed = 0 win = True collided = True if collided: win_alert_s.draw(screen) running = False screen.blit(text_timer, (CENTER_W - 600, CENTER_H - 300)) screen.blit(text_crashes_limit, (CENTER_W - 600, CENTER_H - 240)) pygame.display.flip() clock.tick(64) pygame.time.wait(1000) level3.main()
def main(): screen = pygame.display.set_mode( (pygame.display.Info().current_w, pygame.display.Info().current_h), pygame.FULLSCREEN) background = pygame.Surface(screen.get_size()) background = background.convert_alpha() background.fill((39, 174, 96)) CENTER_W = int(pygame.display.Info().current_w / 2) CENTER_H = int(pygame.display.Info().current_h / 2) GREEN1 = 174 GREEN2 = 192 clock = pygame.time.Clock() font = pygame.font.Font(None, 50) car = pygame_classes.Player(car_customization.change_color(), CENTER_W, CENTER_H) car.x -= 200 cam = pygame_classes.Camera() target = pygame_classes.Finish(8, 9) bound_alert = pygame_classes.BoundsAlert() time_alert = pygame_classes.TimeAlert() win_alert = pygame_classes.WinAlert() map_s = pygame.sprite.Group() player_s = pygame.sprite.Group() tracks_s = pygame.sprite.Group() target_s = pygame.sprite.Group() timer_alert_s = pygame.sprite.Group() bound_alert_s = pygame.sprite.Group() bomb_s = pygame.sprite.Group() win_alert_s = pygame.sprite.Group() map_tile = [ 'sand0.png', 'grass1.png', 'grass2.png', 'grass3.png', 'grass4.png', 'grass5.png', 'grass6.png', 'race.png', 'tree.png', 'tribune.png', 'grass.png', 'band.png' ] map = [[11, 9, 3, 3, 4, 3, 3, 2, 9, 11], [11, 4, 3, 2, 1, 7, 7, 5, 2, 11], [11, 1, 7, 1, 1, 7, 7, 8, 1, 11], [11, 1, 7, 7, 1, 7, 7, 4, 6, 11], [11, 1, 7, 7, 1, 7, 7, 1, 7, 11], [11, 5, 3, 3, 6, 7, 7, 1, 7, 11], [11, 4, 3, 3, 2, 4, 3, 6, 7, 11], [11, 1, 7, 7, 5, 6, 8, 8, 7, 11], [11, 5, 3, 3, 3, 2, 8, 8, 7, 11], [11, 10, 10, 8, 8, 5, 3, 3, 3, 11]] pygame_classes.map_files.clear() for tile_num in range(0, len(map_tile)): pygame_classes.map_files.append( pygame_classes.load_image(map_tile[tile_num], False)) for x in range(0, 10): for y in range(0, 10): map_s.add(pygame_classes.Map(map[x][y], x * 500, y * 500)) for i in range(6): x = randint(0, 9) y = randint(0, 9) while map[x][y] != 1: x = randint(0, 9) y = randint(0, 9) bomb = pygame_classes.Bomb(y, x) bomb_s.add(bomb) # load tracks pygame_classes.initialize_tracks() target_s.add(target) timer_alert_s.add(time_alert) bound_alert_s.add(bound_alert) win_alert_s.add(win_alert) player_s.add(car) cam.set_position(car.x, car.y) win = None collided = False bombs = False running = True while running: for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: car.reset() car.x -= 200 target.reset() bomb.reset() win = None bombs = False collided = False elif event.key == pygame.K_ESCAPE: running = False break keys = pygame.key.get_pressed() if target.time_left > 0 and win == None: if keys[K_LEFT]: car.steer_left() if keys[K_RIGHT]: car.steer_right() if keys[K_UP]: car.accelerate() else: car.soften() if keys[K_DOWN]: car.deaccelerate() cam.set_position(car.x, car.y) text_timer = font.render( 'Timer: ' + str(int((target.time_left / 60) / 60)) + ":" + str(int((target.time_left / 60) % 60)), 1, (255, 255, 255)) screen.blit(background, (0, 0)) map_s.update(cam.x, cam.y) map_s.draw(screen) if car.tracks: tracks_s.add( pygame_classes.Track(cam.x + CENTER_W, cam.y + CENTER_H, car.dir)) tracks_s.update(cam.x, cam.y) tracks_s.draw(screen) player_s.update(cam.x, cam.y) player_s.draw(screen) target_s.update(cam.x, cam.y) target_s.draw(screen) bomb_s.update(cam.x, cam.y) bomb_s.draw(screen) if pygame_classes.is_out_of_map( car.x + CENTER_W, car.y + CENTER_H) or car.is_out_of_road( screen.get_at( (int(CENTER_W), int(CENTER_H))).g, GREEN1, GREEN2): car.speed = 0 win = False bound_alert_s.update() bound_alert_s.draw(screen) if target.time_left == 0: timer_alert_s.draw(screen) win = False car.speed = 0 if pygame.sprite.spritecollide(car, bomb_s, True): car.speed = 0 win = False bombs = True if bombs: bound_alert_s.update() bound_alert_s.draw(screen) if pygame.sprite.spritecollide(car, target_s, True): car.speed = 0 win = True collided = True if collided: win_alert_s.draw(screen) pygame.time.wait(1000) screen.blit(text_timer, (CENTER_W - 600, CENTER_H - 300)) pygame.display.flip() clock.tick(64)
def main(): screen = pygame.display.set_mode( (pygame.display.Info().current_w, pygame.display.Info().current_h), pygame.FULLSCREEN) background = pygame.Surface(screen.get_size()) background = background.convert_alpha() background.fill((39, 174, 96)) CENTER_W = int(pygame.display.Info().current_w / 2) CENTER_H = int(pygame.display.Info().current_h / 2) BARRIER_WHITE = 238 BARRIER_RED = 23 ROAD_BRIGHT_COLOR = 203 ROAD_DARK_COLOR = 194 GRASS = 174 clock = pygame.time.Clock() font = pygame.font.Font(None, 50) car = pygame_classes.Player(car_customization.change_color(), CENTER_W, CENTER_H) cam = pygame_classes.Camera() car.dir = 180 car.steer_left() car.x -= 200 target = pygame_classes.Finish(8, 9) bound_alert = pygame_classes.BoundsAlert() time_alert = pygame_classes.TimeAlert() win_alert = pygame_classes.WinAlert() map_s = pygame.sprite.Group() player_s = pygame.sprite.Group() tracks_s = pygame.sprite.Group() target_s = pygame.sprite.Group() timer_alert_s = pygame.sprite.Group() bound_alert_s = pygame.sprite.Group() win_alert_s = pygame.sprite.Group() hole_s = pygame.sprite.Group() diamond_s = pygame.sprite.Group() map_tile = [ 'asphalt0.png', 'asphalt1.png', 'asphalt2.png', 'asphalt3.png', 'asphalt4.png', 'asphalt5.png', 'asphalt6.png', 'grass.png', 'tribune.png' ] map = [[5, 3, 3, 3, 3, 3, 3, 3, 3, 6], [1, 7, 8, 7, 8, 7, 1, 8, 8, 1], [1, 5, 6, 1, 7, 7, 5, 3, 3, 4], [1, 1, 1, 1, 7, 7, 1, 7, 8, 8], [1, 1, 1, 1, 7, 7, 1, 8, 8, 8], [1, 1, 1, 1, 7, 8, 2, 3, 3, 6], [1, 1, 1, 1, 7, 7, 7, 8, 8, 1], [1, 1, 1, 1, 7, 8, 5, 3, 3, 4], [2, 4, 1, 1, 7, 8, 1, 7, 7, 7], [7, 7, 2, 4, 7, 7, 2, 3, 3, 7]] pygame_classes.map_files.clear() for tile_num in range(0, len(map_tile)): pygame_classes.map_files.append( pygame_classes.load_image(map_tile[tile_num], False)) for x in range(0, 10): for y in range(0, 10): map_s.add(pygame_classes.Map(map[x][y], x * 500, y * 500)) for i in range(12): x = randint(0, 9) y = randint(0, 9) while map[x][y] == 7 or map[x][y] == 8: x = randint(0, 9) y = randint(0, 9) if i < 6: hole = pygame_classes.Hole(y, x) hole_s.add(hole) else: diamond = pygame_classes.Diamond(y, x) diamond_s.add(diamond) pygame_classes.initialize_tracks() target_s.add(target) timer_alert_s.add(time_alert) bound_alert_s.add(bound_alert) win_alert_s.add(win_alert) player_s.add(car) cam.set_position(car.x, car.y) win = None collided = False running = True BONUS_POINTS = 10 PENALTY_POINTS = 0 while running: for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: car.reset() car.dir = 180 car.steer_left() car.x -= 200 target.reset() win = None collided = False BONUS_POINTS = 10 PENALTY_POINTS = 0 elif event.key == pygame.K_ESCAPE: running = False break keys = pygame.key.get_pressed() if target.time_left > 0 and win is None: if keys[K_LEFT]: car.steer_left() if keys[K_RIGHT]: car.steer_right() if keys[K_UP]: car.accelerate() else: car.soften() if keys[K_DOWN]: car.deaccelerate() cam.set_position(car.x, car.y) text_timer = font.render( 'Timer: ' + str(int((target.time_left / 60) / 60)) + ":" + str(int((target.time_left / 60) % 60)), 1, (255, 255, 255)) text_score = font.render( "Score: " + str(BONUS_POINTS + PENALTY_POINTS), 1, (255, 255, 255)) screen.blit(background, (0, 0)) map_s.update(cam.x, cam.y) map_s.draw(screen) car.slow_down_on_grass( screen.get_at((int(CENTER_W - 5), int(CENTER_H - 5))).g, GRASS) if car.tracks: tracks_s.add( pygame_classes.Track(cam.x + CENTER_W, cam.y + CENTER_H, car.dir)) tracks_s.update(cam.x, cam.y) tracks_s.draw(screen) target_s.update(cam.x, cam.y) target_s.draw(screen) hole_s.update(cam.x, cam.y) hole_s.draw(screen) diamond_s.update(cam.x, cam.y) diamond_s.draw(screen) player_s.update(cam.x, cam.y) player_s.draw(screen) if car.is_collision(screen, BARRIER_RED, BARRIER_WHITE): car.speed = 0 if car.is_out_of_road( screen.get_at((car.rect.left + car.rect.width, car.rect.top - car.rect.height)).b, ROAD_BRIGHT_COLOR, ROAD_DARK_COLOR): car.x = car.x + car.rect.width car.y = car.y - 0.5 * car.rect.height elif car.is_out_of_road( screen.get_at((car.rect.right - car.rect.width, car.rect.top - car.rect.height)).b, ROAD_BRIGHT_COLOR, ROAD_DARK_COLOR): car.x = car.x - car.rect.width car.y = car.y - 0.5 * car.rect.height elif car.is_out_of_road( screen.get_at((car.rect.left + car.rect.width, car.rect.bottom + car.rect.height)).b, ROAD_BRIGHT_COLOR, ROAD_DARK_COLOR): car.x = car.x + car.rect.width car.y = car.y + car.rect.height elif car.is_out_of_road( screen.get_at((car.rect.right - car.rect.width, car.rect.bottom + car.rect.height)).b, ROAD_BRIGHT_COLOR, ROAD_DARK_COLOR): car.x = car.x - car.rect.width car.y = car.y + car.rect.height if pygame.sprite.spritecollide(car, hole_s, True, pygame.sprite.collide_mask): PENALTY_POINTS = PENALTY_POINTS + hole.penalty if pygame.sprite.spritecollide(car, diamond_s, True, pygame.sprite.collide_mask): BONUS_POINTS = BONUS_POINTS + diamond.prize if target.time_left == 0: timer_alert_s.draw(screen) car.speed = 0 win = False if BONUS_POINTS + PENALTY_POINTS <= 0: car.speed = 0 win = False bound_alert_s.update() bound_alert_s.draw(screen) if pygame.sprite.spritecollide(car, target_s, True): car.speed = 0 win = True collided = True if collided: win_alert_s.draw(screen) running = False screen.blit(text_timer, (CENTER_W - 600, CENTER_H - 300)) screen.blit(text_score, (CENTER_W - 600, CENTER_H - 240)) pygame.display.flip() clock.tick(64) pygame.time.wait(1000) level5.main()
def main(): os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (0, 50) pygame.init() screen = pygame.display.set_mode( (int(pygame.display.Info().current_w / 3), int(pygame.display.Info().current_h / 2))) background = pygame.Surface(screen.get_size()) background = background.convert_alpha() background.fill((39, 174, 96)) CENTER_W = int(pygame.display.Info().current_w / 2) CENTER_H = int(pygame.display.Info().current_h / 2) GREEN = 174 clock = pygame.time.Clock() network = Network() players = network.get_players() car = pygame_classes.from_state(players[0]) car2 = pygame_classes.from_state(players[1]) cam = pygame_classes.Camera() cup = pygame_classes.Cup(3, 9) bound_alert = pygame_classes.BoundsAlert() win_alert = pygame_classes.WinAlert() map_s = pygame.sprite.Group() player_s = pygame.sprite.Group() cup_s = pygame.sprite.Group() bound_alert_s = pygame.sprite.Group() win_alert_s = pygame.sprite.Group() map_tile = ['sand8.png', 'sand1.png', 'sand2.png', 'sand3.png', 'sand4.png', 'sand5.png', 'sand6.png', 'grass.png', 'tribune.png', 'tree.png'] map = [ [7, 2, 3, 3, 3, 4, 7, 2, 3, 4], [7, 1, 7, 7, 7, 1, 7, 1, 7, 1], [8, 7, 8, 8, 7, 1, 7, 1, 7, 1], [2, 3, 3, 4, 9, 1, 7, 1, 7, 1], [1, 2, 3, 6, 9, 1, 8, 1, 7, 1], [1, 1, 8, 8, 7, 5, 3, 6, 9, 1], [1, 5, 3, 3, 4, 7, 7, 7, 9, 1], [1, 7, 7, 7, 1, 7, 9, 9, 7, 1], [1, 8, 8, 8, 5, 3, 3, 3, 3, 6], [5, 3, 3, 0, 7, 7, 7, 7, 7, 7] ] pygame_classes.map_files.clear() for tile_num in range(0, len(map_tile)): pygame_classes.map_files.append(pygame_classes.load_image(map_tile[tile_num], False)) for x in range(0, 10): for y in range(0, 10): map_s.add(pygame_classes.Map(map[x][y], x * 500, y * 500)) cup_s.add(cup) bound_alert_s.add(bound_alert) win_alert_s.add(win_alert) player_s.add(car) cam.set_position(car.x, car.y) win = None collided = False win2 = None running = True while running: for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: car.reset() car.x = 320 car.y = 270 win = None collided = False if event.key == pygame.K_TAB: car2.reset() car2.x = 420 car2.y = 270 win2 = None elif event.key == pygame.K_ESCAPE: running = False break keys = pygame.key.get_pressed() if win == None: if keys[K_LEFT]: car.steer_left() if keys[K_RIGHT]: car.steer_right() if keys[K_UP]: car.accelerate() else: car.soften() if keys[K_DOWN]: car.deaccelerate() if win2 == None: if keys[K_a]: car2.steer_left() if keys[K_d]: car2.steer_right() if keys[K_w]: car2.accelerate() else: car2.soften() if keys[K_s]: car2.deaccelerate() cam.set_position(car.x, car.y) screen.blit(background, (0, 0)) map_s.update(cam.x, cam.y) map_s.draw(screen) cup_s.update(cam.x, cam.y) cup_s.draw(screen) player_s.update(cam.x, cam.y) player_s.draw(screen) car2.update_additional_car(cam.x, cam.y) car2.draw_additional_car(screen) if pygame.sprite.collide_rect(car2, car): car.impact() car.x = car.x + int(car.rect.width / 4) car2.impact() car2.x = car2.x - int(car2.rect.width / 4) if pygame_classes.is_out_of_map(car.x + CENTER_W, car.y + CENTER_H) or car.is_out_of_road( screen.get_at((int(CENTER_W), int(CENTER_H))).g, GREEN, GREEN): car.speed = 0 win = False bound_alert_s.draw(screen) flags = network.receive_flags() # Car2 is out of road if flags[0]: car2.speed = 0 win2 = False elif win == False and flags[0] == False : win2 = None if pygame.sprite.spritecollide(car, cup_s, True): car.speed = 0 win = True win2 = False collided = True if flags[1]: car2.speed = 0 car.speed = 0 win2 = True win = False if collided: win2 = False car2.speed = 0 win_alert_s.draw(screen) players[0] = car.get_state() players[1] = car2.get_state() network.send(players) pygame.display.flip() clock.tick(64)