def createfireball(self): # Creating fireballs donkeyx, donkeyy = self.donkey.getPosition() self.fireballs += [ fireball.Fireball("Images/fireball.png", "Images/fireball.png", (donkeyx + 5, 80), self.FIREBALL_WIDTH, self.FIREBALL_HEIGHT, randint(1, 2)) ] self.fireball_group = pygame.sprite.RenderPlain(*self.fireballs)
def shoot(self, targetX, targetY): if self.cool_down_counter == 0: vectorX = targetX - self.x vectorY = targetY - self.y ball = fireball.Fireball(self.x, self.y, vectorX, vectorY, self.fireball_img) self.fireballs.append(ball) self.cool_down_counter = 1
def update(self): """ Updates Donkey's motion and determines whether to emit fireball or not """ self.__loop_count += 1 if self.__loop_count >= 100: fireball.Fireball.all_fireballs.add( fireball.Fireball(self.rect.left, self.rect.bottom)) self.__loop_count = 0 self.__random_movement() super(Donkey, self).update()
def test_fireball_collision(define_sprites): #Set players initial position define_sprites.rect.left = 100; define_sprites.rect.bottom = 0; #Set a fireball at players position fireball.Fireball.all_fireballs.add(fireball.Fireball(define_sprites.rect.left,define_sprites.rect.bottom)) #Check collision define_sprites.check_fireball() assert len(fireball.Fireball.all_fireballs) == 0 assert define_sprites.life == constants.PLAYER_LIFE - 1 assert define_sprites.rect.left == 0 assert define_sprites.rect.bottom == constants.SCREEN_HEIGHT
def updatefireballs(self): # Update fireball positions and directions i = 0 for s in self.fireball_group.sprites(): x, y = s.getPosition() if x <= 0 and y == self.PLAYER_SPAWN_LEVEL: pass else: state = s.getState() if x <= 0: state = 1 if x >= 1180: state = 2 if state != 3: if state == 1: x += self.FIREBALL_SPEED else: x -= self.FIREBALL_SPEED collisions = pygame.sprite.spritecollide( s, self.ladder_group, False) if collisions: ly = self.ladderlimits[collisions[0].rect.topleft] ladderx, laddery = collisions[0].rect.topleft if y != ly: val = randint(1, 10) if val == 5: y += 2 * self.FIREBALL_SPEED x = ladderx state = 3 if y == 80 and x > 700: y += 2 * self.FIREBALL_SPEED state = 3 if y in self.levellimits and int( self.levellimits[y]) == 1 and x > 1000: y += 2 * self.FIREBALL_SPEED state = 3 if y in self.levellimits and int( self.levellimits[y]) == 2 and x < 170: y += 2 * self.FIREBALL_SPEED state = 3 else: y = min(self.fireballparentdict[y], y + 2 * self.FIREBALL_SPEED) if self.fireballparentdict[y] == y: state = randint(0, 1) self.fireballs[i] = fireball.Fireball( "Images/fireball.png", "Images/fireball.png", (x, y), self.FIREBALL_WIDTH, self.FIREBALL_HEIGHT, state) i += 1 del self.fireballs[i:] self.fireball_group = pygame.sprite.RenderPlain(*self.fireballs)
enemies = pygame.sprite.Group() fireballs = pygame.sprite.Group() powerups = pygame.sprite.Group() player = dragon.Dragon() all_sprites.add(player) for i in range(5): new_enemy() score = 0 clock.tick(fps) keystate = pygame.key.get_pressed() current_time = pygame.time.get_ticks() if keystate[pygame.K_SPACE]: if current_time - previous_time > 500: previous_time = current_time if player.power == 1: fire = fireball.Fireball() fire.rect.left = player.rect.right fire.rect.centery = player.rect.centery all_sprites.add(fire) fireballs.add(fire) if player.power >= 2: fire1 = fireball.Fireball() fire2 = fireball.Fireball() fire1.rect.left = player.rect.right fire2.rect.left = player.rect.right fire1.rect.centery = player.rect.centery - 18 fire2.rect.centery = player.rect.centery + 18 all_sprites.add(fire1) all_sprites.add(fire2) fireballs.add(fire1) fireballs.add(fire2)
def mainGame(): global pause #global allows me to swith between pause and unpause state #sets up the players player = serge.Serge((100, 200)) player2 = badGuy.BadGuy((300, 200)) player_health = player.health player2_health = player2.health myfont = pygame.font.SysFont("monospace", 20) label1 = myfont.render("Player 1", 1, (0, 0, 0)) label2 = myfont.render("Player 2", 1, (0, 0, 0)) mugShot = pygame.image.load( '/Users/Aditya/Desktop/CMU 16-17 /112/tp/mug.png') background = pygame.image.load( "/Users/Aditya/Desktop/CMU 16-17 /112/tp/Bckgrnd0.png") background = pygame.transform.scale(background, (width, height)) direction = '' game_over = False cooldown = 300 lastMoveP1 = pygame.time.get_ticks() lastMoveP2 = pygame.time.get_ticks() times = clock.tick(10) flag = 'twoPlayer' countp1 = 0 countp2 = 0 blastBarP1 = player.blastBar blastBarP2 = player2.blastBar while game_over == False: for event in pygame.event.get(): # check if the event is the X button if event.type == pygame.QUIT: # if it is quit the game pygame.quit() exit(0) #checks for key press if event.type == pygame.KEYDOWN: if event.key == 275: direction = 'left' elif event.key == 276: direction = 'right' elif event.key == 274: direction = 'up' elif event.key == 273: direction = 'down' elif event.key == 97: direction = 'a' elif event.key == 100: direction = 'd' elif event.key == 119: direction = 'w' elif event.key == 115: direction = 's' elif event.key == 27: pause = True paused() elif event.key == 47: direction = 'blast' elif event.key == 115: direction = 'p2blast' #checks for keys that arent pressed if event.type == pygame.KEYUP: if event.key == 275: direction = 'Notleft' elif event.key == 276: direction = 'Notright' elif event.key == 274: direction = 'Notup' elif event.key == 273: direction = 'Notdown' elif event.key == 97: direction = 'Nota' elif event.key == 100: direction = 'Notd' elif event.key == 119: direction = 'Notw' elif event.key == 115: direction = 'Nots' elif event.key == 47: direction = 'Notblast' elif event.key == 115: direction = 'Notp2blast' #intialize fire ball if countp1 == 0: blastPos = [player.rect.x + 150, player.rect.y + 180] if countp2 == 0: blastPosP2 = [player2.rect.x + 150, player2.rect.y + 180] player.handle_event(event) player2.handle_event(event) screen.fill(0) screen.blit(background, (0, 0)) screen.blit(player.image, player.rect) screen.blit(player2.image, player2.rect) screen.blit(mugShot, (50, 50)) screen.blit(mugShot, (500, 50)) screen.blit(label1, (50, 30)) screen.blit(label2, (500, 30)) health_bars(player_health, player2_health) blastBar(player_health, player2_health, blastBarP1, blastBarP2) #adds to blast bar based on health of ooponent and time if blastBarP1 <= 100: blastBarP1 += .5 if blastBarP2 <= 100: blastBarP2 += .5 if player_health == 75: if blastBarP2 + 10 > 100: blastBarP2 = 100 else: blastBarP2 += 10 if player_health == 50: if blastBarP2 + 20 > 100: blastBarP2 = 100 else: blastBarP2 += 20 if player2_health == 75: if blastBarP1 + 10 > 100: blastBarP1 = 100 else: blastBarP1 += 10 if player2_health == 50: if blastBarP1 + 20 > 100: blastBarP1 = 100 else: blastBarP1 += 20 #moves fire ball and checks for collision if direction == 'blast': if blastBarP1 > 100: countp1 += 1 specialAttack = fireball.Fireball(blastPos) screen.blit(specialAttack.blast, blastPos) if countp1 == 1: time.sleep(1) blastPos[0] += 10 if specialAttack.blastRect.x == player2.rect.x + 100: if player2_health == 0: player2_health -= 0 player2_health -= 20 blastBarP1 = 0 del specialAttack if player2_health == 0: endScreen('Player 1', 'Player 2', flag) if direction == 'p2blast': countp2 += 1 specialAttack = fireball.Fireball(blastPosP2) screen.blit(specialAttack.blast, blastPosP2) if count == 1: time.sleep(1) blastPos[0] -= 10 if specialAttack.blastRect.x == player1.rect.x + 100: if player2_health == 0: player2_health -= 0 player2_health -= 20 del specialAttack if player2_health == 0: endScreen('Player 1', 'Player 2', flag) #Checks for puch and kick collisions if pygame.sprite.collide_rect(player, player2): player2.collision = True now1 = pygame.time.get_ticks() if ((direction == 'up' or direction == 'down')): if (now1 - lastMoveP1) >= cooldown: lastMoveP1 = now1 if player2_health == 0: player2_health -= 0 player2_health -= 5 if player2_health == 0: endScreen('Player 1', 'Player 2', flag) if pygame.sprite.collide_rect(player2, player): player2.collision = True now2 = pygame.time.get_ticks() if (direction == 'w' or direction == 's'): if (now2 - lastMoveP2) >= cooldown: lastMoveP2 = now2 if player_health == 0: player_health -= 0 player_health -= 5 if player_health == 0: endScreen('Player 2', 'Player 1', flag) player2.collision = False clock.tick(10) pygame.display.flip()