I am getting an error for the bullets. I want the bullets to move across the x axis when fired. bullet.x is getting ~ AttributeError: ‘list’ object has no attribute ‘x’. This is occurring in my function that handle_bullet. It should be bullet.x += BULLET_SPEED. This is when I fire from the player.
def draw_window(red,yellow, red_bullets, yellow_bullets,red_health,yellow_health):
screen.blit(SPACE_BACKGROUND,(0,0))
pygame.draw.rect(screen,BLACK,BORDER )
#
red_health_text = HEALTH_FONT.render('Health: '+str(red_health),1,WHITE)
screen.blit(red_health_text, (WIDTH-red_health_text.get_width()-10,10))
#
yellow_health_text = HEALTH_FONT.render('Health: '+str(yellow_health),1,WHITE)
screen.blit(yellow_health_text, (10,10))
# yellow.x/y target the RECT creation in the Main function as starting points
screen.blit(YELLOW_SPACESHIP,(yellow.x,yellow.y))
screen.blit(RED_SPACESHIP,(red.x,red.y))
for bullet in red_bullets:
pygame.draw.rect(screen, RED, bullet)
#
for bullet in yellow_bullets:
pygame.draw.rect(screen,YELLOW,bullet)
pygame.display.update()
#
#
#
class Movement:
def yellow_movement(keys_pressed, yellow):
# Targets the key pressed and the box they can not go passed
if keys_pressed[pygame.K_a] and yellow.x - 5 > 0:
yellow.x -= 5
elif keys_pressed[pygame.K_d] and yellow.x + 5 + yellow.width < BORDER.x:
yellow.x += 5
elif keys_pressed[pygame.K_w] and yellow.y - 5 > 0:
yellow.y += -5
elif keys_pressed[pygame.K_s] and yellow.y + 5 + yellow.height < HEIGHT:
yellow.y += 5
#
def red_movement(keys_pressed, red):
if keys_pressed[pygame.K_LEFT] and red.x - 5 > BORDER.x:
red.x -= 5
elif keys_pressed[pygame.K_RIGHT] and red.x + 5 + red.width < WIDTH:
red.x += 5
elif keys_pressed[pygame.K_UP] and red.y - 5 > 0:
red.y += -5
elif keys_pressed[pygame.K_DOWN] and red.y + 5 + red.height < HEIGHT:
red.y += 5
movement = Movement
#
#
#
def handle_bullets(yellow_bullets,red_bullets,yellow,red):
for bullet in yellow_bullets:
bullet.x += BULLET_SPEED
if red.colliderect(bullet):
pygame.event.post(pygame.event.Event(RED_HIT))
yellow_bullets.remove(bullet)
elif bullet.x > WIDTH:
yellow_bullets.remove(bullet)
for bullet in red_bullets:
bullet.x -= BULLET_SPEED
if yellow.colliderect(bullet):
pygame.event.post(pygame.event.Event(YELLOW_HIT))
red_bullets.remove(bullet)
elif bullet.x < 0:
red_bullets.remove(bullet)
#
#
#
def draw_winner(text):
draw_text = WINNER_FONT.render(text,1,WHITE)
screen.blit(draw_text, (WIDTH//2 - draw_text.get_width()/2,HEIGHT/2 - draw_text.get_height()/2))
pygame.display.update()
pygame.time.delay(5000)
#
#
#
# Main game loop
def main():
red = pygame.Rect(700,300,50,50)
yellow = pygame.Rect(100,300,50,50)
#
red_bullets = []
yellow_bullets = []
#
red_health = 10
yellow_health = 10
#
clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LSHIFT and len(yellow_bullets)<MAX_BULLETS:
bullet = pygame.Rect(yellow.x + yellow.width, yellow.y +yellow.height//2 - 2, 10,5)
yellow_bullets.append(yellow_bullets)
if event.key == pygame.K_RSHIFT and len(red_bullets)< MAX_BULLETS:
bullet = pygame.Rect(red.x , red.y + red.height//2 - 2, 10,5)
red_bullets.append(red_bullets)
if event.type == RED_HIT:
red_health -= 1
if event.type ==YELLOW_HIT:
yellow_health -= 1
winner_text = ''
if red_health <=0:
winner_text = 'Yellow Wins!'
if yellow_health <=0:
winner_text = 'Red Wins!'
if winner_text != '':
draw_winner(winner_text)
break
# Listening for the key pressed event below:
keys_pressed = pygame.key.get_pressed()
movement.yellow_movement(keys_pressed,yellow)
movement.red_movement(keys_pressed,red)
#
handle_bullets(yellow_bullets,red_bullets,yellow,red)
# calls the draw function to draw in main game loop
draw_window(red,yellow,red_bullets,yellow_bullets,red_health,yellow_health)
# Continues the game until closed
main()
# pygame.quit()
# Will only run the code if we are in the main file of the game
if __name__ == '__main__':
main()
pygame.display.update()
>Solution :
I think you have not added the bullet correctly to the list. You actually append a list to the bullet list
yellow_bullets.append(yellow_bullets)
Thus, when you write bullet.x you are calling a list which does not have that atribute.
UPDATE
I think to fix the error you need to write
yellow_bullets.append(bullet)
as this will make it append the rectangle class to the list allowing you to call bullet.x