def shoot(self,player): '''Shoots in the direction of the player based on its own position. Each time it shoots, each projectile is also checked to see if it is on screen Off screen projectiles are removed.''' if self.shot_timer > 100: dir = 1 if player.rect.x > self.rect.x else 0 self.projectiles.append(Projectile.Bullet(self.rect.x,self.rect.y+12,dir,"projectile1.png")) self.shot_timer = 0 self.projectiles = [i for i in self.projectiles if 0 < i.rect.x < 1000]
def get_projectiles(self, time, x, y, w, h, dir, img): '''Creates a new projectile object using the players data Returns an empty array if the delay is not fulfilled Else returns a projectile for each position it makes''' self.get_positions(h) if time < self.projectile_delay or self.positions == []: return [] projectiles = [] for yoff, yvel in self.positions: bullet = Projectile.Bullet(x + w, y + yoff, dir, img, yvel) projectiles.append(bullet) return projectiles
def checkpress(self, screenwidth): keys = pygame.key.get_pressed() if keys[pygame.K_SPACE]: if len(self.projectiles) < 5 and ( self.walkingLeft or self.walkingRight) and self.canfire(): self.projectiles.append( Projectile.Bullet(round(self.x + self.width // 2), round(self.y + self.height // 2), 6, (0, 0, 0), self.getdirection())) if keys[pygame.K_LEFT] and self.x >= self.velocity: self.x -= self.velocity self.walkingLeft = True self.walkingRight = False elif keys[ pygame. K_RIGHT] and self.x <= screenwidth - self.width - self.velocity: self.x += self.velocity self.walkingLeft = False self.walkingRight = True else: self.walkCount = 0 if not self.isJump: if keys[pygame.K_UP]: self.isJump = True self.walkCount = 0 else: if self.jumpCount >= -10: neg = 1 if self.jumpCount < 0: neg = -1 self.y -= (self.jumpCount**2) * 0.5 * neg self.jumpCount -= 1 else: self.isJump = False self.jumpCount = 10
p2X, p2Y = 400, 400 p2HitBox = player2.get_rect() p2Bullets = [] p2Dir = "right" p2Health = 100 size = 20 while not done: # check for keypress for event in pygame.event.get(): if event.type == pygame.QUIT: done = True if gameState == "playing": if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: p1Bullets.append( Projectile.Bullet(p1X + 2.5, p1Y + 2.50, p1Dir, 5)) if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN: p2Bullets.append( Projectile.Bullet(p2X + 2.5, p2Y + 2.50, p2Dir, 5)) if gameState == "GameOver": if event.type == pygame.KEYDOWN and event.key == pygame.K_r: p1X, p1Y = 100, 100 p1Bullets = [] p1Dir = "" p1Health = 100 p2X, p2Y = 400, 400 p2Bullets = [] p2Dir = "" p2Health = 100 gameState = "playing"
player2 = pygame.image.load("smallCharacter.png") player2x = 400 player2y = 400 player2HitBox = player2.get_rect() p2Bullets = [] player2dir = "right" p2Health = 100 while not done: #REQUIRED by pygame: check for single keypress for event in pygame.event.get(): if event.type == pygame.QUIT: done = True if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: p1Bullets.append( Projectile.Bullet(player1x, player1y, 5, (255, 0, 0), player1dir)) if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN: p2Bullets.append( Projectile.Bullet(player2x, player2y, 5, (0, 255, 0), player2dir)) #checking for continuous key presses pressed = pygame.key.get_pressed() if pressed[pygame.K_w]: player1y -= .3 player1dir = "up" if pressed[pygame.K_s]: player1y += .3 player1dir = "down" if pressed[pygame.K_d]: player1x += .3
player2 = pygame.image.load("smallCharacter.png") p2x = 400 p2y = 400 p2HitBox = player2.get_rect() p2Bullets = [] p2Dir = "right" p2Health = 100 while not done: # required by pygame, check for keypresses ONE time for event in pygame.event.get(): if event.type == pygame.QUIT: done = True if gameState == "playing": if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: p1Bullets.append(Projectile.Bullet(p1x,p1y,p1Dir,5)) if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN: p2Bullets.append(Projectile.Bullet(p2x,p2y,p2Dir,5)) if gameState == "game over": if event.type == pygame.KEYDOWN and event.key == pygame.K_r: p1Bullets = [] p2Bullets = [] p1Health = 100 p2Health = 100 p1x = 100 p1y = 100 p2x = 400 p2y = 400 gameState = "playing"