#Catapult 스프라이트와 그룹을 생성. catapult = Catapult(stone) catapult.rect.x = 50 # 위치 변경 catapult.rect.y = BASE_Y catapult_group = pygame.sprite.Group() catapult_group.add(catapult) #Alien 스프라이트와 그룹을 생성. alien = Alien() alien.rect.x = 350 alien.rect.y = BASE_Y alien_group = pygame.sprite.Group() alien_group.add(alien) #Explosion 스프라이트와 그룹을 생성. explosion = Explosion() explosion_group = pygame.sprite.Group() explosion_group.add(explosion) # 게임 루프 while run: # 1) 사용자 입력 처리 for event in pygame.event.get(): if event.type == pygame.QUIT: run = False elif event.type == pygame.KEYUP: if event.key == pygame.K_SPACE: # 초기화면에서 스페이스를 입력하면 시작 if game_state == GAME_INIT: game_state = GAME_PLAY elif game_state == GAME_PLAY:
def explode(self): self.world.gameobjects += [Explosion(self.pos, self.owner, 50, 8)] self.despawn()
def main(winstyle=0): # Initialize pygame capture = Capture('0') pygame.init() if pygame.mixer and not pygame.mixer.get_init(): print('Warning, no sound') pygame.mixer = None # Set the display mode winstyle = 0 # |FULLSCREEN bestdepth = pygame.display.mode_ok(SCREENRECT.size, winstyle, 32) screen = pygame.display.set_mode(SCREENRECT.size, winstyle, bestdepth) #Load images, assign to sprite classes #(do this before the classes are used, after screen setup) img = load_image('tank.gif') Player.images = [img, pygame.transform.flip(img, 1, 0)] img = load_image('explosion1.gif') Explosion.images = [img, pygame.transform.flip(img, 1, 1)] Alien.images = load_images('alien1.gif', 'alien2.gif', 'alien3.gif') Bomb.images = [load_image('bomb.gif')] Shot.images = [load_image('shot.gif')] #decorate the game window icon = pygame.transform.scale(Alien.images[0], (32, 32)) pygame.display.set_icon(icon) pygame.display.set_caption('Pygame Aliens') pygame.mouse.set_visible(0) #create the background, tile the bgd image bgdtile = load_image('background.gif') background = pygame.Surface(SCREENRECT.size) for x in range(0, SCREENRECT.width, bgdtile.get_width()): background.blit(bgdtile, (x, 0)) screen.blit(background, (0, 0)) pygame.display.flip() #load the sound effects boom_sound = load_sound('boom.wav') shoot_sound = load_sound('car_door.wav') if pygame.mixer: music = os.path.join(main_dir, 'data', 'house_lo.wav') pygame.mixer.music.load(music) pygame.mixer.music.play(-1) # Initialize Game Groups aliens = pygame.sprite.Group() shots = pygame.sprite.Group() bombs = pygame.sprite.Group() all = pygame.sprite.RenderUpdates() lastalien = pygame.sprite.GroupSingle() #assign default groups to each sprite class Player.containers = all Alien.containers = aliens, all, lastalien Shot.containers = shots, all Bomb.containers = bombs, all Explosion.containers = all Score.containers = all #Create Some Starting Values global score alienreload = ALIEN_RELOAD kills = 0 clock = pygame.time.Clock() #initialize our starting sprites global SCORE player = Player() Alien() #note, this 'lives' because it goes into a sprite group if pygame.font: all.add(Score()) current_pos = [0, 0] while player.alive(): capture.run() #get input if (len(capture.trackers) > 0): old_pos = current_pos current_pos = capture.trackers[0].pos for event in pygame.event.get(): if event.type == QUIT or \ (event.type == KEYDOWN and event.key == K_ESCAPE): return keystate = pygame.key.get_pressed() if (old_pos[0] > current_pos[0]): direction = 1 elif (old_pos[0] < current_pos[0]): direction = -1 else: direction = 0 # clear/erase the last drawn sprites all.clear(screen, background) #update all the sprites all.update() #handle player input #direction = keystate[K_RIGHT] - keystate[K_LEFT] player.move(direction) firing = keystate[K_SPACE] if not player.reloading and firing and len(shots) < MAX_SHOTS: Shot(player.gunpos()) shoot_sound.play() player.reloading = firing # Drop bombs if lastalien and not int(random.random() * BOMB_ODDS): Bomb(lastalien.sprite) # Detect collisions for alien in pygame.sprite.spritecollide(player, aliens, 1): boom_sound.play() Explosion(alien) Explosion(player) SCORE = SCORE + 1 player.kill() for alien in pygame.sprite.groupcollide(shots, aliens, 1, 1).keys(): boom_sound.play() Explosion(alien) SCORE = SCORE + 1 for bomb in pygame.sprite.spritecollide(player, bombs, 1): boom_sound.play() Explosion(player) Explosion(bomb) player.kill() #draw the scene dirty = all.draw(screen) pygame.display.update(dirty) #cap the framerate clock.tick(40) if pygame.mixer: pygame.mixer.music.fadeout(1000) pygame.time.wait(1000) pygame.quit() cv2.destroyWindow('frame')
def main_game(): crashed = False global tank_health arrows_down = 0 wasd_down = 0 bullets = list() boxes = list() explosions = list() for i in range(1, display_height - boxheight, 180): for j in range(1, display_width - boxheight, 180): if (random.randint(1, 3) % 2 != 0): boxes.append( Box(j, i, 'box.png', display_width, display_height)) p2 = Panzar(display_width * 0.15, display_height * 0.5, tank_health, 1, 'tank.png', display_width, display_height) p1 = Panzar(display_width * 0.8, display_height * 0.5, tank_health, 3, 'tank.png', display_width, display_height) while not crashed: for event in pygame.event.get(): if event.type == pygame.QUIT: crashed = True if event.type == pygame.KEYDOWN: if isArrow(event.key): p1.speed = 5 arrows_down += 1 if event.key == pygame.K_UP: p1.direction = 0 elif event.key == pygame.K_RIGHT: p1.direction = 1 elif event.key == pygame.K_DOWN: p1.direction = 2 elif event.key == pygame.K_LEFT: p1.direction = 3 if (isWASD(event.key)): p2.speed = 5 wasd_down += 1 if event.key == pygame.K_w: p2.direction = 0 elif event.key == pygame.K_d: p2.direction = 1 elif event.key == pygame.K_s: p2.direction = 2 elif event.key == pygame.K_a: p2.direction = 3 if event.key == pygame.K_t and not p2.is_reloading(): p2.fire() bullets.append( Bullet(p2.get_muzzle_coordes()[0], p2.get_muzzle_coordes()[1], 'bullet.png', display_width, display_height, p2.direction)) bullets[len(bullets) - 1].move() if event.key == pygame.K_m and not p1.is_reloading(): p1.fire() bullets.append( Bullet(p1.get_muzzle_coordes()[0], p1.get_muzzle_coordes()[1], 'bullet.png', display_width, display_height, p1.direction)) bullets[len(bullets) - 1].move() if event.key == pygame.K_ESCAPE: crashed = True if event.type == pygame.KEYUP: if isArrow(event.key): if (arrows_down == 1): p1.speed = 0 arrows_down -= 1 if (isWASD(event.key)): if (wasd_down == 1): p2.speed = 0 wasd_down -= 1 gameDisplay.fill(white) for i in boxes: paint(i, i.x, i.y) p1.move() p2.move() if (p1.collides_with(p2)): p2.move_back() p1.move_back() for i in boxes: if p1.collides_with(i) or i.collides_with(p1): p1.move_back() if (p2.collides_with(i) or i.collides_with(p2)): p2.move_back() paint(p1, p1.x, p1.y) paint(p2, p2.x, p2.y) for i in bullets: if (i.out_of_bounds()): bullets.remove(i) else: paint(i, i.x, i.y) i.move() #collision between a bullet and a box for j in boxes: if i.collides_with(j) or j.collides_with(i): explosions.append(Explosion(i.x, i.y, 'explose.png')) bullets.remove(i) j.health -= 1 if (j.health == 0): boxes.remove(j) #collision with a bullet and a tank if i.collides_with(p1) or p1.collides_with(i): p1.health -= 1 explosions.append(Explosion(i.x, i.y, 'explose.png')) bullets.remove(i) if (p1.health == 0): restart(2) if i.collides_with(p2) or p2.collides_with(i): p2.health -= 1 explosions.append(Explosion(i.x, i.y, 'explose.png')) bullets.remove(i) if (p2.health == 0): restart(1) for i in explosions: if (i.is_going()): paint(i, i.x, i.y) else: explosions.remove(i) CountText, CTRect = text_objects( str(p2.health) + " : " + str(p1.health), pygame.font.Font('freesansbold.ttf', 30)) CTRect.center = ((display_width / 2), (display_height * 0.5)) gameDisplay.blit(CountText, CTRect) pygame.display.update() clock.tick(60)
def kill_player(self): if len(self.lives): life = self.lives.pop() life.kill() explosion = Explosion(self.player.rect.center) self.player_explosion_sprite.add(explosion)
def create_enemy_explosion(self, sprite_dict): for sprite in sprite_dict: if sprite_dict[sprite]: explosion = Explosion(sprite.rect.center) self.all_sprites.add(explosion) self.num_of_enemies -= 1
def explode(self): self.all_sprites.remove(self.player) self.player.dead = True explosion = Explosion(self.player.rect[0] - WHEEL_SIZE / 2, self.player.rect[1], self.take_damage) self.all_sprites.add(explosion)