def __init__(self, population_size, vacc_percentage, virus_name, mortality_rate, basic_repro_num, initial_infected=1): self.population_size = population_size self.population = [] self.total_infected = 0 self.current_infected = 0 self.next_person_id = 0 self.virus_name = virus_name self.mortality_rate = mortality_rate self.basic_repro_num = basic_repro_num self.initial_infected = initial_infected self.file_name = "{}_simulation_pop_{}_vp_{}_infected_{}.txt".format( virus_name, population_size, vacc_percentage, initial_infected) # TODO: Create a Logger object and bind it to self.logger. You should use this # logger object to log all events of any importance during the simulation. Don't forget # to call these logger methods in the corresponding parts of the simulation! self.logger = logger.Logger(self.file_name) self.logger.write_metadata(population_size, vacc_percentage, virus_name, mortality_rate, basic_repro_num) self.virus = virus.Virus(virus_name, mortality_rate, basic_repro_num) # This attribute will be used to keep track of all the people that catch # the infection during a given time step. We'll store each newly infected # person's .ID attribute in here. At the end of each time step, we'll call # self._infect_newly_infected() and then reset .newly_infected back to an empty # list. self.newly_infected = [] # TODO: Call self._create_population() and pass in the correct parameters. # Store the array that this method will return in the self.population attribute. self._create_population(initial_infected)
def test_infected_did_survive_infection(): weak_virus = virus.Virus("weak virus", -1.0, -1.0) print(weak_virus.name) print(weak_virus) print(weak_virus.mortality_rate) survivor = person.Person(0, is_vaccinated=False, infection=weak_virus) print() assert survivor.did_survive_infection() == True
def test_log_infection_survival_false(self): _logger = logger.Logger('what.txt') _virus = virus.Virus("Tuberculosis", 0.67, 0.55) _person = person.Person(1, False, _virus) _logger.write_metadata(100, 0.5, 'Virus', 0.5, 0.5) _logger.log_infection_survival(_person, False) file = open(_logger.file_name, 'r') assert file.read( ) == f'''{str(100)}\t{str(0.5)}\t{'Virus'}\t{str(0.5)}\t{str(0.5)}\n{1} became immune to the infection \n''' file.close() os.remove("what.txt")
def test_log_interaction_fail_to_infect_vacc(self): _logger = logger.Logger('what.txt') _virus = virus.Virus("Tuberculosis", 0.67, 0.55) _person = person.Person(1, False, _virus) _person2 = person.Person(2, False) _logger.write_metadata(100, 0.5, 'Virus', 0.5, 0.5) _logger.log_interaction(_person, _person2, None, True, None) file = open(_logger.file_name, 'r') assert file.read( ) == f'''{str(100)}\t{str(0.5)}\t{'Virus'}\t{str(0.5)}\t{str(0.5)}\n{1} did not infect vaccinated person {2} \n''' file.close() os.remove("what.txt")
def part_two(args, input_lines): "Process part two of the puzzle" # 1. Create the puzzle solver solver = virus.Virus(part2=True, text=input_lines) # 2. Determine the solution for part two solution = solver.part_two(verbose=args.verbose) if solution is None: print("There is no solution") else: print("The solution for part two is %s" % (solution)) # 3. Return result return solution is not None
def test_did_survive_infection_certain_death(): # mortality rate is impossibly high, so exposed_person2 should always die new_virus = virus.Virus("new_virus", 1.01, 1.01) exposed_person2 = person.Person(34, is_vaccinated=False, infection=new_virus) assert new_virus.name == "new_virus" assert new_virus.mortality_rate == 1.01 assert new_virus.repro_rate == 1.01 assert exposed_person2._id == 34 assert exposed_person2.is_vaccinated == False assert exposed_person2.infection == new_virus # unsure how to implement this test; trying to make sure that did_survive_infection() returns a value assert exposed_person2.did_survive_infection() == False assert exposed_person2.is_alive == False
def test_create_population(self): _virus = virus.Virus('Virus', 0.5, 0.5) _sim = simulation.Simulation(100, 0.5, _virus, 10) _sim.population = _sim._create_population(_sim.initial_infected) infected = [] vacc = [] normies = [] total = [] for _person in _sim.population: total.append(_person) if _person.infection != None: infected.append(_person) elif _person.is_vaccinated == True: vacc.append(_person) else: normies.append(person) assert len(_sim.population) == 100 assert len(infected) == 10 assert len(vacc) == 50 assert len(normies) == 40 assert len(total) == 100 assert len(total) == len(infected) + len(vacc) + len(normies)
def test_virus(): # Athena = superheroes.Hero("Athena") hiv = virus.Virus("HIV", .8, .3) assert hiv.name == 'HIV' assert hiv.mortality_rate == .8 assert hiv.reproduction_rate == .3
def test_person_survive(): my_virus = virus.Virus("cold", infection_rate=.5, mortality_rate=0) my_person = person.Person(0, False, my_virus) my_person.did_survive_infection() #Cant kill you assert my_person.is_alive is True
def test_person_death(): # tests the did survive infection function my_virus = virus.Virus("death", infection_rate=.5, mortality_rate=1) my_person = person.Person(0, False, my_virus) my_person.did_survive_infection() #Has to kill you assert my_person.is_alive is False
def test_infection(): my_virus = virus.Virus("cold", infection_rate=.5, mortality_rate=0) my_person = person.Person(3, False, my_virus) assert my_person._id is 3 assert my_person.infection is my_virus
# created. Iterate though this list. # For every person id in self.newly_infected: # - Find the Person object in self.population that has this corresponding ID. # - Set this Person's .infected attribute to True. # Once you have iterated through the entire list of self.newly_infected, remember # to reset self.newly_infected back to an empty list! for infected_person_id in self.newly_infected: for person in self.population: if person._id == infected_person_id: person.infection = self.virus self.total_infected += len(self.newly_infected) print('newly infected: {}'.format(self.newly_infected)) self.newly_infected = [] if __name__ == "__main__": params = sys.argv[1:] pop_size = int(params[0]) vacc_percentage = float(params[1]) virus_name = str(params[2]) mortality_rate = float(params[3]) basic_repro_num = float(params[4]) if len(params) == 6: initial_infected = int(params[5]) else: initial_infected = 1 virus = virus.Virus(virus_name, mortality_rate, basic_repro_num) simulation = Simulation(pop_size, vacc_percentage, virus, initial_infected) simulation.run()
def add_virus_at_location(virus_count, loc): for i in range(virus_count): World.cell_list.append( virus.Virus(loc.x + .01 * random.uniform(0, 1), loc.y + .01 * random.uniform(0, 1)))
def add_viruses(virus_count): ''' Add virus_count number of cells of random colors at random locations to the world''' for i in range(virus_count): World.cell_list.append( virus.Virus(random.uniform(0, World.width), random.uniform(0, World.height)))
def add_virus(pos): World.cell_list.append( virus.Virus(pos.x, pos.y, 1, 1, 4, 4, "on_death_disperse", 50, 2))
def test_infection(): # written while pair coding with Nolan test_virus = virus.Virus("supervirus", 2.00, 2.00) infected_person = person.Person(49, is_vaccinated=False, infection=test_virus) # testing to make sure that the virus object that is on the parameter infection is indeed an object assert isinstance(infected_person.infection, virus.Virus)
class Testing(unittest.TestCase): virus = virus.Virus("Ebola", 0.70, 0.25) person1 = person.Person(1, False, virus) person2 = person.Person(2, True, False) person3 = person.Person(3, False, False) person4 = person.Person(4, False, None) person5 = person.Person(5, True, False) simulation = simulation.Simulation(100, 0.90, "Ebola", 0.70, 0.25, 10) def test_virus(self): self.assertEqual(self.virus.name, "Ebola") self.assertEqual(self.virus.mortality_rate, 0.70) self.assertEqual(self.virus.basic_repro_num, 0.25) def test_person(self): self.person1.infected = self.virus self.person1.is_vaccinated = False self.assertEqual(self.person1.infected, self.virus) self.assertEqual(self.person1.is_vaccinated, False) self.assertEqual(self.person1._id, 1) self.assertEqual(self.person2.infected, False) self.assertEqual(self.person2.is_vaccinated, True) self.assertEqual(self.person2._id, 2) self.assertEqual(self.person3.infected, False) self.assertEqual(self.person3.is_vaccinated, False) self.assertEqual(self.person3._id, 3) def test_did_survive_infection(self): self.assertEqual(self.person1.did_survive_infection(), False) self.assertEqual(self.person2.did_survive_infection(), 0) self.assertEqual(self.person3.did_survive_infection(), 0) def test_create_population(self): self.assertEqual( self.simulation._create_population( self.simulation.initial_infected), self.simulation.population) # def test_simulation_should_continue(self): # self.assertEqual(self.simulation._simulation_should_continue(), True) # self.person1.is_alive = False # self.person2.is_alive = False # self.person3.is_alive = False # population = [self.person1, self.person2, self.person3] # self.simulation.population = population # self.assertEqual(self.simulation._simulation_should_continue(), False) # def test_time_step(self): # self.assertEqual(self.simulation.time_step(), 100) # def test_run(self): # self.assertEqual(self.simulation.run(), 10) def test_interaction(self): self.person1 = person.Person(1, False, self.virus) self.person1.is_alive = True self.person5.is_alive = True self.assertEqual( self.simulation.interaction(self.person1, self.person4), 0) self.assertEqual( self.simulation.interaction(self.person1, self.person1), 0) self.person1.is_alive = True self.assertEqual( self.simulation.interaction(self.person1, self.person5), 1) def test_infect_newly_infected(self): self.person6 = person.Person(6, False, self.virus) self.person7 = person.Person(7, False, False) self.simulation.population = [self.person6, self.person7] self.simulation.newly_infected = [self.person6, self.person7] self.assertEqual(self.simulation._infect_newly_infected(), True)
def test_instantiation(self): _virus = virus.Virus('Virus', 0.5, 0.5) _sim = simulation.Simulation(100, 0.5, _virus, 10) assert _sim.virus == _virus assert _sim.pop_size == 100 assert _sim.total_dead == 0
def setUp(self): self.person1 = person.Person(1, False, infected=virus.Virus('Ebola', .3, .6)) self.person2 = person.Person(2, True)
def tutorial_starting(screen) : chal = [0,0,0,0,0,0,0,0,0] chal_sv = [0,0,0,0,0,0,0,0,0] touer = [False, False, False, False] chal_scr = [500, 1000, 2000, 4000,6000,2000, 500, 3000, 5000] #도전과제 달성시 보상 chal_money = [100,200,350,550,700,200,100,500,0] score = 0 virus.Virus.Allnum = 0 virus.game_reset() WHITE = (255, 255, 255) BLACK = (0, 0, 0) back_ground = pygame.image.load("image/virus/map2.png") interface = pygame.image.load("image/virus/interface.png") map1 = map.map(screen) map1.ch = [[120,60],[120,300],[900,300],[900,720]] map1.set([0, 60], [900, 720]) chal_img = [] chal_img.append(pygame.image.load("image/challenge/chal_1.png")) chal_img.append(pygame.image.load("image/challenge/chal_2.png")) chal_img.append(pygame.image.load("image/challenge/chal_3.png")) chal_img.append(pygame.image.load("image/challenge/chal_4.png")) chal_img.append(pygame.image.load("image/challenge/chal_5.png")) chal_img.append(pygame.image.load("image/challenge/chal_6.png")) chal_img.append(pygame.image.load("image/challenge/chal_7.png")) chal_img.append(pygame.image.load("image/challenge/chal_8.png")) support_index = [] life = 20 tower1 = [] enemy1 = [] index = -1 vindex = -1 timg = [] timg.append(pygame.image.load("tower/normal_tower1.png")) timg.append(pygame.image.load("tower/short_tower1.png")) timg.append(pygame.image.load("tower/long_tower1.png")) timg.append(pygame.image.load("tower/support_tower1.png")) cancel_img = pygame.image.load("tower/cancel.png") build = 0 build_ok = True game_timer = time.time() old_time = game_timer gold = 1000 round123 = 0 count = 0 Font = pygame.font.Font(None, 52) font = pygame.font.Font(None, 26) vtimer = time.time() screen.blit(back_ground, (0, 0)) map1.draw() Gameoverbool = False ## 바이러스 badguy = [] selectNum = -1 count = 0 type_virus = 0 # copyright -shin hyuk jin pygame.mixer.init() pygame.mixer.music.load("sound/tutorbgm.wav") pygame.mixer.music.set_volume(0.1) # 1 ~ 0.1 pygame.mixer.music.play(-1) pygame.mixer.Sound("sound/tutorbgm.wav") managetime = time.time() viruslist = [[1,0,0,0],[2,0,0,0],[3,0,0,0],[4,0,0,0],[5,0,0,0]] wave = 0 while True : screen.blit(back_ground, (0, 0)) screen.blit(interface, (1030, 0)) map1.draw() pygame.display.flip() oldtime = time.time() curtime = time.time() while True : #Copyright : 노관태 timer = Font.render("Time : " + str(int(10-(curtime-oldtime))),True,(0,0,0)) #Copyright : 노관태 screen.blit(back_ground, (0, 0)) screen.blit(interface, (1030, 0)) if(curtime-oldtime < 10) : screen.blit(timer,(850,20)) for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: position = pygame.mouse.get_pos() if build == 0: for n in range(0, len(badguy)): # Copyright : 이동우 ~ badguy[n].calDistance(position[0], position[1]) for i in range(0, len(badguy)): if badguy[i].boolDtc(): selectNum = i break selectNum = -1 # ~ Copyright : 이동우 for i in range(0, len(tower1)): # made by 김재희~ if tower1[i].selected: if position[0] >= 1048 and position[0] <= 1048 + tower1[i].sell.get_width(): if position[1] >= 512 and position[1] <= 512 + tower1[i].sell.get_height(): gold += tower1[i].sell_tower() if tower1[i].is_support == True: for k in range(0, len(support_index)): if support_index[k] == i: support_index.pop(k) break for k in range(0, len(tower1)): if tower1[k].is_support == False: tower1[k].plus_damage = 0 for j in range(0, len(support_index)): if support_index[j] > i: support_index[j] -= 1 tower1.pop(i) index -= 1 break if position[1] >= 459 and position[1] <= 459 + tower1[i].upgrade.get_height(): if tower1[i].level <= 2: if gold >= tower1[i].upgrade_price[tower1[i].level]: gold -= tower1[i].upgrade_tower() else: tower1[i].selected = tower1[i].select_tower(position[0], position[1]) else: tower1[i].selected = tower1[i].select_tower(position[0], position[1]) if position[0] >= 1050 and position[0] <= 1050 + timg[0].get_width() and \ position[1] >= 110 and position[1] <= 110 + timg[0].get_height(): if gold >= 50: gold -= 50 index += 1 build += 1 tower1.append(tower()) tower1[index].timer = time.time() if position[0] >= 1170 and position[0] <= 1170 + timg[1].get_width() and \ position[1] >= 110 and position[1] <= 110 + timg[1].get_height(): if gold >= 150: gold -= 150 index += 1 build += 1 tower1.append(short_tower()) tower1[index].timer = time.time() if position[0] >= 1050 and position[0] <= 1050 + timg[2].get_width() and \ position[1] >= 230 and position[1] <= 230 + timg[2].get_height(): if gold >= 100: gold -= 100 index += 1 build += 1 tower1.append(long_tower()) tower1[index].timer = time.time() if position[0] >= 1170 and position[0] <= 1170 + timg[3].get_width() and \ position[1] >= 230 and position[1] <= 230 + timg[3].get_height(): if gold >= 200: gold -= 200 index += 1 build += 1 tower1.append(support_tower()) support_index.append(index) tower1[index].timer = time.time() elif build == 1: for i in range(0, len(tower1)): if (i != index): if (position[0] <= tower1[i].x + timg[0].get_width() / 2 + 15 and position[0] >= tower1[ i].x - timg[0].get_width() / 2 - 15\ and position[1] <= tower1[i].y + timg[0].get_height() / 2 + 15 and position[1] >= tower1[i].y - timg[0].get_height() / 2 - 15): build_ok = False break else: build_ok = True if on_roadt(position[0], position[1]) == False : if (build_ok == True) : build -= 1 if tower1[index].tower_name == "normal tower":# Copyright : 노관태~ touer[0] = True if tower1[index].tower_name == "short tower": touer[1] = True if tower1[index].tower_name == "long tower": touer[2] = True if tower1[index].tower_name == "support tower":# ~ Copyright : 노관태 touer[3] = True if position[0] >= 1150 and position[0] <= 1150 + cancel_img.get_width() and \ position[1] >= 615 and position[1] <= 615 + cancel_img.get_height(): build -= 1 if tower1[index].tower_name == "normal tower": # Copyright : 노관태~ gold += 50 if tower1[index].tower_name == "short tower": gold += 150 if tower1[index].tower_name == "long tower": gold += 100 if tower1[index].tower_name == "support tower": # ~ Copyright : 노관태 gold += 200 if tower1[i].is_support: support_index.pop() index -= 1 tower1.pop() # ~made by 김재희 if build == 1: # made by 김재희~ position = pygame.mouse.get_pos() tower1[index].build_tower(position[0], position[1]) for i in range(0, len(tower1)): attack_on = False if len(badguy) > 0: for j in range(0, len(badguy)): if build == 1: if index == i : break if (tower1[i].is_support != True): if (badguy[j].center[0] - tower1[i].x) * (badguy[j].center[0] - tower1[i].x) \ + (badguy[j].center[1] - tower1[i].y) * (badguy[j].center[1] - tower1[i].y) <= tower1[i].range * \ tower1[i].range: attack_on = True if tower1[i].tower_attack(game_timer): badguy[j].hp -= tower1[i].damage + tower1[i].plus_damage if badguy[j].hp <= 0: if badguy[j].name == "MERS virus": # Copyright : 노관태~ score += 10 gold += 7 if badguy[j].name == "ZIKA virus": score += 15 gold += 8 if badguy[j].name == "EBOLA virus": score += 20 gold += 10 if badguy[j].name == "CORONA virus": # ~ Copyright : 노관태 score += 300 gold += 300 badguy[j].dead() badguy.pop(j) if selectNum == j: selectNum = -1 elif selectNum > j: selectNum -= 1 vindex -= 1 break break else: if tower1[i].is_support == False: tower1[i].attack = 0 if tower1[i].is_support == False: if attack_on == False: tower1[i].attack = 0 if build == 0: for i in support_index: exist = False for j in range(0, len(tower1)): if tower1[j].is_support == False: if (tower1[j].x - tower1[i].x) * (tower1[j].x - tower1[i].x) \ + (tower1[j].y - tower1[i].y) * (tower1[j].y - tower1[i].y) <= tower1[i].range * \ tower1[i].range: tower1[j].plus_damage = tower1[i].plus_damage exist = True tower1[i].tower_attack(game_timer) if exist == False: tower1[i].attack = 0 for i in range(0, len(tower1)): tower1[i].blit_tower(screen) for i in range(0, len(enemy1)): screen.blit(enemy1[i].enemy_img, ( enemy1[i].x - enemy1[i].enemy_img.get_width() / 2, enemy1[i].y - enemy1[i].enemy_img.get_height() / 2)) # ~made by 김재희 gold_font = font.render(str(gold), True, (0, 0, 0)) life_font = font.render(str(life),True,(0,0,0)) screen.blit(life_font, (1080, 32)) screen.blit(gold_font, (1195, 32)) screen.blit(timg[0], (1050, 112)) screen.blit(timg[1], (1175, 115)) screen.blit(timg[2], (1052, 238)) screen.blit(timg[3], (1177, 233)) screen.blit(cancel_img, (1150, 615)) game_timer = time.time() map1.draw() chal = challenge.challenge_1_5_8_9(chal,virus.Virus.AllNum,gold,touer) for z in range (0,7,1) : if chal_sv[z] ==0 and chal[z] == 1 : chal_sv[z] = 2 managetime = time.time() for z in range(0, 7, 1): if chal_sv[z] == 2 or chal_sv[z] == 3 : if curtime - managetime < 3 : scr_giv = font.render("+" + str(chal_scr[z]),True,(0,0,0)) screen.blit(scr_giv,(150,680)) mon_giv = font.render("+" + str(chal_money[z]), True, (0, 0, 0)) screen.blit(mon_giv, (1190, 60)) if chal_sv[z] == 2 : score += int(chal_scr[z]) gold += int(chal_money[z]) chal_sv[z] =3 screen.blit(chal_img[z],(0,0)) elif curtime - managetime > 3 : if chal_sv[z] == 3 : chal_sv[z] =1 wave_print = Font.render("Wave : " + str(int(wave + 1)), True, (0, 0, 0)) Score_print = Font.render("Score : " + str(int(score)), True, (0, 0, 0)) screen.blit(Score_print, (10, 620)) screen.blit(wave_print, (10, 580)) chal = challenge.challenge_6(chal, life) if chal_sv[7] == 0 and chal[7] == 1: chal_sv[7] = 2 managetime = time.time() if chal_sv[7] == 2 or chal_sv[7] == 3: if curtime - managetime < 3: scr_giv = font.render("+" + str(chal_scr[7]), True, (0, 0, 0)) screen.blit(scr_giv, (150, 680)) mon_giv = font.render("+" + str(chal_money[7]), True, (0, 0, 0)) screen.blit(mon_giv, (1190, 60)) if chal_sv[7] == 2: score += int(chal_scr[7]) gold += int(chal_money[7]) chal_sv[7] = 3 screen.blit(chal_img[7], (0, 0)) elif curtime - managetime > 3: if chal_sv[7] == 3: chal_sv[7] = 1 if(curtime-oldtime > 10) : # 타이머 구현 #copyright 이동우 if count < viruslist[wave][0]: type_virus = 0 elif count >= viruslist[wave][0] and count < viruslist[wave][0] + viruslist[wave][1]: type_virus = 1 elif count >= viruslist[wave][0] + viruslist[wave][1] and count < viruslist[wave][0] + viruslist[wave][ 1] + viruslist[wave][2]: type_virus = 2 elif count >= viruslist[wave][0] + viruslist[wave][1] + viruslist[wave][2]: type_virus = 3 if count < viruslist[wave][0] + viruslist[wave][1] + viruslist[wave][2] + viruslist[wave][3] : if curtime - vtimer >= 1 : count += 1 vtimer = curtime badguy.append(virus.Virus(type_virus)) vindex += 1 badguy[vindex].setType() badguy[vindex].setPos([900, 720]) badguy[vindex].path = [[900,720],[900,300],[120,300],[120,60],[0,60]] #바이러스 좌표 조정 #copyright 이동우 for n in range(0, len(badguy)): if badguy[n].pos[0] < badguy[n].x_size: # 바이러스가 맵 끝에 도달하면 vindex -= 1 life -= badguy[n].dmg badguy.pop(n) if selectNum == n: selectNum = -1 elif selectNum > n: selectNum -= 1 break else: badguy[n].move() # 바이러스 화면 출력 #copyright 이동우 for n in range(0, len(badguy)): x = badguy[n].pos[0] y = badguy[n].Repeat(badguy[n].pos[1]) screen.blit(badguy[n].img, (x, y)) # 바이러스 클릭 시 바이러스 정보 출력 #copyright 이동우 if selectNum == -1: pass elif selectNum != -1: badguy[selectNum].drawInfo(screen, 1085, 370) if(life <=0) : # copyright -shin hyuk jin pygame.mixer.init() pygame.mixer.music.load("sound/damage.wav") pygame.mixer.music.set_volume(0.1) # 1 ~ 0.1 pygame.mixer.music.play() pygame.mixer.Sound("sound/damage.wav") Gameoverbool = True break if count >= viruslist[wave][0] + viruslist[wave][1] + viruslist[wave][2] + viruslist[wave][3] : if len(badguy) == 0 : count = 0 gold += 100 wave += 1 if wave == 5: #copyright-shin pygame.mixer.init() pygame.mixer.music.load("sound/roundswap.wav") pygame.mixer.music.set_volume(0.1) # 1 ~ 0.1 pygame.mixer.music.play() pygame.mixer.Sound("sound/roundswap.wav") score += life*100 score += gold GameOver.GameClear(screen, score, life, gold) return 1 break pygame.display.flip() curtime = time.time() if Gameoverbool : GameOver.GameOver(screen,score,life,gold) return 1 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit(0)
def Map_1_starting(screen): chal = [0, 0, 0, 0, 0, 0, 0, 0, 0] chal_sv = [0, 0, 0, 0, 0, 0, 0, 0, 0] touer = [False, False, False, False] chal_scr = [500, 1000, 2000, 4000, 6000, 2000, 500, 3000, 5000] #도전과제 달성시 보상 chal_money = [100, 200, 350, 550, 700, 200, 100, 500, 0] WHITE = (255, 255, 255) BLACK = (0, 0, 0) back_ground = pygame.image.load("image/virus/map2.png") interface = pygame.image.load("image/virus/interface.png") map1 = map.map(screen) map1.ch = [[120, 60], [120, 420], [300, 420], [300, 180], [540, 180], [540, 420], [780, 420], [780, 720]] map1.set([0, 60], [1280, 600]) chal_img = [] chal_img.append(pygame.image.load("image/challenge/chal_1.png")) chal_img.append(pygame.image.load("image/challenge/chal_2.png")) chal_img.append(pygame.image.load("image/challenge/chal_3.png")) chal_img.append(pygame.image.load("image/challenge/chal_4.png")) chal_img.append(pygame.image.load("image/challenge/chal_5.png")) chal_img.append(pygame.image.load("image/challenge/chal_6.png")) chal_img.append(pygame.image.load("image/challenge/chal_7.png")) chal_img.append(pygame.image.load("image/challenge/chal_8.png")) support_index = [] life = 20 tower1 = [] enemy1 = [] index = -1 vindex = -1 timg = [] timg.append(pygame.image.load("tower/normal_tower1.png")) timg.append(pygame.image.load("tower/short_tower1.png")) timg.append(pygame.image.load("tower/long_tower1.png")) timg.append(pygame.image.load("tower/support_tower1.png")) cancel_img = pygame.image.load("tower/cancel.png") build = 0 build_ok = True game_timer = time.time() old_time = game_timer gold = 1000 round123 = 0 count = 0 Font = pygame.font.Font(None, 52) font = pygame.font.Font(None, 26) vtimer = time.time() screen.blit(back_ground, (0, 0)) map1.draw() Gameoverbool = False ## 바이러스 badguy = [] selectNum = -1 count = 0 type_virus = 0 # bgm pygame.mixer.init() pygame.mixer.music.load("sound/gamebgm.wav") pygame.mixer.music.set_volume(0.1) # 1 ~ 0.1 pygame.mixer.music.play() pygame.mixer.Sound("sound/gamebgm.wav") while True: screen.blit(back_ground, (0, 0)) screen.blit(interface, (1030, 0)) map1.draw() pygame.display.flip() oldtime = time.time() curtime = time.time() while True: #Copyright : 노관태 timer = Font.render("Time : " + str(int(10 - (curtime - oldtime))), True, (0, 0, 0)) #Copyright : 노관태 screen.blit(back_ground, (0, 0)) screen.blit(interface, (1030, 0)) if (curtime - oldtime < 10): screen.blit(timer, (850, 20)) for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: position = pygame.mouse.get_pos() for n in range(0, len(badguy)): # Copyright : 이동우 ~ badguy[n].calDistance(position[0], position[1]) for i in range(0, len(badguy)): if badguy[i].boolDtc(): selectNum = i break selectNum = -1 # ~ Copyright : 이동우 if build == 0: # made by 김재희~ for i in range(0, len(tower1)): if tower1[i].selected: if position[0] >= 1048 and position[ 0] <= 1048 + tower1[i].sell.get_width( ): if position[1] >= 512 and position[ 1] <= 512 + tower1[ i].sell.get_height(): gold += tower1[i].sell_tower() if tower1[i].is_support == True: for k in range( 0, len(support_index)): if support_index[k] == i: support_index.pop(k) break for k in range(0, len(tower1)): if tower1[ k].is_support == False: tower1[k].plus_damage = 0 tower1.pop(i) index -= 1 break if position[1] >= 459 and position[ 1] <= 459 + tower1[ i].upgrade.get_height(): if tower1[i].level <= 2: if gold >= tower1[i].upgrade_price[ tower1[i].level]: if tower1[i].level == 2: tower1[i].upgrade_tower() else: gold -= tower1[ i].upgrade_tower() else: tower1[i].selected = tower1[ i].select_tower( position[0], position[1]) else: tower1[i].selected = tower1[i].select_tower( position[0], position[1]) if position[0] >= 1050 and position[0] <= 1050 + timg[0].get_width() and \ position[1] >= 110 and position[1] <= 110 + timg[0].get_height(): if gold >= 50: gold -= 50 index += 1 build += 1 tower1.append(tower()) tower1[index].timer = time.time() if position[0] >= 1170 and position[0] <= 1170 + timg[1].get_width() and \ position[1] >= 110 and position[1] <= 110 + timg[1].get_height(): if gold >= 100: gold -= 100 index += 1 build += 1 tower1.append(short_tower()) tower1[index].timer = time.time() if position[0] >= 1050 and position[0] <= 1050 + timg[2].get_width() and \ position[1] >= 230 and position[1] <= 230 + timg[2].get_height(): if gold >= 100: gold -= 100 index += 1 build += 1 tower1.append(long_tower()) tower1[index].timer = time.time() if position[0] >= 1170 and position[0] <= 1170 + timg[3].get_width() and \ position[1] >= 230 and position[1] <= 230 + timg[3].get_height(): if gold >= 100: gold -= 100 index += 1 build += 1 tower1.append(support_tower()) support_index.append(index) tower1[index].timer = time.time() elif build == 1: for i in range(0, len(tower1)): if (i != index): if (position[0] <= tower1[i].x + timg[0].get_width() / 2 and position[0] >= tower1[ i].x - timg[0].get_width() / 2 \ and position[1] <= tower1[i].y + timg[0].get_height() / 2 and position[1] >= tower1[i].y - timg[0].get_height() / 2): build_ok = False break else: build_ok = True if build_ok == True: build -= 1 if tower1[index].tower_name == "normal tower": touer[0] = True if tower1[index].tower_name == "short tower": touer[1] = True if tower1[index].tower_name == "long tower": touer[2] = True if tower1[index].tower_name == "support tower": touer[3] = True if position[0] >= 1150 and position[0] <= 1150 + cancel_img.get_width() and \ position[1] >= 615 and position[1] <= 615 + cancel_img.get_height(): index -= 1 if tower1[i].is_support: support_index.pop() tower1.pop() if build == 1: # made by 김재희~ position = pygame.mouse.get_pos() tower1[index].build_tower(position[0], position[1]) for i in range(0, len(tower1)): attack_on = False if len(badguy) > 0: for j in range(0, len(badguy)): if build == 1: if index == i: break if (tower1[i].is_support != True): if (badguy[j].center[0] - tower1[i].x) * (badguy[j].center[0] - tower1[i].x) \ + (badguy[j].center[1] - tower1[i].y) * (badguy[j].center[1] - tower1[i].y) <= tower1[i].range * \ tower1[i].range: attack_on = True if tower1[i].tower_attack(game_timer): badguy[j].hp -= tower1[i].damage + tower1[ i].plus_damage if badguy[j].hp <= 0: badguy[j].dead() badguy.pop(j) if selectNum == j: selectNum = -1 elif selectNum > j: selectNum -= 1 vindex -= 1 break break else: if tower1[i].is_support == False: tower1[i].attack = 0 if tower1[i].is_support == False: if attack_on == False: tower1[i].attack = 0 if build == 0: for i in support_index: exist = False for j in range(0, len(tower1)): if tower1[j].is_support == False: if (tower1[j].x - tower1[i].x) * (tower1[j].x - tower1[i].x) \ + (tower1[j].y - tower1[i].y) * (tower1[j].y - tower1[i].y) <= tower1[i].range * \ tower1[i].range: tower1[j].plus_damage = tower1[i].plus_damage exist = True tower1[i].tower_attack(game_timer) if exist == False: tower1[i].attack = 0 # ~made by 김재희 for i in range(0, len(tower1)): tower1[i].blit_tower(screen) for i in range(0, len(enemy1)): screen.blit( enemy1[i].enemy_img, (enemy1[i].x - enemy1[i].enemy_img.get_width() / 2, enemy1[i].y - enemy1[i].enemy_img.get_height() / 2)) gold_font = font.render(str(gold), True, (0, 0, 0)) life_font = font.render(str(life), True, (0, 0, 0)) screen.blit(life_font, (1080, 32)) screen.blit(gold_font, (1195, 32)) screen.blit(timg[0], (1050, 112)) screen.blit(timg[1], (1175, 115)) screen.blit(timg[2], (1052, 238)) screen.blit(timg[3], (1177, 233)) screen.blit(cancel_img, (1150, 615)) game_timer = time.time() map1.draw() chal = challenge.challenge_1_5_8_9(chal, virus.Virus.AllNum, gold, touer) for z in range(0, 7, 1): if chal_sv[z] == 0 and chal[z] == 1: chal_sv[z] = 2 managetime = time.time() for z in range(0, 7, 1): if chal_sv[z] != 0: if curtime - managetime < 3: scr_giv = font.render("+" + str(chal_scr[z]), True, (0, 0, 0)) screen.blit(scr_giv, (1075, 60)) mon_giv = font.render("+" + str(chal_money[z]), True, (0, 0, 0)) screen.blit(mon_giv, (1190, 60)) if chal_sv[z] == 2: gold += int(chal_money[z]) chal_sv[z] = 1 screen.blit(chal_img[z], (0, 0)) elif curtime - managetime > 3: if chal_sv[z] == 2: chal_sv[z] = 1 if (curtime - oldtime > 2): # 타이머 구현 #copyright 이동우 if count < 40: if curtime - vtimer >= 1: count += 1 vtimer = curtime badguy.append(virus.Virus(type_virus)) vindex += 1 badguy[vindex].setType() badguy[vindex].setPos([780, 720]) badguy[vindex].path = [[780, 720], [780, 420], [540, 420], [540, 180], [300, 180], [300, 420], [120, 420], [120, 60], [0, 60]] type_virus = count // 10 # 문구 출력 #copyright 이동우 virus.draw_text("remaining virus : ", screen, 110, 20, BLACK) virus.draw_text(str(len(badguy)), screen, 210, 20, BLACK) #virus.draw_text(str(selectNum), screen, 210, 40, BLACK) if life < 0: virus.draw_text("Game over", screen, 640, 300, BLACK) #바이러스 좌표 조정 #copyright 이동우 for n in range(0, len(badguy)): if badguy[n].pos[0] < badguy[n].x_size: # 바이러스가 맵 끝에 도달하면 vindex -= 1 life -= badguy[n].dmg badguy.pop(n) if selectNum == n: selectNum = -1 elif selectNum > n: selectNum -= 1 break else: badguy[n].move() # 바이러스 화면 출력 #copyright 이동우 for n in range(0, len(badguy)): x = badguy[n].pos[0] y = badguy[n].Repeat(badguy[n].pos[1]) screen.blit(badguy[n].img, (x, y)) # 바이러스 클릭 시 바이러스 정보 출력 #copyright 이동우 if selectNum == -1: pass elif selectNum != -1: badguy[selectNum].drawInfo(screen, 1085, 370) if (life <= 0): Gameoverbool = True break if count >= 40: if len(badguy) == 0: break pygame.display.flip() curtime = time.time() if Gameoverbool: GameOver.GameOver(screen, 95000, life, gold) return 1 chal = challenge.challenge_6(chal, life) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit(0)