def __init__(self): self.clock = pygame.time.Clock() self.running = True self.font = pygame.font.Font(None, 32) self.car = player.Player() self.cam = camera.Camera() self.target = gamemode.Finish() self.bound_alert = bounds.Alert() self.time_alert = timeout.Alert() self.celular_alert = leis_transito.CelularAlert() self.stop_alert = leis_transito.StopAlert() self.info = menu.Alert() self.pointer = direction.Tracker(int(CENTER_W * 2), int(CENTER_H * 2)) self.hardmodeOn = False self.extraTimeValue = AI_CONFIG._L3_tempo_extra_facil self.quantidadeVeiculos = AI_CONFIG._L3_quantidade_veiculos_facil # create sprite groups. self.map_s = pygame.sprite.Group() self.player_s = pygame.sprite.Group() self.celular_s = pygame.sprite.Group() self.stop_s = pygame.sprite.Group() self.traffic_s = pygame.sprite.Group() self.tracks_s = pygame.sprite.Group() self.target_s = pygame.sprite.Group() self.pointer_s = pygame.sprite.Group() self.timer_alert_s = pygame.sprite.Group() self.bound_alert_s = pygame.sprite.Group() self.menu_alert_s = pygame.sprite.Group() self.CENTER_W = int(1024 / 2) self.CENTER_H = int(768 / 2)
def main(): # initialize objects. clock = pygame.time.Clock() running = True font = pygame.font.Font(None, 24) car = player.Player() cam = camera.Camera() target = gamemode.Finish() bound_alert = boundary.Alert() time_alert = timeout.Alert() info = menu.Alert() pointer = direction.Tracker(int(CENTER_W * 2), int(CENTER_H * 2)) # create sprite groups. map_s = pygame.sprite.Group() player_s = pygame.sprite.Group() traffic_s = pygame.sprite.Group() tracks_s = pygame.sprite.Group() target_s = pygame.sprite.Group() pointer_s = pygame.sprite.Group() timer_alert_s = pygame.sprite.Group() bound_alert_s = pygame.sprite.Group() menu_alert_s = pygame.sprite.Group() # generate tiles for tile_num in range(0, len(maps.map_tile)): maps.map_files.append(load_image(maps.map_tile[tile_num], False)) for x in range(0, 10): for y in range(0, 10): map_s.add(maps.Map(maps.map_1[x][y], x * 1000, y * 1000, maps.map_1_rot[x][y])) # load tracks tracks.initialize() # load finish target_s.add(target) # load direction pointer_s.add(pointer) # load alerts timer_alert_s.add(time_alert) bound_alert_s.add(bound_alert) menu_alert_s.add(info) # load traffic traffic.initialize(CENTER_W, CENTER_H) for count in range(0, TRAFFIC_COUNT): traffic_s.add(traffic.Traffic()) player_s.add(car) cam.set_pos(car.x, car.y) while running: # Check for key input. (KEYDOWN, trigger often) keys = pygame.key.get_pressed() # Render loop. # Check for menu/reset, (keyup event - trigger ONCE) for event in pygame.event.get(): if event.type == pygame.KEYUP: if keys[K_m]: if (info.visibility == True): info.visibility = False if (keys[K_p]): car.reset() target.reset() if (keys[K_q]): pygame.quit() sys.exit(0) if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: running = False break if (target.timeleft > 0): if keys[K_c]: car.steerleft() if keys[K_b]: car.steerright() if keys[K_UP]: car.accelerate() else: car.soften() if keys[K_DOWN]: car.deaccelerate() if keys[K_RIGHT]: car.breakk() cam.set_pos(car.x, car.y) # Show text data. text_fps = font.render('FPS: ' + str(int(clock.get_fps())), 1, (224, 16, 16)) textpos_fps = text_fps.get_rect(centery=25, centerx=60) text_score = font.render('Score: ' + str(target.score), 1, (224, 16, 16)) textpos_score = text_fps.get_rect(centery=45, centerx=60) text_timer = font.render( 'Timer: ' + str(int((target.timeleft /1000) / 1000)) + ":" + str(int((target.timeleft / 1500) % 300)), 6, (224, 16, 16)) textpos_timer = text_fps.get_rect(centery=65, centerx=60) # Render Scene. screen.blit(background, (0, 0)) # cam.set_pos(car.x, car.y) map_s.update(cam.x, cam.y) map_s.draw(screen) # Conditional renders/effects car.grass(screen.get_at(((int(CENTER_W - 5), int(CENTER_H - 5)))).g) if (car.tracks): tracks_s.add(tracks.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) traffic_s.update(cam.x, cam.y) traffic_s.draw(screen) target_s.update(cam.x, cam.y) target_s.draw(screen) pointer_s.update(car.x + CENTER_W, car.y + CENTER_H, target.x, target.y) pointer_s.draw(screen) # Conditional renders. if (boundary.breaking(car.x + CENTER_W, car.y + CENTER_H) == True): bound_alert_s.update() bound_alert_s.draw(screen) if (target.timeleft == 0): timer_alert_s.draw(screen) car.speed = 0 text_score = font.render('Final Score: ' + str(target.score), 1, (224, 16, 16)) textpos_score = text_fps.get_rect(centery=CENTER_H + 56, centerx=CENTER_W - 20) if (info.visibility == True): menu_alert_s.draw(screen) # Blit Blit.. screen.blit(text_fps, textpos_fps) screen.blit(text_score, textpos_score) screen.blit(text_timer, textpos_timer) pygame.display.flip() # Check collision!!! if pygame.sprite.spritecollide(car, traffic_s, False): car.impact() target.car_crash() if pygame.sprite.spritecollide(car, target_s, True): target.claim_flag() target.generate_finish() target_s.add(target) clock.tick(64)
background = background.convert_alpha() background.fill((26, 26, 26)) clock = pygame.time.Clock() running = True font = pygame.font.Font(None, 32) car = player.Player() cam = camera.Camera() target = gamemode.Finish() bound_alert = bounds.Alert() time_alert = timeout.Alert() celular_alert = leis_transito.CelularAlert() stop_alert = leis_transito.StopAlert() info = menu.Alert() pointer = direction.Tracker(int(CENTER_W * 2), int(CENTER_H * 2)) # create sprite groups. map_s = pygame.sprite.Group() player_s = pygame.sprite.Group() celular_s = pygame.sprite.Group() stop_s = pygame.sprite.Group() traffic_s = pygame.sprite.Group() tracks_s = pygame.sprite.Group() target_s = pygame.sprite.Group() pointer_s = pygame.sprite.Group() timer_alert_s = pygame.sprite.Group() bound_alert_s = pygame.sprite.Group() menu_alert_s = pygame.sprite.Group()
def main(): #initialize objects. clock = pygame.time.Clock() running = True font = pygame.font.Font(None, 32) car = player.Player() cam = camera.Camera() target = gamemode.Finish() bound_alert = bounds.Alert() time_alert = timeout.Alert() celular_alert = leis_transito.CelularAlert() stop_alert = leis_transito.StopAlert() info = menu.Alert() pointer = direction.Tracker(int(CENTER_W * 2), int(CENTER_H * 2)) #create sprite groups. map_s = pygame.sprite.Group() player_s = pygame.sprite.Group() celular_s = pygame.sprite.Group() stop_s = pygame.sprite.Group() traffic_s = pygame.sprite.Group() tracks_s = pygame.sprite.Group() target_s = pygame.sprite.Group() pointer_s = pygame.sprite.Group() timer_alert_s = pygame.sprite.Group() bound_alert_s = pygame.sprite.Group() menu_alert_s = pygame.sprite.Group() #generate tiles for tile_num in range(0, len(maps.map_tile)): maps.map_files.append(load_image(maps.map_tile[tile_num], False)) for x in range(0, 10): for y in range(0, 10): map_s.add( maps.Map(maps.map_1[x][y], x * 1000, y * 1000, maps.map_1_rot[x][y])) #load tracks tracks.initialize() #load finish target_s.add(target) #load direction pointer_s.add(pointer) #load alerts timer_alert_s.add(time_alert) bound_alert_s.add(bound_alert) celular_s.add(celular_alert) stop_s.add(stop_alert) menu_alert_s.add(info) #load traffic traffic.initialize(CENTER_W, CENTER_H) for count in range(0, TRAFFIC_COUNT): traffic_s.add(traffic.Traffic()) player_s.add(car) cam.set_pos(car.x, car.y) while running: #Render loop. #Check for menu/reset, (keyup event - trigger ONCE) for event in pygame.event.get(): if event.type == pygame.KEYUP: if keys[K_m]: if (info.visibility == True): info.visibility = False else: info.visibility = True if (keys[K_p]): car.reset() target.reset() if (keys[K_q]): pygame.quit() sys.exit(0) if (keys[K_z]): # atende ligacao celular_alert.atender = True if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: from source.menu import Menu as menu2 aux = menu2() aux.main_menu() running = False break #Check for key input. (KEYDOWN, trigger often) keys = pygame.key.get_pressed() if (target.timeleft > 0): if keys[K_LEFT]: car.steerleft() if keys[K_RIGHT]: car.steerright() if keys[K_UP]: car.accelerate() else: car.soften() if keys[K_DOWN]: car.deaccelerate() cam.set_pos(car.x, car.y) #Show text data. text_fps = font.render('', 1, (224, 16, 16)) textpos_fps = text_fps.get_rect(centery=25, centerx=60) text_score = font.render('Pontuacao: ' + str(target.score), 1, (224, 16, 16)) textpos_score = text_fps.get_rect(centery=45, centerx=60) text_timer = font.render( 'Tempo: ' + str(int((target.timeleft / 60) / 60)) + ":" + str(int((target.timeleft / 60) % 60)), 1, (224, 16, 16)) textpos_timer = text_fps.get_rect(centery=65, centerx=60) #Render Scene. screen.blit(background, (0, 0)) #cam.set_pos(car.x, car.y) map_s.update(cam.x, cam.y) map_s.draw(screen) #Conditional renders/effects car.grass(screen.get_at(((int(CENTER_W - 5), int(CENTER_H - 5)))).g) if car.tracks: tracks_s.add( tracks.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) traffic_s.update(cam.x, cam.y) traffic_s.draw(screen) target_s.update(cam.x, cam.y) target_s.draw(screen) pointer_s.update(car.x + CENTER_W, car.y + CENTER_H, target.x, target.y) pointer_s.draw(screen) # stop_alert.updateTimer(time.time()) #Conditional renders. if (bounds.breaking(car.x + CENTER_W, car.y + CENTER_H) == True): bound_alert_s.update() bound_alert_s.draw(screen) if (target.timeleft == 0): timer_alert_s.draw(screen) car.speed = 0 text_score = font.render('Pontuacao Final: ' + str(target.score), 1, (224, 16, 16)) textpos_score = text_fps.get_rect(centery=CENTER_H + 56, centerx=CENTER_W - 20) celular_alert.grass( screen.get_at(((int(CENTER_W - 5), int(CENTER_H - 5)))).g, car.speed) if (int((target.timeleft / 60) % 60) % 20 == 0): celular_alert.visibility = True if (target.timeleft > 0 and celular_alert.visibility is True): celular_s.draw(screen) keys = pygame.key.get_pressed() celular_alert.startTimer() if (keys[K_c] and car.speed > 0.4): target.score -= 15 celular_alert.visibility = False if (keys[K_c] and car.speed <= 0.4): target.score += 200 celular_alert.visibility = False if (not celular_alert.cel_time()): target.score -= 100 celular_alert.visibility = False stop_alert.stop_car(car.speed) if (int((target.timeleft / 60) % 60) % 15 == 0 and car.speed >= 0.1): stop_alert.visibility = True if (target.timeleft > 0 and stop_alert.visibility is True): stop_s.draw(screen) if (car.speed < 0.1): target.score += 200 stop_alert.visibility = False else: target.score -= 1 if (info.visibility == True): menu_alert_s.draw(screen) #Blit Blit.. pygame.draw.rect(screen, (216, 216, 216), [40, 30, 200, 55]) screen.blit(text_fps, textpos_fps) screen.blit(text_score, textpos_score) screen.blit(text_timer, textpos_timer) pygame.display.flip() #Check collision!!! if pygame.sprite.spritecollide(car, traffic_s, False): car.impact() target.car_crash() target.score -= 5 if pygame.sprite.spritecollide(car, target_s, True): target.claim_flag() target.score += 150 target.generate_finish() target_s.add(target) clock.tick(64)
def main(): #database record_switch = False text_rank = [] textpos_rank = [] #start music pygame.mixer.music.load('./media/background.wav') pygame.mixer.music.play(-1) #initialize objects. clock = pygame.time.Clock() running = True font = pygame.font.Font(None, 32) car = player.Player() cam = camera.Camera() target = gamemode.Finish() item = [] traffics = [] bound_alert = bounds.Alert() time_alert = timeout.Alert() info = menu.Alert() pointer = direction.Tracker(int(CENTER_W * 2), int(CENTER_H * 2)) #create sprite groups. map_s = pygame.sprite.Group() player_s = pygame.sprite.Group() traffic_s = pygame.sprite.Group() tracks_s = pygame.sprite.Group() target_s = pygame.sprite.Group() item_s = pygame.sprite.Group() pointer_s = pygame.sprite.Group() timer_alert_s = pygame.sprite.Group() bound_alert_s = pygame.sprite.Group() menu_alert_s = pygame.sprite.Group() #generate tiles 기본 맵 초원 for tile_num in range(0, len(maps.map_tile[0])): maps.map_files.append(load_image(maps.map_tile[0][tile_num], False)) for x in range(0, 10): for y in range(0, 10): map_s.add( maps.Map(maps.map_1[x][y], x * 1000, y * 1000, maps.map_1_rot[x][y])) #load tracks tracks.initialize() #load finish target_s.add(target) #load items items.initialize() for count in range(0, ITEM_COUNT): item.append(items.Items()) item_s.add(item[count]) #load direction pointer_s.add(pointer) #load alerts timer_alert_s.add(time_alert) bound_alert_s.add(bound_alert) menu_alert_s.add(info) #load traffic traffic.initialize(CENTER_W, CENTER_H) for count in range(0, TRAFFIC_COUNT): traffics.append(traffic.Traffic()) traffic_s.add(traffics[count]) player_s.add(car) cam.set_pos(car.x, car.y) while running: #Render loop. #Check for menu/reset, (keyup event - trigger ONCE) for event in pygame.event.get(): if event.type == pygame.KEYUP: if keys[K_m]: if (info.visibility == True): info.visibility = False else: info.visibility = True if info.visibility == True: if (keys[K_p]): car.reset() target.reset() for i in range(0, ITEM_COUNT): item[i].reset record_switch = False if (keys[K_q]): pygame.quit() sys.exit(0) if (keys[K_1]): change_map(map_s, maps, 0) car.reset() target.reset() for i in range(0, ITEM_COUNT): item[i].reset if (keys[K_2]): change_map(map_s, maps, 1) car.reset() target.reset() for i in range(0, ITEM_COUNT): item[i].reset if (keys[K_3]): change_map(map_s, maps, 2) car.reset() target.reset() for i in range(0, ITEM_COUNT): item[i].reset if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: running = False break #Check for key input. (KEYDOWN, trigger often) keys = pygame.key.get_pressed() if (target.timeleft > 0): if keys[K_LEFT]: car.steerleft() if keys[K_RIGHT]: car.steerright() if keys[K_UP]: car.accelerate() else: car.soften() if keys[K_DOWN]: car.deaccelerate() cam.set_pos(car.x, car.y) #Show text data. text_fps = font.render('FPS: ' + str(int(clock.get_fps())), 1, (224, 16, 16)) textpos_fps = text_fps.get_rect(centery=175, centerx=260) text_score = font.render('Score: ' + str(target.score), 1, (224, 16, 16)) textpos_score = text_fps.get_rect(centery=205, centerx=260) text_timer = font.render( 'Timer: ' + str(int((target.timeleft / 60) / 60)) + ":" + str(int((target.timeleft / 60) % 60)), 1, (224, 16, 16)) textpos_timer = text_fps.get_rect(centery=235, centerx=260) text_red_ball = font.render( 'Red Ball: ' + str(int(car.red_timer / 64)), 1, (224, 16, 16)) textpos_red_ball = text_fps.get_rect(centery=265, centerx=260) text_blue_ball = font.render( 'Blue Ball: ' + str(int(car.blue_timer / 64)), 1, (224, 16, 16)) textpos_blue_ball = text_fps.get_rect(centery=295, centerx=260) text_green_ball = font.render( 'Green Ball: ' + str(int(car.green_timer / 64)), 1, (224, 16, 16)) textpos_green_ball = text_fps.get_rect(centery=325, centerx=260) text_ranking = font.render('TOP 5 Ranking', 1, (16, 224, 16)) textpos_ranking = text_fps.get_rect(centery=CENTER_H, centerx=CENTER_W - 20) text_boundtimer = font.render( 'Game over After ' + str(int(bound_alert.timeleft / 64)) + ' Second.', 1, (224, 16, 16)) textpos_boundtimer = text_fps.get_rect(centery=CENTER_H + 30, centerx=CENTER_W - 100) #Render Scene. screen.blit(background, (0, 0)) #cam.set_pos(car.x, car.y) map_s.update(cam.x, cam.y) map_s.draw(screen) #Conditional renders/effects car.grass(screen.get_at(((int(CENTER_W - 5), int(CENTER_H - 5)))).g) if (car.tracks): tracks_s.add( tracks.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) traffic_s.update(cam.x, cam.y) traffic_s.draw(screen) target_s.update(cam.x, cam.y) target_s.draw(screen) item_s.update(cam.x, cam.y) item_s.draw(screen) pointer_s.update(car.x + CENTER_W, car.y + CENTER_H, target.x, target.y) pointer_s.draw(screen) #Conditional renders. if (bounds.breaking(car.x + CENTER_W, car.y + CENTER_H) == True): bound_alert_s.update() bound_alert_s.draw(screen) bound_alert.update() screen.blit(text_boundtimer, textpos_boundtimer) if (bound_alert.timeleft == 0): target.timeleft = 0 else: bound_alert.timeleft = 640 if (target.timeleft == 0): timer_alert_s.draw(screen) car.speed = 0 text_score = font.render('Your Score: ' + str(target.score), 1, (224, 16, 16)) textpos_score = text_fps.get_rect(centery=CENTER_H - 56, centerx=CENTER_W - 20) if record_switch == False: cur.execute("SELECT * From rank ORDER BY Point DESC") dataList = [] dataList = cur.fetchall() rank = 0 if len(dataList) == 0: cur.execute("INSERT INTO rank VALUES(?,?)", (1, target.score)) elif len(dataList) < 5: for i in dataList: if i[1] < target.score: rank = i[0] break if rank != 0: cur.execute( "UPDATE rank SET Rank=Rank+1 WHERE Point < %d" % (target.score)) cur.execute("INSERT INTO rank VALUES(?,?)", (rank, target.score)) else: cur.execute("INSERT INTO rank VALUES(?,?)", (len(dataList) + 1, target.score)) elif len(dataList) >= 5: for i in dataList: if i[1] < target.score: rank = i[0] break if rank != 0: cur.execute( "UPDATE rank SET Rank=Rank+1 WHERE Point<%d" % (target.score)) cur.execute("INSERT INTO rank VALUES(?,?)", (rank, target.score)) cur.execute("DELETE FROM rank WHERE Rank > 5") con.commit() cur.execute("SELECT * From rank ORDER BY Rank ASC") dataList = cur.fetchall() cnt = 0 text_rank.clear() textpos_rank.clear() for i in dataList: cnt = cnt + 1 text_rank.append( font.render( str(i[0]) + '. ' + str(i[1]), 1, (16, 224, 16))) textpos_rank.append( text_fps.get_rect(centery=CENTER_H + 25 + cnt * 25, centerx=CENTER_W - 20)) record_switch = True if (info.visibility == True): menu_alert_s.draw(screen) #Blit Blit.. screen.blit(text_fps, textpos_fps) screen.blit(text_score, textpos_score) screen.blit(text_timer, textpos_timer) screen.blit(text_red_ball, textpos_red_ball) screen.blit(text_blue_ball, textpos_blue_ball) screen.blit(text_green_ball, textpos_green_ball) if record_switch == True: screen.blit(text_ranking, textpos_ranking) for i in range(0, cnt): screen.blit(text_rank[i], textpos_rank[i]) pygame.display.flip() #Check collision!!! #traffic 과의 충돌 확인. 블루볼 먹었을 시에는 충돌 없음 for i in range(0, TRAFFIC_COUNT): if traffics[i].collision_check(car) == True: if car.blue_ball == True: pygame.mixer.Sound.play(point_sound) traffics[i].respawn() else: car.impact() target.car_crash() if pygame.sprite.spritecollide(car, target_s, True): pygame.mixer.Sound.play(point_sound) target.claim_flag() target.generate_finish() target_s.add(target) for i in range(0, ITEM_COUNT): if item[i].collision_check(car) == True: pygame.mixer.Sound.play(point_sound) car.get_item(item[i].color) item[i].generate_items() item_s.add(item[i]) clock.tick(64)