class Screen: width = 1200 height = 800 background = pygame.image.load('resources/images/grass.png') player = pygame.image.load('resources/images/player.png') zombie1 = pygame.image.load("resources/images/zombie1.png") zombie2 = pygame.image.load("resources/images/zombie2.png") zom_num = 0 step1 = [] step2 = [] step3 = [] step4 = [] sprt_clock = 60 fpsClock = pygame.time.Clock() FPS = 100 bg_columns = background.get_width() bg_rows = background.get_height() screen = pygame.display.set_mode((width, height)) player_lst = [] for x in range(6): player_lst.append((x * 126, 126, 100, 100)) def __init__(self): self.player = Player(self.screen, self.player, 100, 100) self.zombie = Zombie(self.screen,100, 200, self.zom_num) def Start(self): while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit(0) pygame.display.update() for i in range(int(self.width // self.bg_columns) + 1): #배경 채우기 for j in range(int(self.height // self.bg_rows) + 1): self.screen.blit(self.background, (i * self.bg_columns, j * self.bg_rows)) #self.screen.blit(self.player, (100, 100)) if self.sprt_clock >= 60: self.zom_num -= 1 self.zombie.move() print(self.zom_num) self.sprt_clock-=1 elif self.sprt_clock <= 0: self.zom_num += 1 self.zombie.move() print(self.zom_num) position = pygame.mouse.get_pos()
def crear_pueblo(nombre, num_zombies, num_humanos): p = Pueblo(nombre) for i in range(num_zombies): p.add_personaje(Zombie("z" + str(i), "zombie" + str(i), 1000, random.randrange(-200, 200), p)) for i in range(num_humanos): p.add_personaje(Humano("h" + str(i), "humano" + str(i), 1000, 0, p)) return p
def fire(self, vector): global go, mark, wait if (millis() - mark > wait): go = not go mark = millis() if (go): go = False SpriteManager.spawn(Zombie(self.x, self.y, self.team))
def populate_world(self, population, initial_infection_rate, closeness): for person in range(population): self.objectList.append( Zombie(self.speed, initial_infection_rate, closeness, self.dimensions, self.length_of_immunity)) for doctor in range(self.no_doctors): self.doctorsList.append( Doctor(self.speed, closeness, self.dimensions))
def generate_monsters(self): for i in range(self.num_monsters): num = random.randint(0, 10) if num < 3: z = Zombie() z.add_observer(self) self.monsters.append(z) elif num < 6: v = Vampire() v.add_observer(self) self.monsters.append(v) elif num < 8: g = Ghoul() g.add_observer(self) self.monsters.append(g) else: w = Werewolf() w.add_observer(self) self.monsters.append(w)
def __init__(self, generated): self.monsters = [] self.isClear = False if generated: self.monsters = [Ghoul(), Vampire(), Werewolf(), Zombie()] else: num = random.randint(0, 10) for i in range(0, num): rand = random.randint(1, 4) if rand == 1: self.monsters[i] = Ghoul() elif rand == 2: self.monsters[i] = Vampire() elif rand == 3: self.monsters[i] = Werewolf() elif rand == 4: self.monsters[i] = Zombie() else: print("You broke something in home")
def __init__(self): #List of monsters in the house self.mon = [] self.p = Person() #random number of monsters in house self.ran = random.randint(1, 10) self.observers = [] #instantiates monster list for x in range(0, self.ran): self.mon = self.mon + random.sample( [Ghoul(), Vampire(), Werewolf(), Zombie(), self.p], 1)
def Step2(self): while self.step2_finisher: for event in pygame.event.get(): #종료 이벤트 if event.type == pygame.QUIT: pygame.quit() exit(0) pygame.display.update() #업데이트 for i in range(int(self.width // 100) + 1): #배경 채우기 for j in range(int(self.height // 100) + 1): self.screen.blit(self.background, (i * 100, j * 100)) self.screen.fill((128, 128, 128)) self.screen.blit(self.waydoor, (100, 100)) self.screen.blit(self.waydoor, (500, 100)) self.screen.blit(self.waydoor, (900, 100)) self.arrow = Arrow(self.screen, 100, 700) self.arrow.draw() self.player2.move() #플레이어 무브함수 self.collider.collide() #충돌 함수 self.healgauge = self.collider.heallgauge pygame.display.update() for zombie in self.zombies: #몹의 객체만큼 zombie.move() #몹 이동 함수 pygame.display.update() self.zom_timer -= 1 if self.zom_timer == 0: zombie = Zombie(self.screen, 0, random.randint(50, self.height - 100), 16) #위치랜덤의 속도8인 몹 객체 생성 self.zombies.append(zombie) #리스트에 추가 self.zom_timer = 20 if self.healgauge < 0: break if self.player2.colliderect(self.arrow): self.step2_finisher = False if self.healgauge < 0: #체력게이지가 0보다 작으면 self.wl.print() #win or lose 출력
def poserBloc(self, event): #Place un bloc sous la souris (clic gauche) x = int(event.x / 32) y = int(event.y / 32) if ( self.testCollision(x, y) or not self.niveauActuel[y][x] == 0 ): #On n'execute pas la suite si il y a quelque chose a l'endrois ciblé return if (self.blocActuel == 1): self.create_image(16 + x * 32, 16 + y * 32, image=self.murTexture) self.niveauActuel[y][x] = 1 if (self.blocActuel == 2): self.zombies.append(Zombie(x, y, len(self.zombies), self)) self.Z.append( self.create_image( 16 + self.zombies[len(self.zombies) - 1].getPositionX() * 32, 16 + self.zombies[len(self.zombies) - 1].getPositionY() * 32, image=self.zombieTexture)) if (self.blocActuel == 3): self.create_image(16 + x * 32, 16 + y * 32, image=self.boutonTexture) self.niveauActuel[y][x] = 3 if (self.blocActuel == 4): self.portes.append( self.create_image(16 + x * 32, 16 + y * 32, image=self.porteTexture)) self.portesX.append(x) self.portesY.append(y) self.niveauActuel[y][x] = 4 if (self.blocActuel == 5): self.create_image(16 + x * 32, 16 + y * 32, image=self.drapeauTexture) self.niveauActuel[y][x] = 5 self.premierPlan()
def main(): # hero_health = 10 # hero_power = 5 # goblin_health = 6 # goblin_power = 2 hero = Hero("Lancelot", 10, 3) goblin = Goblin("Cretan", 8, 1) zombie = Zombie("Zombo", 5, 2) enemy = goblin while enemy.is_alive() and hero.is_alive(): hero.print_status() enemy.print_status() print() print("What do you want to do?") print("1. fight enemy") print("2. do nothing") print("3. flee") print("> ",) user_input = input() if user_input == "1": # Hero attacks enemy hero.attack(enemy) print("You do %d damage to the enemy." % hero.power) if not enemy.is_alive(): print("The enemy is dead.") elif user_input == "2": pass elif user_input == "3": print("Goodbye.") break else: print("Invalid input %r" % user_input) if enemy.is_alive(): # Goblin attacks hero enemy.attack(hero) print("The enemy does %d damage to you." % goblin.power) if not hero.is_alive(): print("You are dead.")
def __init__(self, name): self.inv = Inventory() self.dim = Portal() self.ui = Interface() self.loader = BlockLoader() self.no_quest = Quest(desc="No quest.", name="None") self.you = Player(self.inv, name, self.no_quest) self.mob1 = Cow() self.mob2 = Gnu() self.mob3 = Zombie() self.mob4 = Grue() self.mob5 = Kitty() self.no_quest = Quest(desc="No quest.", name="None") self.hell_quest = Quest(desc="Conquer Hell!", name="Crusader") self.sky_quest = Quest(desc="Conquer the Sky Dimension!", name="Hostile Heavenly Takeover") self.magic_quest = Quest(desc="Magify something.", name="We Don't Have to Explain It") self. self.sk = SkyKing() self.satan = Lucifer()
def createMonster(self, house: Home): """Method randomly creates and inserts monsters in each home Parameters house - Home object """ x = randint(1, 10) house.population = 0 for i in range(0, x): m_type = randint(0, 4) if m_type == 0: monster = Person() monster.home = house house.thrall.append(monster) elif m_type == 1: house.population += 1 monster = Zombie() monster.home = house house.thrall.append(monster) elif m_type == 2: house.population += 1 monster = Vampire() monster.home = house house.thrall.append(monster) elif m_type == 3: house.population += 1 monster = Ghouls() monster.home = house house.thrall.append(monster) elif m_type == 4: house.population += 1 monster = Werewolf() monster.home = house house.thrall.append(monster) self.totalMonsters += house.population
def zombie_respawn(zombie_type, zombies_number, distance): for i in range(zombies_number): zombie_position_h = choice(zombie_position_y) zombie_position_w = choice(zombie_position_x) if zombie_type == "weak_slow": zombie_weak = Zombie(5, 5, 6, choice(zombie_types)) # HP, dano, velocidade zombie_weak.rect.x = zombie_position_w + distance zombie_weak.rect.y = zombie_position_h all_sprites_list.add(zombie_weak) enemy_list.add(zombie_weak) if zombie_type == "weak_fast": zombie_weak = Zombie(5, 5, 7, choice(zombie_types)) zombie_weak.rect.x = zombie_position_w + distance zombie_weak.rect.y = zombie_position_h all_sprites_list.add(zombie_weak) enemy_list.add(zombie_weak) if zombie_type == "medium_slow": zombie_medium = Zombie(7, 7, 6, choice(zombie_types)) zombie_medium.rect.x = zombie_position_w + distance zombie_medium.rect.y = zombie_position_h all_sprites_list.add(zombie_medium) enemy_list.add(zombie_medium) if zombie_type == "medium_fast": zombie_medium = Zombie(7, 7, 7, choice(zombie_types)) zombie_medium.rect.x = zombie_position_w + distance zombie_medium.rect.y = zombie_position_h all_sprites_list.add(zombie_medium) enemy_list.add(zombie_medium) if zombie_type == "strong_slow": zombie_medium = Zombie(10, 10, 7, choice(zombie_types)) zombie_medium.rect.x = zombie_position_w + distance zombie_medium.rect.y = zombie_position_h all_sprites_list.add(zombie_medium) enemy_list.add(zombie_medium) if zombie_type == "strong_fast": zombie_strong = Zombie( 10, 10, 9, choice(zombie_types)) # testando um tipo de zumbi. zombie_strong.rect.x = zombie_position_w + distance zombie_strong.rect.y = zombie_position_h all_sprites_list.add(zombie_strong) enemy_list.add(zombie_strong)
clock = pygame.time.Clock() survivor = Survivor(Tile.TILE_SIZE * 2, Tile.TILE_SIZE * 4) avg = 0 while survivor.health > 0: # USER INPUT EventResponder.userInteraction(screen, survivor) # UPDATE GAME AStar(survivor, total_frames, FPS) Zombie.spawn(total_frames, FPS) survivor.movement() # RENDING ACTIONS Tile.update(screen) Bullet.update(screen) Zombie.update(screen, survivor) survivor.draw(screen) miscellaneous.display_health_bar(screen, survivor.health , SCREEN_WIDTH) pygame.display.flip() clock.tick(FPS) total_frames += 1
def main(): global text,choose global sun_num_surface running = True # index必须初始化为0,否则第一张图片进不去屏幕 index = 0 while running: # if index >= 130: # index = 0 clock.tick(20) # if not pygame.mixer.music.get_busy(): # pygame.mixer.music.play() #2s产生一个太阳花 # if index % 40 == 0: # sun = Sun(sunflower.rect) # sunList.add(sun) #3s产生一个子弹 # if index % 30 == 0: # for sprite in spriteGroup: # if isinstance(sprite, Peashooter): # bullet = Bullet(sprite.rect, backgd_size) # spriteGroup.add(bullet) for bullet in bulletGroup: for zombie in zombieGroup: # 直接使用下列函数实现碰撞检测, # 这个函数接收两个精灵作为参数,返回值是一个bool变量。 if pygame.sprite.collide_mask(bullet,zombie): zombie.energy -= 1 bulletGroup.remove(bullet) for wallNut in wallNutGroup: # 只要僵尸与植物接触就是zombie.isMeetWallNut = True # 接触之后将其放入精灵建立的set函数产生的无序列表中 for zombie in zombieGroup: if pygame.sprite.collide_mask(wallNut, zombie): zombie.isMeetWallNut = True wallNut.zombies.add(zombie) for peaShooter in peaShooterGroup: for zombie in zombieGroup: if pygame.sprite.collide_mask(peaShooter, zombie): zombie.isMeetWallNut = True peaShooter.zombies.add(zombie) for sunFlower in sunFlowerGroup: for zombie in zombieGroup: if pygame.sprite.collide_mask(sunFlower, zombie): zombie.isMeetWallNut = True sunFlower.zombies.add(zombie) # 图片绘制 screen.blit(bg_img_obj,(0,0)) screen.blit(sunbackImg,(250,0)) # 这个是放植物的框图 screen.blit(sun_num_surface,(270,60)) # 绘制种子图片在框框里面 screen.blit(flower_seed, (330, 10)) screen.blit(wallNut_seed, (380, 10)) screen.blit(peashooter_seed, (430, 10)) # spriteGroup.update(index) # spriteGroup.draw(screen) bulletGroup.update(index) bulletGroup.draw(screen) zombieGroup.update(index) zombieGroup.draw(screen) wallNutGroup.update(index) wallNutGroup.draw(screen) peaShooterGroup.update(index) peaShooterGroup.draw(screen) sunFlowerGroup.update(index) sunFlowerGroup.draw(screen) sunList.update(index) sunList.draw(screen) # 确定鼠标点击的横纵坐标 (x,y) = pygame.mouse.get_pos() # if choose == 1: # screen.blit(sunFlowerImg,(x,y)) # elif choose == 2: # screen.blit(wallnutImg, (x, y)) # elif choose == 3: # screen.blit(peashooterImg, (x, y)) # 确定好三种植物后绘制图像位置,一般都在鼠标点击点在图片的正中心 if choose == 1: screen.blit(sunFlowerImg, (x - sunFlowerImg.get_rect().width // 2, y - sunFlowerImg.get_rect().height // 2)) if choose == 2: screen.blit(wallnutImg, (x - wallnutImg.get_rect().width // 2, y - wallnutImg.get_rect().height // 2)) if choose == 3: screen.blit(peashooterImg, (x - peashooterImg.get_rect().width // 2, y - peashooterImg.get_rect().height // 2)) # index控制确定的次数和机会,点了一次,index加一对应刷新一次屏幕 index+=1 # 精灵调用以及添加精灵组 for event in pygame.event.get(): if event.type == GEN_FLAGZOMBIE_EVENT: zombie = FlagZombie() zombieGroup.add(zombie) if event.type == GEN_ZOMBIE_EVENT: zombie = Zombie() zombieGroup.add(zombie) if event.type == GEN_SUN_EVENT: for sprite in sunFlowerGroup: # 返回当前时间的时间戳,控制太阳出现的时间 now = time.time() if now - sprite.lasttime >= 5: sun = Sun(sprite.rect) sunList.add(sun) sprite.lasttime = now if event.type == GEN_BULLET_EVENT: for sprite in peaShooterGroup: bullet = Bullet(sprite.rect, backgd_size) bulletGroup.add(bullet) if event.type == pygame.QUIT: running = False if event.type == pygame.MOUSEBUTTONDOWN: pressed_key = pygame.mouse.get_pressed() print(pressed_key) if pressed_key[0] == 1: pos = pygame.mouse.get_pos() print(pos) x,y = pos if 330<=x<=380 and 10<=y<=80 and int(text) >= 50: print('点中了太阳花卡片') choose = 1 elif 380<x<=430 and 10<=y<=80 and int(text) >= 50: print('点中了坚果卡片') choose = 2 elif 430 < x <= 480 and 10 <= y <= 80 and int(text) >= 100: print('点中了豌豆射手卡片') choose = 3 elif 250 < x < 1200 and 70<y<600: #种植植物 if choose == 1: current_time = time.time() sunFlower = SunFlower(current_time) sunFlower.rect.top = y sunFlower.rect.left = x sunFlowerGroup.add(sunFlower) choose = 0 #扣除分数 text = int(text) text -= 50 myfont = pygame.font.SysFont('arial',20) sun_num_surface = myfont.render(str(text),True,(0,0,0)) elif choose == 2: wallNut = WallNut() wallNut.rect.top = y wallNut.rect.left = x wallNutGroup.add(wallNut) choose = 0 # 扣除分数 text = int(text) text -= 50 myfont = pygame.font.SysFont('arial', 20) sun_num_surface = myfont.render(str(text), True, (0, 0, 0)) elif choose == 3: peashooter = Peashooter() peashooter.rect.top = y peashooter.rect.left = x peaShooterGroup.add(peashooter) choose = 0 # 扣除分数 text = int(text) text -= 50 myfont = pygame.font.SysFont('arial', 20) sun_num_surface = myfont.render(str(text), True, (0, 0, 0)) print('#########') print(x,y) pass else: pass for sun in sunList: if sun.rect.collidepoint(pos): sunList.remove(sun) text = str(int(text)+50) # 分数变化后循环渲染字体 sun_font = pygame.font.SysFont('arial', 20) sun_num_surface = sun_font.render(text, True, (0, 0, 0)) pygame.display.update()
def create_enemy(self, *args, **kwargs): '''Skapar Zombie(enemy)''' return Zombie(*args, **kwargs)
banner = GameOver("rsc/Menus/titlebanner.png", [25, 25], screenSize) screen.blit(banner.surface, banner.rect) screen.blit(easy.surface, easy.rect) screen.blit(medium.surface, medium.rect) screen.blit(hard.surface, hard.rect) screen.blit(back.surface, back.rect) pygame.display.flip() #-------------------------GameStart------------------------------ while len(zombies) < maxZombies: zombieSpeed = [random.randint(1, 6), random.randint(1, 6)] zombiePos = [ random.randint(map.mazeWallSize, screenWidth - map.mazeWallSize), random.randint(map.mazeWallSize, screenHeight - map.mazeWallSize) ] zombies += [Zombie(zombieSpeed, zombiePos, screenSize)] collided = True while collided: collided = False for mazeWall in map.mazeWalls: if zombies[-1].collideMazeWall(mazeWall): zombiePos = [ random.randint(map.mazeWallSize, screenWidth - map.mazeWallSize), random.randint(map.mazeWallSize, screenHeight - map.mazeWallSize) ] zombies[-1].place(zombiePos) collided = True elif zombies[-1].chase(man): zombiePos = [
def addZombie(self, spawnx, spawny, vel, health): self.zombies.append(Zombie(self.screenWidth, self.screenWidth, spawnx, spawny, vel, health, self.image, self.drawBounds)) self.zombies_am += 1
if timeNow - sunFlower.lasttime >= 5: sunFlower.lasttime = timeNow sun = Sun(sunFlower.rect) sunList.add(sun) if event.type == GENERATOR_PEASHOOTER_EVENT: # 当前是否有太阳花对象,有几个太阳花对象,就生成几个太阳 if len(peashooterList) > 0: for peashooter in peashooterList: bullet = Bullet(peashooter.rect, size) bulletList.add(bullet) if event.type == GENERATOR_ZOMBIE_EVENT: randomIndex = random.randrange(0, len(nameList)) nameOfZombie = nameList[randomIndex] zombie = Zombie(nameOfZombie) zombieList.add(zombie) if event.type == GENERATOR_FLAGZOMBIE_EVENT: flagZombie = FlagZombie() flagZombieList.add(flagZombie) if event.type == MOUSEBUTTONDOWN: mouse_pressed = pygame.mouse.get_pressed() # 判断是否按下的事鼠标左键 if mouse_pressed[0]: (x, y) = pygame.mouse.get_pos() # print(x, y) # 判断鼠标是否点中了某个卡片 if 330 <= x <= 380 and 10 <= y <= 80 and int(score) >= 50:
def spawn(self, x, y, health, speed): self.zombies.append(Zombie(x, y, health, speed))
size = width, height bg = pygame.image.load("Resources/Object/Background/Sure.png") startScreen = pygame.image.load("Resources/Object/Start Menu/Day Zeyro.png") startScreenRect = startScreen.get_rect() bgColor = r,g,b = 200, 0, 200 screen = pygame.display.set_mode(size) fullscreen = False bgImage = pygame.image.load("Resources/Object/Background/Sure.png").convert() bgImage = pygame.transform.scale (bgImage, (800, 600)) bgRect = bgImage.get_rect() player = Player([width/2, 500]) zombies = [] zombies += [Zombie([4,5], [250, 400])] bullets = [] #timer = Score([80, height - 25], "Time: ", 36) #timerWait = 0 #timerWaitMax = 6 run = False startButton = Button([width/2, height-300],"Resources/Object/Start Menu/Face.png", ) while True: while not run:
# Actually updates the screen pygame.display.update() floor_img = pygame.image.load('images/tiles/surface_tile_gray.png') wall_img = pygame.image.load("images/tiles/dark_wall.png") while survivor.health > 0: # USER INPUT EventResponder.userInteraction(screen, survivor) # UPDATE GAME AStar(survivor, total_frames, FPS) Zombie.spawn(total_frames, FPS) survivor.movement() # RENDING ACTIONS # draws either a Floor or a Wall tile for tile in Tile.list_: if tile.type == Tile.Type.FLOOR: screen.blit(floor_img, (tile.x, tile.y)) elif tile.type == Tile.Type.WALL: screen.blit(wall_img, (tile.x, tile.y)) Bullet.update(screen) Zombie.update(screen, survivor) survivor.draw(screen) display_health_bar()
def __init__(self): self.player = Player(self.screen, self.player, 100, 100) self.zombie = Zombie(self.screen,100, 200, self.zom_num)
class Main: def __init__(self, name): self.inv = Inventory() self.dim = Portal() self.ui = Interface() self.loader = BlockLoader() self.no_quest = Quest(desc="No quest.", name="None") self.you = Player(self.inv, name, self.no_quest) self.mob1 = Cow() self.mob2 = Gnu() self.mob3 = Zombie() self.mob4 = Grue() self.mob5 = Kitty() self.no_quest = Quest(desc="No quest.", name="None") self.hell_quest = Quest(desc="Conquer Hell!", name="Crusader") self.sky_quest = Quest(desc="Conquer the Sky Dimension!", name="Hostile Heavenly Takeover") self.magic_quest = Quest(desc="Magify something.", name="We Don't Have to Explain It") self. self.sk = SkyKing() self.satan = Lucifer() def clear_screen(self): if sys.platform.startswith("win32") or sys.platform.startswith("os2"): system("cls") else: sys.stdout.write('\033[2J') sys.stdout.write('\033[H') sys.stdout.flush() def run(self): while True: self.you.day += 1 sleep(1) self.clear_screen() if self.you.dimension == "Hell": self.run_hell() elif self.you.dimension == "Sky Dimension": self.run_sd() elif self.you.dimension == "Base": pass else: print "ERR" cowspawn = self.mob1.x == None and self.mob1.y == None and self.mob1.z == None zomspawn = self.mob3.x == None and self.mob3.y == None and self.mob3.z == None gruespawn = self.mob4.x == None and self.mob4.y == None and self.mob4.z == None kittyspawn = self.mob5.x == None and self.mob5.y == None and self.mob5.z == None if cowspawn and zomspawn and gruespawn and kittyspawn: self.ui.load_peaceful_graphics(self.you) elif cowspawn and not zomspawn and gruespawn and kittyspawn: self.ui.load_zombie_graphics(self.you, self.mob3) elif cowspawn and zomspawn and not gruespawn and kittyspawn: self.ui.load_grue_graphics(self.you, self.mob4) elif not cowspawn and zomspawn and gruespawn and kittyspawn: self.ui.load_cow_graphics(self.you, self.mob1) elif cowspawn and zomspawn and gruespawn and not kittyspawn: self.ui.load_kitty_graphics(self.you, self.mob5) else: pass if not zomspawn: zomcheck1 = abs(self.mob3.y) - abs(self.you.y) == 1 and abs(self.mob3.x) - abs(self.you.x) == 0 else: zomcheck1 = False if not zomspawn: zomcheck2 = abs(self.mob3.x) - abs(self.you.x) == 1 and abs(self.mob3.y) - abs(self.you.y) == 0 else: zomcheck2 = False if zomcheck1 or zomcheck2: self.you.attack(self.mob3, self.inv) else: pass if not gruespawn: gruecheck1 = abs(self.mob4.y) - abs(self.you.y) == 1 and abs(self.mob4.x) - abs(self.you.x) == 0 else: gruecheck1 = False if not gruespawn: gruecheck2 = abs(self.mob4.x) - abs(self.you.x) == 1 and abs(self.mob4.y) - abs(self.you.y) == 0 else: gruecheck2 = False if gruecheck1 or gruecheck2: self.you.attack(self.mob4, self.inv) else: pass if not cowspawn: cowcheck1 = abs(self.mob1.y) - abs(self.you.y) == 1 and abs(self.mob1.x) - abs(self.you.x) == 0 else: cowcheck1 = False if not cowspawn: cowcheck2 = abs(self.mob1.x) - abs(self.you.x) == 1 and abs(self.mob1.y) - abs(self.you.y) == 0 else: cowcheck2 = False if cowcheck1 or cowcheck2: self.you.attack(self.mob1, self.inv) else: pass if self.inv.hellporterpartone == 1 and self.inv.hellporterparttwo == 1 and self.inv.hellporterpartthree == 1: print "You have created the Hell Teleporter!" self.inv.hell_teleporter = 1 else: pass if self.inv.skykeypartone == 1 and self.inv.skykeyparttwo == 1 and self.inv.skykeypartthree == 1: print "You have created the key to the Stairway to the Sky Dimension!" self.inv.sky_key = 1 self.mob1.check_spawn(self.you, randint(10, 15)) self.mob2.check_spawn(self.you, 35) self.mob3.check_spawn(self.you, randint(20, 40)) self.mob4.check_spawn(self.you, randint(30, 45)) self.mob5.check_spawn(self.you, randint(60,75)) command = str(raw_input("> ")) if command.startswith("dig "): self.you.move(command[4:].upper()) elif command == "inventory": self.clear_screen() self.inv.list_inv() sleep(3) self.clear_screen() elif command == "inv": self.clear_screen() self.inv.list_inv() sleep(3) elif command == "craft bow": if self.inv.wood >= 3: self.inv.bow = self.inv.UNMAGIC_HAD self.you.equipped = "L1" else: print "You don't have enough materials." elif command == "craft dagger": if self.inv.wood >= 3: self.inv.dagger = self.inv.UNMAGIC_HAD self.you.equipped = "L1" else: print "You don't have enough materials." elif command == "craft sword": if self.inv.wood >= 1 and self.inv.iron >= 2: self.inv.sword = self.inv.UNMAGIC_HAD self.you.equipped = "L2" else: print "You don't have enough materials." elif command == "craft crossbow": if self.inv.wood >= 2 and self.inv.iron >= 2: self.inv.crossbow = self.inv.UNMAGIC_HAD self.you.equipped = "L2" else: print "You don't have enough materials." elif command == "craft magifier": if self.inv.iron >= 3 and self.inv.gold >= 4 and self.inv.unobtainium >= 2: self.inv.magifier = self.inv.UNMAGIC_HAD print "You created the Magifier! You can now magify weaponry with 3 involatilium and the magifier!" else: print "You don't have enough materials." elif command.startswith("magify "): if self.inv.magifier == self.inv.UNMAGIC_HAD: self.inv.magify(command[7:]) else: print "You don't have the Magifier." elif command.startswith("teleport "): if command[9:].lower() == "hell" and self.you.dimension != "Hell": if self.inv.hell_teleporter == self.inv.UNMAGIC_HAD: self.dim.teleport(self.you, "Hell") else: print "You can't teleport to Hell without the Teleporter!" elif command[9:].lower() == "sky" and self.you.dimension != "Sky": if self.inv.skydim_key == self.inv.UNMAGIC_HAD: self.dim.teleport(self.you, "Sky") else: print "You can't teleport to the Sky Dimension without the key to the stairway!" elif command[9:].lower() == "base" and self.you.dimension != "Base": self.dim.teleport(self.you, "Sky") else: print "Either that dimension doesn't exist or it has not been implemented yet." elif command == "exit": temp = raw_input("Do you really want to exit?\n>") if temp.lower() == "yes" or temp.lower() == "y": exit("Goodbye, then.") else: pass elif command == "quests": self.questboard.display_board() else: print "Unrecognized command." def run_hell(self): while True: sleep(1) self.clear_screen() if self.you.dimension == "Hell": pass elif self.you.dimension == "Sky Dimension": self.run_sd() elif self.you.dimension == "Base": self.run() else: print "ERR" self.ui.load_peaceful_graphics(self.you) command = str(raw_input("> ")) if command.startswith("dig "): self.you.move_hell(command[4:].upper()) elif command == "inventory": self.clear_screen() self.inv.list_inv() sleep(2) self.clear_screen() elif command == "inv": self.clear_screen() self.inv.list_inv() sleep(2) elif command == "fight boss" or command == "boss fight": self.satan.boss_fight(self.you, self.inv) elif command.startswith("craft "): print "You can't craft in Hell." elif command.startswith("magify "): if self.inv.magifier == self.inv.UNMAGIC_HAD: self.inv.magify(command[7:]) else: print "You don't have the Magifier." elif command.startswith("teleport"): if command[9:].lower() == "base" and self.you.dimension != "Base": self.dim.teleport(self.you, "Base") else: print "You can't teleport anywhere other than back to the base dimension while in Hell." elif command == "exit": temp = raw_input("Do you really want to exit?\n>") if temp.lower() == "yes" or temp.lower() == "y": exit("Goodbye, then.") else: pass else: print "Unrecognized command." def run_sd(self): while True: sleep(1) self.clear_screen() if self.you.dimension == "Hell": self.run_hell() elif self.you.dimension == "Sky Dimension": pass elif self.you.dimension == "Base": self.run() else: print "ERR" if command.startswith("dig "): self.you.move_sd(command[4:].upper()) elif command == "inventory": self.clear_screen() self.inv.list_inv() sleep(3) self.clear_screen() elif command == "inv": self.inv.list_inv() elif command == "boss fight": self.sk.boss_fight(self.you, self.inv) elif command.startswith("craft "): print "You can't craft in the Sky Dimension." elif command.startswith("magify "): if self.inv.magifier == self.inv.UNMAGIC_HAD: self.inv.magify(command[7:]) else: print "You don't have the Magifier." elif command == "boss fight" or command == "fight boss": print "Are you /sure/ you want to fight the Sky King? (y|n)" tmp = raw_input("> ") if tmp.lower() == "y" or tmp.lower() == "yes": sk.boss_fight(self.you, self.inv) else: pass elif command.startswith("teleport"): if command[9:].lower() == "base" and self.you.dimension != "Base": self.dim.teleport(self.you, "Base") else: print "You can't teleport anywhere other than back to the base dimension while in the Sky Dimension." elif command == "exit": temp = raw_input("Do you really want to exit?\n>") if temp.lower() == "yes" or temp.lower() == "y": exit("Goodbye, then.") else: pass else: print "Unrecognized command."
def start_battle(self, clock): pygame.display.set_caption("Fight!!") # ENTITIES # Background background = pygame.Surface(self.screen.get_size()) background.fill((0, 0, 0)) self.screen.blit(background, (0, 0)) # Sprites top_widget_size = (self.screen.get_size()[0], round(self.screen.get_size()[1] / 10)) top_widget = Boundary((self.screen.get_size()[0] / 2, round(self.screen.get_size()[1] / 20)), color=(255, 255, 0), size=top_widget_size) # Grids grid_sprites, safe_grid, grid_position = self.make_grid( (0, round(self.screen.get_size()[1] / 10))) curr_map = Map(grid_position) obsticles = curr_map.make_map(OBSTACLE_GRID_TILE) potential_box = curr_map.get_box_position() obsticles = pygame.sprite.Group(obsticles) # Boundary top_boundary = Boundary((600, 69), (255, 255, 255), (1200, 20)) left_boundary = Boundary((-4, 350), (255, 255, 255), (20, 700)) right_boundary = Boundary((1204, 350), (255, 255, 255), (20, 700)) bottom_boundary = Boundary((600, 701), (255, 255, 255), (1200, 20)) top_boundary.make_invisible() left_boundary.make_invisible() right_boundary.make_invisible() bottom_boundary.make_invisible() obsticles.add(top_boundary, left_boundary, right_boundary, bottom_boundary) # Player player_1 = Player(grid_position[7][32]) player_2 = Player(grid_position[7][0]) player_1_attacks = pygame.sprite.Group() player_2_attacks = pygame.sprite.Group() # Player score zone p1_score_zone = Boundary((0, 0), (0, 0, 0), (72, 144)) p1_score_zone.rect.topleft = (grid_position[6][0][0] - 18, grid_position[6][0][1] - 18) p1_score_zone.make_invisible() p2_score_zone = Boundary((0, 0), (0, 0, 0), (72, 144)) p2_score_zone.rect.topleft = (grid_position[6][31][0] - 18, grid_position[6][31][1] - 18) p2_score_zone.make_invisible() score_zone = [p1_score_zone, p2_score_zone] # Zombies zombie_group = pygame.sprite.Group() zombie_group.add(Zombie(player_1, player_2, (500, -5), safe_grid)) zombie_group.add(Zombie(player_1, player_2, (700, -5), safe_grid)) zombie_group.add(Zombie(player_1, player_2, (600, 350), safe_grid)) zombie_group.add(Zombie(player_1, player_2, (500, 705), safe_grid)) zombie_group.add(Zombie(player_1, player_2, (700, 705), safe_grid)) # HP Bar player_2_bar = StatBar((200, 35), (0, 255, 0), (300, 25), MAX_HP) player_1_bar = StatBar((1000, 35), (0, 255, 0), (300, 25), MAX_HP) player_1_bar.set_reversed_direction() bars = [ player_2_bar.get_full_bar((255, 0, 0)), player_1_bar.get_full_bar((255, 0, 0)) ] # Box # Item Box box_sprites = pygame.sprite.Group() # Goals flag = Flag((600, 350)) goal = CaptureFlag([player_1, player_2], score_zone, flag, num_round=3) # Label color p1_label_color = (0, 0, 255) p2_label_color = (255, 0, 0) # Flag flag_p2 = Label(44, (400, 35)) flag_p2.set_color(p2_label_color) flag_p1 = Label(44, (780, 35)) flag_p1.set_color(p1_label_color) # timer timer = Timer(30, (560, 35), 4, 20, FRAME_RATE) # Labels p2_label = Label(20, (20, 35)) p2_label.set_color(p2_label_color) p2_label.set_msg("P2 ") p1_label = Label(20, (1160, 35)) p1_label.set_color(p1_label_color) p1_label.set_msg("P1") p1_head = Label(15, (player_1.rect.centerx - 8, player_1.rect.top - 5)) p1_head.set_color(p1_label_color) p1_head.set_msg("P1") p2_head = Label(15, (player_2.rect.centerx - 8, player_2.rect.top - 5)) p2_head.set_color(p2_label_color) p2_head.set_msg("P2") p1_weapon = Label(20, (990, 56)) p1_weapon.set_msg("sada") p2_weapon = Label(20, (170, 56)) p2_weapon.set_msg("sada") labels = [ p1_label, p2_label, p1_head, p2_head, p1_weapon, p2_weapon, flag_p1, flag_p2 ] keep_going = True box_flag = True all_sprites = pygame.sprite.OrderedUpdates(grid_sprites, obsticles, flag, player_1, player_2, zombie_group, top_widget, timer, labels, bars, player_1_bar, player_2_bar) # LOOP while keep_going: if goal.screen_pause: pygame.time.wait(1500) goal.screen_pause = False # TIME clock.tick(FRAME_RATE) if timer.get_real_second() % 5 == 0: # Box if box_flag: box_flag = False box = self.generate_box(potential_box) box_sprites.add(box) all_sprites.add(box) else: box_flag = True if timer.get_real_second() % 20 == 0 and len(zombie_group) <= 1: zombie_group.add( Zombie(player_1, player_2, (500, -5), safe_grid)) zombie_group.add( Zombie(player_1, player_2, (700, -5), safe_grid)) zombie_group.add( Zombie(player_1, player_2, (600, 350), safe_grid)) zombie_group.add( Zombie(player_1, player_2, (500, 705), safe_grid)) zombie_group.add( Zombie(player_1, player_2, (700, 705), safe_grid)) all_sprites.add(zombie_group) for z in zombie_group: z.move(zombie_group) # Check for Goal if goal.check_for_win(): keep_going = False if timer.time_up(): keep_going = False flag_p1.set_msg(str(goal.score[0])) flag_p2.set_msg(str(goal.score[1])) # EVENT HANDLING for event in pygame.event.get(): if event.type == pygame.QUIT: return 3 if event.type == pygame.KEYDOWN: if event.key == pygame.K_p: keep_going = False #Player 1 event handle if pygame.key.get_pressed()[pygame.K_RIGHT]: temp_block = TempBlock(player_1.rect.center, player_1.rect.size) temp_block.rect.x += player_1.velocity if pygame.sprite.spritecollide(temp_block, obsticles, False): # Check the previous directoin if player_1.direction == 1 or player_1.prev_direction == 1: i = 1 # Check if the we continue with previous direction for more frames will be able to pass through while i < player_1.velocity * SMOOTH_TURNING_FRAME_DELAY: i += 1 temp_block.rect.y += 1 if not pygame.sprite.spritecollide( temp_block, obsticles, False): player_1.go_down() break elif player_1.direction == 3 or player_1.prev_direction == 3: i = 1 while i < player_1.velocity * SMOOTH_TURNING_FRAME_DELAY: i += 1 temp_block.rect.y -= 1 if not pygame.sprite.spritecollide( temp_block, obsticles, False): player_1.go_up() break else: pass else: player_1.go_right() elif pygame.key.get_pressed()[pygame.K_DOWN]: temp_block = TempBlock(player_1.rect.center, player_1.rect.size) temp_block.rect.y += player_1.velocity if pygame.sprite.spritecollide(temp_block, obsticles, False): if player_1.direction == 0 or player_1.prev_direction == 0: i = 1 while i < player_1.velocity * SMOOTH_TURNING_FRAME_DELAY: i += 1 temp_block.rect.x += 1 if not pygame.sprite.spritecollide( temp_block, obsticles, False): player_1.go_right() break elif player_1.direction == 2 or player_1.prev_direction == 2: i = 1 while i < player_1.velocity * SMOOTH_TURNING_FRAME_DELAY: i += 1 temp_block.rect.x -= 1 if not pygame.sprite.spritecollide( temp_block, obsticles, False): player_1.go_left() break else: pass else: player_1.go_down() elif pygame.key.get_pressed()[pygame.K_UP]: temp_block = TempBlock(player_1.rect.center, player_1.rect.size) temp_block.rect.y -= player_1.velocity if pygame.sprite.spritecollide(temp_block, obsticles, False): if player_1.direction == 0 or player_1.prev_direction == 0: i = 1 while i < player_1.velocity * SMOOTH_TURNING_FRAME_DELAY: i += 1 temp_block.rect.x += 1 if not pygame.sprite.spritecollide( temp_block, obsticles, False): player_1.go_right() break elif player_1.direction == 2 or player_1.prev_direction == 2: i = 1 while i < player_1.velocity * SMOOTH_TURNING_FRAME_DELAY: i += 1 temp_block.rect.x -= 1 if not pygame.sprite.spritecollide( temp_block, obsticles, False): player_1.go_left() break else: pass else: player_1.go_up() elif pygame.key.get_pressed()[pygame.K_LEFT]: temp_block = TempBlock(player_1.rect.center, player_1.rect.size) temp_block.rect.x -= player_1.velocity if pygame.sprite.spritecollide(temp_block, obsticles, False): if player_1.direction == 1 or player_1.prev_direction == 1: i = 1 while i < player_1.velocity * SMOOTH_TURNING_FRAME_DELAY: i += 1 temp_block.rect.y += 1 if not pygame.sprite.spritecollide( temp_block, obsticles, False): player_1.go_down() break elif player_1.direction == 3 or player_1.prev_direction == 3: i = 1 while i < player_1.velocity * SMOOTH_TURNING_FRAME_DELAY: i += 1 temp_block.rect.y -= 1 if not pygame.sprite.spritecollide( temp_block, obsticles, False): player_1.go_up() break else: pass else: player_1.go_left() if pygame.key.get_pressed()[pygame.K_RCTRL]: lst = player_1.attack() player_1_attacks.add(lst) all_sprites.add(lst) # Player 2 event handle if pygame.key.get_pressed()[pygame.K_d]: temp_block = TempBlock(player_2.rect.center, player_2.rect.size) temp_block.rect.x += player_2.velocity if pygame.sprite.spritecollide(temp_block, obsticles, False): # Check the previous directoin if player_2.direction == 1 or player_2.prev_direction == 1: i = 1 # Check if the we continue with previous direction for more frames will be able to pass through while i < player_2.velocity * SMOOTH_TURNING_FRAME_DELAY: i += 1 temp_block.rect.y += 1 if not pygame.sprite.spritecollide( temp_block, obsticles, False): player_2.go_down() break elif player_2.direction == 3 or player_2.prev_direction == 3: i = 1 while i < player_2.velocity * SMOOTH_TURNING_FRAME_DELAY: i += 1 temp_block.rect.y -= 1 if not pygame.sprite.spritecollide( temp_block, obsticles, False): player_2.go_up() break else: pass else: player_2.go_right() elif pygame.key.get_pressed()[pygame.K_s]: temp_block = TempBlock(player_2.rect.center, player_2.rect.size) temp_block.rect.y += player_2.velocity if pygame.sprite.spritecollide(temp_block, obsticles, False): if player_2.direction == 0 or player_2.prev_direction == 0: i = 1 while i < player_2.velocity * SMOOTH_TURNING_FRAME_DELAY: i += 1 temp_block.rect.x += 1 if not pygame.sprite.spritecollide( temp_block, obsticles, False): player_2.go_right() break elif player_2.direction == 2 or player_2.prev_direction == 2: i = 1 while i < player_2.velocity * SMOOTH_TURNING_FRAME_DELAY: i += 1 temp_block.rect.x -= 1 if not pygame.sprite.spritecollide( temp_block, obsticles, False): player_2.go_left() break else: pass else: player_2.go_down() elif pygame.key.get_pressed()[pygame.K_w]: temp_block = TempBlock(player_2.rect.center, player_2.rect.size) temp_block.rect.y -= player_2.velocity if pygame.sprite.spritecollide(temp_block, obsticles, False): if player_2.direction == 0 or player_2.prev_direction == 0: i = 1 while i < player_2.velocity * SMOOTH_TURNING_FRAME_DELAY: i += 1 temp_block.rect.x += 1 if not pygame.sprite.spritecollide( temp_block, obsticles, False): player_2.go_right() break elif player_2.direction == 2 or player_2.prev_direction == 2: i = 1 while i < player_2.velocity * SMOOTH_TURNING_FRAME_DELAY: i += 1 temp_block.rect.x -= 1 if not pygame.sprite.spritecollide( temp_block, obsticles, False): player_2.go_left() break else: pass else: player_2.go_up() elif pygame.key.get_pressed()[pygame.K_a]: temp_block = TempBlock(player_2.rect.center, player_2.rect.size) temp_block.rect.x -= player_2.velocity if pygame.sprite.spritecollide(temp_block, obsticles, False): if player_2.direction == 1 or player_2.prev_direction == 1: i = 1 while i < player_2.velocity * SMOOTH_TURNING_FRAME_DELAY: i += 1 temp_block.rect.y += 1 if not pygame.sprite.spritecollide( temp_block, obsticles, False): player_2.go_down() break elif player_2.direction == 3 or player_2.prev_direction == 3: i = 1 while i < player_2.velocity * SMOOTH_TURNING_FRAME_DELAY: i += 1 temp_block.rect.y -= 1 if not pygame.sprite.spritecollide( temp_block, obsticles, False): player_2.go_up() break else: pass else: player_2.go_left() if pygame.key.get_pressed()[pygame.K_SPACE]: lst2 = player_2.attack() player_2_attacks.add(lst2) all_sprites.add(lst2) # Collisions handleling for box1 in pygame.sprite.spritecollide(player_1, box_sprites, False): temp_label = TempLabel( 15, (player_1.rect.centerx - 8, player_1.rect.top - 5), 3, FRAME_RATE) temp_label.set_msg(box1.__str__()) all_sprites.add(temp_label) box1.collide(player_1) for box2 in pygame.sprite.spritecollide(player_2, box_sprites, False): box2.collide(player_2) temp_label = TempLabel( 15, (player_2.rect.centerx - 8, player_2.rect.top - 5), 3, FRAME_RATE) temp_label.set_msg(box2.__str__()) all_sprites.add(temp_label) # Zombie hit player for zombie in pygame.sprite.spritecollide(player_1, zombie_group, False): zombie.collide(player_1) for zombie in pygame.sprite.spritecollide(player_2, zombie_group, False): zombie.collide(player_2) # Players hit zombies for zombie in zombie_group: for bullet in pygame.sprite.spritecollide( zombie, player_2_attacks, False): bullet.collide(zombie) for bullet in pygame.sprite.spritecollide( zombie, player_1_attacks, False): bullet.collide(zombie) # bullet_hit_walls for wall in obsticles: for bullet in pygame.sprite.spritecollide( wall, player_1_attacks, False): bullet.end() for bullet in pygame.sprite.spritecollide( wall, player_2_attacks, False): bullet.end() # Player_1 attacks for hit in pygame.sprite.spritecollide(player_2, player_1_attacks, False): hit.collide(player_2) # Player_2 attacks for hit in pygame.sprite.spritecollide(player_1, player_2_attacks, False): hit.collide(player_1) # Player hp bar player_2_bar.set_curr_stat(player_2.health) player_1_bar.set_curr_stat(player_1.health) # Player head p1_head.set_position( (player_1.rect.centerx - 8, player_1.rect.top - 5)) p2_head.set_position( (player_2.rect.centerx - 8, player_2.rect.top - 5)) p1_weapon.set_msg(str(player_1.weapon)) p2_weapon.set_msg(str(player_2.weapon)) if player_1.is_dead(): if flag.get_player() == player_1: flag.drop() player_1.respawn() if player_2.is_dead(): if flag.get_player() == player_2: flag.drop() player_2.respawn() for zombie in zombie_group: if zombie.is_dead(): zombie.kill() all_sprites.clear(self.screen, background) all_sprites.update() all_sprites.draw(self.screen) pygame.display.flip() lst = player_1.weapon.update() player_1_attacks.add(lst) all_sprites.add(lst) lst = player_2.weapon.update() player_2_attacks.add(lst) all_sprites.add(lst) end_msg = Label(60, (500, 350)) end_msg.set_color((255, 255, 0)) end_msg.set_msg(goal.get_wining_msg()) self.screen.blit(end_msg.image, end_msg) pygame.display.flip() pygame.time.wait(2000) return 2
# sunbank_img_obj = pygame.image.load(sunbank_img_path).convert_alpha() sunbackImg = pygame.image.load('material/images/SeedBank.png').convert_alpha() flower_seed = pygame.image.load("material/images/TwinSunflower.gif") wallNut_seed = pygame.image.load("material/images/WallNut.gif") peashooter_seed = pygame.image.load("material/images/Peashooter.gif") text = '1000' sun_font = pygame.font.SysFont('arial',20) sun_num_surface = sun_font.render(text,True,(0,0,0)) # peashooter = Peashooter() # sunflower = SunFlower() # wallnut = WallNut() zombie = Zombie() spriteGroup = pygame.sprite.Group() # spriteGroup.add(peashooter) # spriteGroup.add(sunflower) # spriteGroup.add(wallnut) spriteGroup.add(zombie) sunList = pygame.sprite.Group() # sunList = [] clock = pygame.time.Clock() GEN_SUN_EVENT = pygame.USEREVENT + 1 pygame.time.set_timer(GEN_SUN_EVENT,1000)
def afficher(self, niveau): #Affichage et mise en place d'un niveau self.delete("all") self.niveau = niveau #Chargement du niveau if niveau == -1: self.niveauActuel = deepcopy(self.custom) if niveau == 0: self.niveauActuel = deepcopy(self.tuto) if niveau == 1: self.niveauActuel = deepcopy(self.niveau1) if niveau == 2: self.niveauActuel = deepcopy(self.niveau2) if niveau == 3: self.niveauActuel = deepcopy(self.niveau3) if niveau == 4: self.niveauActuel = deepcopy(self.niveau4) if niveau == 5: self.niveauActuel = deepcopy(self.niveau5) if niveau == 6: self.niveauActuel = deepcopy(self.fin) #Initialisation des manches self.deplacement = 0 #Création des joueurs self.joueur1 = Joueur(2, 2, 1, self) self.joueur2 = Joueur(4, 2, 2, self) #Création des listes (remplies lors de la lecture de la matrice) self.zombies = [] self.portes = [] self.portesX = [] self.portesY = [] #Affichage de la grille (21x21 blocs) for y in range(0, 21): for x in range(0, 21): if (self.niveauActuel[y][x] == 0): #Sol self.create_image(16 + x * 32, 16 + y * 32, image=self.solTexture) if (self.niveauActuel[y][x] == 1): #Mur self.create_image(16 + x * 32, 16 + y * 32, image=self.murTexture) if (self.niveauActuel[y][x] == 2): #Zombie self.zombies.append(Zombie(x, y, len(self.zombies), self)) self.create_image(16 + x * 32, 16 + y * 32, image=self.solTexture) if (self.niveauActuel[y][x] == 3): #Bouton self.create_image(16 + x * 32, 16 + y * 32, image=self.solTexture) self.create_image(16 + x * 32, 16 + y * 32, image=self.boutonTexture) if (self.niveauActuel[y][x] == 4): #Porte self.portes.append( self.create_image(16 + x * 32, 16 + y * 32, image=self.porteTexture)) self.portesX.append(x) self.portesY.append(y) if (self.niveauActuel[y][x] == 5): #Drapeau self.create_image(16 + x * 32, 16 + y * 32, image=self.solTexture) self.create_image(16 + x * 32, 16 + y * 32, image=self.drapeauTexture) #Affichage des joueurs self.J1 = self.create_image(16 + self.joueur1.getPositionX() * 32, 16 + self.joueur1.getPositionY() * 32, image=self.J1Texture) self.J2 = self.create_image(16 + self.joueur2.getPositionX() * 32, 16 + self.joueur2.getPositionY() * 32, image=self.J2Texture) self.Z = [] #Affichage de tout les zombies for i in range(0, len(self.zombies)): self.Z.append( self.create_image(16 + self.zombies[i].getPositionX() * 32, 16 + self.zombies[i].getPositionY() * 32, image=self.zombieTexture)) #Tutoriel if (niveau == 0): self.create_image(336, 336, image=self.tutorielTexture) #Editeur if (niveau == -1): self.activeEditeur( True ) #Active l'éditeur si le niveau actuel est -1 (terrain a éditer) self.create_image(26, 26, image=self.editeurTexture) #Interface editeur self.blocEditeur = self.create_image( 26, 26, image=self.murTexture) #Interface editeur else: self.activeEditeur( False ) #Active l'éditeur si le niveau actuel est -1 (terrain a éditer) #Change le titre de la fenêtre en fonction du niveau if (niveau == -1): self.fenetre.title("Zombies ! - Editeur") elif (niveau == 0): self.fenetre.title("Zombies ! - Tutoriel") else: self.fenetre.title("Zombies ! - Niveau " + str(niveau)) self.pack()
from Goblin import Goblin from Store import Store from Wizard import Wizard from Spider import Spider from Snake import Snake from Medic import Medic from Shadow import Shadow from Zombie import Zombie if __name__ == "__main__": hero = Hero() enemies = [ Goblin(), Wizard(), Medic(), Shadow(), Zombie(), Spider(), Snake() ] battle_engine = Battle() shopping_engine = Store() for enemy in enemies: hero_won = battle_engine.do_battle(hero, enemy) if not hero_won: print("YOU LOSE!") exit(0) shopping_engine.do_shopping(hero) print("YOU WIN!")
class Game: pygame.init() BLACK = 0, 0, 0 WHITE = 255, 255, 255 screen = pygame.display.set_mode((600, 500)) Player.Player.screen = screen Zombie.screen = screen z1_coors = [60, 440, 15, 15] player = Player.Player(560, 460) zombie = Zombie(BLACK, z1_coors) zombie.dx = 5 level = [ "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWW", "W W W", "W W W", "W W W", "W WWWWWWWWWWWWWWWWWWWWWW W", "W W", "W W", "W W WWWWWWWWWWW", "W W W", "W W W", "W WWWWWWWWWWW W", "WWWWW WWWWWWWW W", "W W W", "W W W", "W WWWWWWWWWWWWWWWWWWWWWWWWW", "W W W W", "W W W", "W W W", "W W W W", "W WWWWWWWWWWWWWW W W", "W W W W", "W W W", "W W W", "W W W", "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWW", ] x = y = 0 for row in level: for col in row: if col == "W": Wall((x, y)) x += 20 y += 20 x = 0 while True: screen.fill(WHITE) for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_s: player.delta_move_x = -1 player.direction = 'left' elif event.key == pygame.K_e: player.delta_move_y = -1 player.direction = 'up' elif event.key == pygame.K_f: player.delta_move_x = 1 player.direction = 'right' elif event.key == pygame.K_d: player.delta_move_y = 1 player.direction = 'down' elif event.key == pygame.K_SPACE: player.shootAction() if event.type == pygame.KEYUP: if event.key == pygame.K_s or event.key == pygame.K_f: player.delta_move_x = 0 if event.key == pygame.K_e or event.key == pygame.K_d: player.delta_move_y = 0 player.x += player.delta_move_x player.y += player.delta_move_y zombie.move(Wall.walls, player.x, player.y) # zombie.moveRight(5, .5) player.draw(player.delta_move_x, player.delta_move_y, Wall.walls) player.update_projectile() for wall in Wall.walls: pygame.draw.rect(screen, BLACK, wall.rect) pygame.display.update()