Reputation: 23
I am trying to add multiple enemies to my game, but even though I have put the code to do so in, no more than one enemy is showing. I have tried to debug to find any errors, but I can't find any.
import random
import math
import pygame
# initialise pygame
pygame.init()
# player 1
playerimage = pygame.image.load("Main Player.png")
playerX = 365
playerY = 700
playerX_change = 0
# laser
# ready - bullet not on screen
# fire - bullet is shown on screen and is moving
laserimage = pygame.image.load("laser.png")
laserX = 0
laserY = 700
laserY_change = 10
laser_currentstate = "ready"
# alien player / random movement = random.randint()
alienimage = []
alienX = []
alienY = []
alienX_change = []
alienY_change = []
amount_aliens = 3
I've attempted to make multiple aliens, but the code doesn't work either.
for i in range(amount_aliens):
alienimage.append(pygame.image.load('alien.png'))
alienX.append(random.randint(0, 735))
alienY.append(random.randint(50, 200))
alienX_change.append(4)
alienY_change.append(7)
score = 0
# define player
def main_player(x, y):
screen.blit(playerimage, (x, y))
# define laster
def fire_laser(x, y):
global laser_currentstate
laser_currentstate = "fire"
screen.blit(laserimage, (x + 16, y + 10))
# define alien
def alien(x, y, i):
screen.blit(alienimage[i], (x, y))
# collision detection
def hascollision(alienX, alienY, laserX, laserY):
distance = math.sqrt((math.pow(alienX - laserX, 2)) + (math.pow(alienY - laserY, 2)))
if distance < 27:
return True
else:
return False
# background
background = pygame.image.load('stars.png')
# display and screen title/icon
(width, height) = (800, 800)
screen = pygame.display.set_mode((width, height))
flip = pygame.display.flip()
pygame.display.set_caption("space fighters")
pygame.event.get()
icon = pygame.image.load('logo.png')
pygame.display.set_icon(icon)
from sys import exit
# loop of functions
executed = True
while executed:
screen.fill((63, 62, 63))
# image background
screen.blit(background, (0, 0))
for event in pygame.event.get():
# if key pressed, check which input, right or left?
if event.type == pygame.KEYDOWN:
print("key pressed")
if event.key == pygame.K_a:
playerX_change = -6
if event.key == pygame.K_s:
playerX_change = 6
if event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_s:
playerX_change = 0
if event.key == pygame.K_SPACE:
if laser_currentstate is "ready":
laserX = playerX
fire_laser(laserX, laserY)
# bounrary algorithm, prevents player moving out/enemy.
playerX += playerX_change
if playerX <= 0:
playerX = 0
elif playerX >= 736:
playerX = 736
# boundry algorithm, make sure alien doesn't go out of bountry
for i in range(amount_aliens):
alienX[i] += alienX_change[i]
if alienX[i] <= 0:
alienX_change[i] = 4
alienY[i] += alienY_change[i]
elif alienX[i] >= 736:
alienX_change[i] = -4
alienY[i] += alienY_change[i]
# collision
collision = hascollision(alienX[i], alienY[i], laserX, laserY)
if collision:
laserY = 650
laser_currentstate = "ready"
score += 5
print(score)
alienX[i] = random.randint(0, 735)
alienY[i] = random.randint(50, 200)
alien(alienX[i], alienY[i], i)
# movement of laser shot
if laserY <= 0:
laserY = 650
laser_currentstate = "ready"
if laser_currentstate is "fire":
fire_laser(laserX, laserY)
laserY -= laserY_change
# updates screen to show screen
main_player(playerX, playerY)
pygame.display.update()
pygame.quit()
I also can't get the game to quit by pressing the button.
Upvotes: 2
Views: 111
Reputation: 210909
It is a matter of Indentation.
The collision detection and alien(alienX[i], alienY[i], i)
have to be done in the for loop, which iterates all the aliens:
executed = True
while executed:
# [...]
# boundry algorithm, make sure alien doesn't go out of bountry
for i in range(amount_aliens):
alienX[i] += alienX_change[i]
if alienX[i] <= 0:
alienX_change[i] = 4
alienY[i] += alienY_change[i]
elif alienX[i] >= 736:
alienX_change[i] = -4
alienY[i] += alienY_change[i]
# INDENTATION !!!
# collision
collision = hascollision(alienX[i], alienY[i], laserX, laserY)
if collision:
laserY = 650
laser_currentstate = "ready"
score += 5
print(score)
alienX[i] = random.randint(0, 735)
alienY[i] = random.randint(50, 200)
alien(alienX[i], alienY[i], i)
Upvotes: 1
Reputation: 3648
The problemis that you only call
alien(alienX[i], alienY[i], i)
Once, where you should call it once for every one of your aliens (so in your for
loop)
Upvotes: 2