Reputation: 79
I was trying to fix the problem in my code for the "invalid position of blit" but once I solved that another error appeared. TypeError: 'float' object is not iterable. I tried putting int() around(when I mean around it I mean the code I tried was: int(self.x) += int(self.changeX)
)
Here is the code, I have put it in order, meaning this is all the related content in order from the code with everything else.
(Minimalistic Example. There's actually a lot more.):
class Enemy():
def __init__(self, enemyImg, enemyX, enemyY, enemyX_change, enemyY_change):
self.image = enemyImg
self.x = enemyX
self.y = enemyY
self.changeX = enemyX_change
self.changeY = enemyY_change
def move(self):
self.x += self.changeX
self.y += self.changeY
def draw(self):
screen.blit(self.image, (self.x, self.y))
#Enemy
enemyImg = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change = []
enemies = []
number_of_enemies = 5
#Making a list for all 5 enemies image, x value, y value, x value change, y value change. All variables are a seperate list
for i in range(number_of_enemies):
enemyImg.append(pygame.image.load('C:/Users/user/Desktop/Python/CodingBee/Pygame Application Prototype/zombie.png'))
enemyX.append(random.randint(0, 736))
enemyY.append(random.randint(50, 150))
enemyX_change.append(0.45)
enemyY_change.append(40)
enemy = Enemy(enemyImg, enemyX, enemyY, 0.45, 0)
enemies.append(enemy)
#def states an event. The event will not occur unless you call the event in the main game loop.
back_ground = Background()
#Main Game Loop
clock = pygame.time.Clock()
SCEEN_UPDATE = pygame.USEREVENT
pygame.time.set_timer(SCEEN_UPDATE,150)`
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
Player.player_jump()
for enemy in enemies:
enemy.move()
if event.key == pygame.K_SPACE:
if Health_State == "ready":
Health_Fire_Sound = mixer.Sound('laser.wav')
Health_Fire_Sound.play()
Health.fire_Health(HealthX , HealthY)
HealthY = playery + 10
for enemy in enemies:
enemy.draw()
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
playery += playery_change
screen.fill(0,0,255)
back_ground.update()
back_ground.render()
#Health Kit Movement
if HealthX >= 800:
HealthX = 20
Health_State = "ready"
if Health_State == "fire":
Health.fire_Health(HealthX , HealthY)
HealthX -= HealthX_Change
#Enemy Movement and collision check
for i in range(number_of_enemies):
if enemyX[i] > 20:
for j in range(number_of_enemies):
enemyY = 2000
Other.game_over()
break
Enemy.move()
collision = Other.isCollision(enemyX[i],enemyY[i],HealthX,HealthY)
if collision:
BulletY = 480
Bullet_State = "ready"
enemyX[i] = random.randint(800,900)
enemyY[i] = 405
HealthX = 20
Health_State = "ready"
Score_Value += 1
Upvotes: 1
Views: 928
Reputation: 210877
enemyImg
, enemyX
and enemyY are lists. However, the arguments to the Enemy
constructor have to be a single image and the x and y coordinate of the enemy.
You do not need the lists enemyImg
, enemyX
, enemyY
, enemyX_change
and enemyY_change
. Delete the lists:
enemies = []
number_of_enemies = 5
enemyImg = pygame.image.load('C:/Users/user/Desktop/Python/CodingBee/Pygame Application Prototype/zombie.png')
for i in range(number_of_enemies):
x = random.randint(0, 736)
y = random.randint(50, 150)
enemy = Enemy(enemyImg, x, y, 0.45, 0)
enemies.append(enemy)
Note that you can get the coordinates of an enemy with enemies[i].x
and enemies[i].y
instead of enemyX[i]
and enemyY[i]
Furthermore you have to move and draw the enemies in the application loop not in the vent loop.
Your code should look something like this:
class Enemy():
def __init__(self, enemyImg, enemyX, enemyY, enemyX_change, enemyY_change):
self.image = enemyImg
self.x = enemyX
self.y = enemyY
self.changeX = enemyX_change
self.changeY = enemyY_change
def move(self):
self.x += self.changeX
self.y += self.changeY
def draw(self):
screen.blit(self.image, (self.x, self.y))
enemies = []
number_of_enemies = 5
enemyImg = pygame.image.load('C:/Users/user/Desktop/Python/CodingBee/Pygame Application Prototype/zombie.png')
for i in range(number_of_enemies):
x = random.randint(0, 736)
y = random.randint(50, 150)
enemy = Enemy(enemyImg, x, y, 0.45, 0)
enemies.append(enemy)
#def states an event. The event will not occur unless you call the event in the main game loop.
back_ground = Background()
#Main Game Loop
clock = pygame.time.Clock()
SCEEN_UPDATE = pygame.USEREVENT
pygame.time.set_timer(SCEEN_UPDATE,150)`
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
Player.player_jump()
if event.key == pygame.K_SPACE:
if Health_State == "ready":
Health_Fire_Sound = mixer.Sound('laser.wav')
Health_Fire_Sound.play()
Health.fire_Health(HealthX , HealthY)
HealthY = playery + 10
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
playery += playery_change
for enemy in enemies:
if enemy.x > 20:
for j in range(number_of_enemies):
enemyY = 2000
Other.game_over()
break
enemy.move()
collision = Other.isCollision(enemy.x,enemy.y,HealthX,HealthY)
if collision:
BulletY = 480
Bullet_State = "ready"
enemy.x = random.randint(800,900)
enemy.y = 405
HealthX = 20
Health_State = "ready"
Score_Value += 1
#Health Kit Movement
if HealthX >= 800:
HealthX = 20
Health_State = "ready"
if Health_State == "fire":
Health.fire_Health(HealthX , HealthY)
HealthX -= HealthX_Change
screen.fill(0,0,255)
back_ground.update()
back_ground.render()
for enemy in enemies:
enemy.draw()
Upvotes: 1