python 3.x - Bullet to Enemy Collision Detection -


After a few hours of searching the code for reference. I failed, I tried to write the collision code again but it failed. Now my question is

How can I decide to remove the enemy ship in my code when bullet kills it

Note: I am not telling you to do this , I need this to explain further what the problem is or where IM is making

code segment
  for bullet bullet: Hit_list = pygame.sprite.Group () If pygame.sprite .collide_rect (Bullet, Enemy_list): Hit_list.add (hit) Hit for hit_list in: Bullets.remove (bullet) All.remove (bullet) All.remove (hit) Enemy_list.remove T) # If the bullet closes, then removes the screen if bullet.rect.y & lt; 0: bullet. Remove (Bullet) All.remove (bullet)   

error
  traceback (most recent call final): File "C: \" user \ Jb127996 \ Desktop \ Invaders Space in space \ Shooter.py ", line 92,  If pygame.sprite.collide_rect (bullet, Enemy_list):" C: \ Python32 \ lib \ site-package \ pygame \ AttributeError: in the 'group' object there is no attribute 'rect'   

the entire code / H1>
  import pygame import player_func import pygame.locals Import # # Background Background = pygame .image.load ('Tempback.png') Background_size = Background.get_size () Background_rect = Background.get_rect () # screen resoultion width = 640 height = 480 Scr = pygame.display.set_mode ( (Width, height)) # FPS Clock Wars (clock .tick (FPS)) clock = pugam.time.clock () fps = 60 #color white = (255, 255, 255) black = (0, 0, 0) Green = (0, 255, 0) red = (255, 0, 0) Blue = (0, 0, 255) orange = (255, 140, 0) Default = White # Sprite List Bullets = pygame.sprite.Group ( ) Enemy_list = pygame.sprite.Group () All = pygame.sprite.Group () # Class files Do AI = AI.Enemy () Ply = Player_func.Player () # Cord Ect.x for player AI.r, AI.rect.y = 100, 50 AIIPZ = (AITAct.X, AITAct.I) ply.act .x, ply.act.i = 580, 425p_poz = (pleak. X, Ply.rect.y) # True false statements and ongoing Vars = true gun_overload = 0 fire = 20 running: Enemy_list.add (AI) All.add (AI) All.add (Ply) # Collision for walls Check Spaceship for Pleixt.X & lt; = 0: Pleiq. Event for event (): X = = 5.5 elif ply.extax.x> = 590: ply.exex = = 5.5 # pygame.event.get if event.type == QUIT: movable = false elif event.type = = Keydown: if event.key == K_SPACE and Gun_Overload == Fire: Bullet = Player_func.Bullet () bullet.rect.x = (ply.ct.x +15) bullet.act.a = plete.a. Bullet.ad (bullet) all.ed (bullet) gun_overload = 0 # control down below group-overload & lt; Fire: Gun_Overload + = 1 AIHowment () Please control (updates) # Sprite lists so that they can all be prepared All.update () # Bullet bullet bullet bullet kills an enemy on the list Check: Hit_list = pygame.sprite.Group () if pygame.sprite.collide_rect (Bullet, Enemy_list): Hit_list.add (Hit) Hit_list for hits: Bullets.remove (bullet) All.remove (bullet) All. Remove (hit) Enemy_list. Delete (hits) # If the tablet is out of the screen then it is removed if bullet.rect.y < 0: Bullets.remove (Bullet) All.remove (Bullet) Scr.blit (Background, Background_rect) All.draw (SCR) pygame.display.flip () # FPS Clock loaded in Clock.tick (FPS) pygame.quit () If your condition uses bolt:  
    

You have 2 parameters - a bullet, and a Sprite Group pass. The error is telling you that the Sprite Group does not have a rake attribute.

I think you want to use it:

  pygame.sprite.spritecollide () Find sprites in a group that pierces the other phantom each other Spritecollide (Phantom, group, docil, bump = none) - & gt; Sprite_list   

In addition, I will convert all the variable names into lowercase. I have also found that your if bullet.rect.y & lt; 0: is under the colliding examination. I think you want to remove a tablet from the screen even if there is no collision.

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -