def __init__(self, name, lifes, image, collisionBoxImage, playerBulletImage, playerSpeed, playerDamage, putBulletPattern, shootBulletPattern): pygame.sprite.Sprite.__init__(self) self.name = name self.lifes = lifes self.image_origin = image self.image = self.image_origin.copy() self.collisionBoxImage = collisionBoxImage self.playerBulletImage = playerBulletImage self.playerDamage = playerDamage self.playerFastSpeed = playerSpeed[0] self.playerSlowSpeed = playerSpeed[1] self.putBulletPattern = putBulletPattern self.shootBulletPattern = shootBulletPattern self.rect = self.image.get_rect( center=(parameter.getGamearea().centerx, parameter.getGamearea().bottom - 50)) self.collisionBox = playerCollisionBox( collisionBoxImage=self.collisionBoxImage, playerRect=self.rect) self.radius = int(self.collisionBox.rect.width / 2) self.lastShootingTime = parameter.getTimer() self.power = 0 self.hidden = False self.hiddenTime = parameter.getTimer()
def update(self): now = parameter.getTimer() self.rect.move_ip(self.shootBulletPattern(now - self.generateTime)) if not self.rect.colliderect(parameter.getGamearea()): self.kill()
def __init__(self, collisionBoxImage, playerRect): pygame.sprite.Sprite.__init__(self) self.image_origin = collisionBoxImage self.image = self.image_origin.copy() self.rect = self.image.get_rect(center=playerRect.center) self.axis = 0 self.rotateSpeed = 5 self.lastUpdate = parameter.getTimer()
def update(self): now = parameter.getTimer() self.rotate() self.rect.center = (self.dx * (now - self.generateTime) + self.generateCenter[0], \ self.dy * (now - self.generateTime) + self.generateCenter[1]) if not self.rect.colliderect(parameter.getGamearea()): self.kill()
def rotate(self): now = parameter.getTimer() if now - self.lastUpdate > 100: self.lastUpdate = now self.axis = (self.axis + self.rotateSpeed) % 360 newImage = pygame.transform.rotate(self.image_origin, self.axis) oldCenter = self.rect.center self.image = newImage self.rect = self.image.get_rect(center=oldCenter)
def update(self): now = parameter.getTimer() # Moving enemy self.rect.move_ip(self.movePattern(now - self.generateTime)) # Kill enemy if out of screen if not self.rect.colliderect(parameter.getGamearea()): self.kill() # Shoot bullet self.shoot()
def update(self): now = parameter.getTimer() self.rotate(now) # Placing bullet on the orbit that caculated by functions self.rect.center = (self.shootBulletPattern["f(x)"](now - self.generateTime)[0] + self.generateCenter[0], \ self.shootBulletPattern["f(x)"](now - self.generateTime)[1] + self.generateCenter[1]) if not self.rect.colliderect(parameter.getBulletGamearea()): self.kill()
def __init__(self, name, image, generatePosition): pygame.sprite.Sprite.__init__(self) self.name = name self.image = image self.generatePosition = generatePosition self.rect = self.image.get_rect(center=self.generatePosition) self.generateTime = parameter.getTimer() self.radius = self.rect.width * 3
def shoot(self): now = parameter.getTimer() if now - self.lastShootingTime > 6: sound.bullet_SE_list[0].play() # powerups if self.power in range(0, 8): # line 1 way self.newPlayerBullet( putBulletPattern=function.raletivePosition( self.putBulletPattern[0](now, self.power), self.rect.center)) self.lastShootingTime = now if self.power in range(8, 24): # line 1 way self.newPlayerBullet( putBulletPattern=function.raletivePosition( self.putBulletPattern[0](now, self.power), self.rect.center)) # tracking 2 way for i in range(len(self.putBulletPattern[1](now, self.power))): self.newPlayerBullet_tracking(putBulletPattern = function.raletivePosition(self.putBulletPattern[1](now, self.power)[i], self.rect.center), \ shootBulletPattern = self.shootBulletPattern[1](now)[i]) self.lastShootingTime = now if self.power in range(24, 48): # line 3 way for i in range(len(self.putBulletPattern[0](now, self.power))): self.newPlayerBullet( putBulletPattern=function.raletivePosition( self.putBulletPattern[0](now, self.power)[i], self.rect.center)) # tracking 2 way for i in range(len(self.putBulletPattern[1](now, self.power))): self.newPlayerBullet_tracking(putBulletPattern = function.raletivePosition(self.putBulletPattern[1](now, self.power)[i], self.rect.center), \ shootBulletPattern = self.shootBulletPattern[1](now)[i]) self.lastShootingTime = now if self.power == 48: # line 3 way for i in range(len(self.putBulletPattern[0](now, self.power))): self.newPlayerBullet( putBulletPattern=function.raletivePosition( self.putBulletPattern[0](now, self.power)[i], self.rect.center)) # tracking 4 way for i in range(len(self.putBulletPattern[1](now, self.power))): self.newPlayerBullet_tracking(putBulletPattern = function.raletivePosition(self.putBulletPattern[1](now, self.power)[i], self.rect.center), \ shootBulletPattern = self.shootBulletPattern[1](now)[i]) self.lastShootingTime = now
def shoot(self): now = parameter.getTimer() for j in range(len(self.enemyBulletImage)): if now - self.generateTime > self.putBulletPattern[j]( now - self.generateTime)["delateTime"]: if now - self.lastShootingTime[j] > self.putBulletPattern[j]( now - self.generateTime)["intermediateTime"]: sound.bullet_SE_list[2].play() for i in range(self.putBulletPattern[j](now)["numbers"]): x = self.putBulletPattern[j](now)["position"][i][0] y = self.putBulletPattern[j](now)["position"][i][1] if "tracking" in self.putBulletPattern[j](now).keys(): enemyBullet = EnemyBullet_tracking(name = "enemyBullet", \ image = self.enemyBulletImage[j], \ bulletRadius = 1, \ bulletDamage = None, \ putBulletPattern = (x + self.rect.center[0], \ y + self.rect.center[1]), \ shootBulletPattern = self.shootBulletPattern[j]((x, y))) parameter.getAllSprites().add(enemyBullet) parameter.getEnemyBulletSprites().add(enemyBullet) elif "deathGenerate" in self.putBulletPattern[j]( now).keys(): enemyBullet = EnemyBullet_deathGenerate(name = "enemyBullet", \ image = self.enemyBulletImage[j], \ bulletRadius = 1, \ bulletDamage = None, \ putBulletPattern = (x + self.rect.center[0], \ y + self.rect.center[1]), \ shootBulletPattern = self.shootBulletPattern[j]((x, y)), \ death = self.putBulletPattern[j](now)["generateBullet"]) parameter.getAllSprites().add(enemyBullet) parameter.getEnemyBulletSprites().add(enemyBullet) elif "decay" in self.putBulletPattern[j](now).keys(): enemyBullet = EnemyBullet_decay(name = "enemyBullet", \ image = self.enemyBulletImage[j], \ bulletRadius = 1, \ bulletDamage = None, \ putBulletPattern = (x + self.rect.center[0], \ y + self.rect.center[1]), \ shootBulletPattern = self.shootBulletPattern[j]((x, y))) parameter.getAllSprites().add(enemyBullet) parameter.getEnemyBulletSprites().add(enemyBullet) else: enemyBullet = EnemyBullet(name = "enemyBullet", \ image = self.enemyBulletImage[j], \ bulletRadius = 1, \ bulletDamage = None, \ putBulletPattern = (x + self.rect.center[0], \ y + self.rect.center[1]), \ shootBulletPattern = self.shootBulletPattern[j]((x, y))) parameter.getAllSprites().add(enemyBullet) parameter.getEnemyBulletSprites().add(enemyBullet) self.lastShootingTime[j] = now
def __init__(self, name, Hp, image, movePattern, enemyBulletImage, putBulletPattern, shootBulletPattern, dropItem): pygame.sprite.Sprite.__init__(self) self.name = name self.Hp = Hp self.image = image self.movePattern = movePattern self.enemyBulletImage = enemyBulletImage self.putBulletPattern = putBulletPattern self.shootBulletPattern = shootBulletPattern self.dropItem = dropItem self.rect = self.image.get_rect(center = (movePattern(-1)[0] + parameter.getGamearea().left, \ movePattern(-1)[1] + parameter.getGamearea().top)) self.radius = int((self.rect.width / 2) * 0.5) self.generateTime = parameter.getTimer() self.lastShootingTime = [ parameter.getTimer() for i in range(len(self.enemyBulletImage)) ]
def hide(self): # Hide player for eb in parameter.getEnemyBulletSprites(): eb.kill() self.hidden = True self.hiddenTime = parameter.getTimer() self.rect.center = (parameter.getGamearea().centerx, parameter.getGamearea().top - 200) # Decrease player power if self.power > 4: self.power -= 4 else: self.power = 0
def __init__(self, name, image, bulletRadius, bulletDamage, putBulletPattern, shootBulletPattern): pygame.sprite.Sprite.__init__(self) self.name = name self.image_origin = image self.image = self.image_origin.copy() self.radius = bulletRadius self.damage = bulletDamage self.shootBulletPattern = shootBulletPattern self.generateCenter = putBulletPattern self.rect = self.image.get_rect(center=self.generateCenter) self.generateTime = parameter.getTimer()
def update(self): now = parameter.getTimer() time = now - self.generateTime def itemMovement(time): v0 = -3 g = 0.1 v = v0 + g * time if v < 3: oldCenterX = self.rect.center[0] self.rect.center = (oldCenterX, v0 * time + 0.5 * g * (time**2) + self.generatePosition[1]) else: self.rect.move_ip(0, 3) itemMovement(time) if self.rect.top > parameter.getGamearea().bottom: self.kill()
def __init__(self, name, image, bulletRadius, bulletDamage, putBulletPattern, shootBulletPattern, death): pygame.sprite.Sprite.__init__(self) self.name = name self.image_origin = image[0] self.image = self.image_origin.copy() self.damage = bulletDamage self.shootBulletPattern = shootBulletPattern self.generateCenter = putBulletPattern self.rect = self.image.get_rect(center=self.generateCenter) self.radius = int(self.rect.width / 2 * 0.75) self.generateTime = parameter.getTimer() self.deathGenerateImage = image[1] self.deathGenerate = death self.generateBulletNumber = self.deathGenerate["generateBulletNumber"]
def update(self): self.rotate(self.generateTime) now = parameter.getTimer() # Becoming non-transparent by time if now - self.generateTime < 128: self.image.set_alpha((now - self.generateTime) * 2) self.radius = 0 # Can collide detect with player as being opaque else: self.image.set_alpha(255) self.radius = self.radius_origin if now - self.generateTime > 300: self.px += self.dx self.py += self.dy self.rect.center = (self.px, self.py) if not self.rect.colliderect(parameter.getGamearea()): self.kill()
def update(self): now = parameter.getTimer() # Find the nearest enemy try: enemyCenter = function.findMostCloseEnemy(self.playerRectCenter) self.dx += (enemyCenter[0] - self.rect.center[0]) / 5000 * ( now - self.generateTime)**2 self.dy += (enemyCenter[1] - self.rect.center[1]) / 5000 * ( now - self.generateTime)**2 except: pass self.rotate() self.rect.move_ip(function.returnTheComponentOfVectorX(self.dx, self.dy, self.speed), \ function.returnTheComponentOfVectorY(self.dx, self.dy, self.speed)) if not self.rect.colliderect(parameter.getGamearea()): self.kill()
def update(self): now = parameter.getTimer() # Hide player for a while when player died if self.hidden and now - self.hiddenTime < 30: for eb in parameter.getEnemyBulletSprites(): eb.kill() elif self.hidden and now - self.hiddenTime == 30: self.radius = 0 self.rect.center = (parameter.getGamearea().centerx, parameter.getGamearea().bottom - 50) elif self.hidden and now - self.hiddenTime < 120: self.radius = 0 if ((now - self.hiddenTime) % 6) in [0, 1, 2]: transparentImg = self.image_origin.copy() transparentImg.set_alpha(64) self.image = transparentImg else: self.image = self.image_origin else: self.image = self.image_origin self.radius = int(self.collisionBox.rect.width / 2) self.hidden = False # Key control keystate = pygame.key.get_pressed() if keystate[pygame.K_LSHIFT]: speed = self.playerSlowSpeed else: speed = self.playerFastSpeed if keystate[pygame. K_LEFT] and self.rect.left > parameter.getGamearea().left: self.rect.move_ip(-1 * speed, 0) if self.rect.left < parameter.getGamearea().left: self.rect.left = parameter.getGamearea().left if keystate[ pygame. K_RIGHT] and self.rect.right < parameter.getGamearea().right: self.rect.move_ip(speed, 0) if self.rect.right > parameter.getGamearea().right: self.rect.right = parameter.getGamearea().right if keystate[ pygame.K_UP] and self.rect.top > parameter.getGamearea().top: self.rect.move_ip(0, -1 * speed) if self.rect.top < parameter.getGamearea().top: self.rect.top = parameter.getGamearea().top if keystate[ pygame. K_DOWN] and self.rect.bottom < parameter.getGamearea().bottom: self.rect.move_ip(0, speed) if self.rect.bottom > parameter.getGamearea().bottom: self.rect.bottom = parameter.getGamearea().bottom if keystate[pygame.K_z]: self.shoot() # Stick player collision box to the player center self.collisionBox.rect.center = self.rect.center # self.collisionBox.rotate() # Return player position parameter.returnPlayerPosition(self.rect.center)
# Play Music pygame.mixer.music.load(bgmList[1]) pygame.mixer.music.set_volume(0.4) pygame.mixer.music.play(loops=1) # Game loop while running: for event in pygame.event.get(): if event.type == QUIT: terminate() if event.type == KEYUP: if event.key == K_ESCAPE: running = False # Generate Enemy if parameter.getTimer() >= 500 and parameter.getTimer() <= 2000: if (parameter.getTimer() - 500) % 500 == 0: newEnemy_1() if parameter.getTimer() >= 2600 and parameter.getTimer() <= 3600: if (parameter.getTimer() - 2600) % 50 == 0: newEnemy_2() newEnemy_3() if parameter.getTimer() == 3900: newEnemy_4() if parameter.getTimer() >= 5000 and stageInitiated: pygame.mixer.music.stop() pygame.mixer.music.load(bgmList[2]) pygame.mixer.music.set_volume(0.4)