Home > other >  Questions about pygame Sprite class
Questions about pygame Sprite class

Time:01-07

This is a fabricated with pygame aircraft games, currently only plotted aircraft and bullets
Part of the code has been omitted
Main function:
Bullets=pygame. Sprite. Group ()
While True:
Gf. Check_events (ai_settings, screen, ship, bullets)
The ship. The update ()
Bullets. The update ()
Gf. Update_screen (ai_settings, screen, ship, bullets)

Bullet:
Bullet. Py
The class Bullet (Sprite) :
"" "a bullet to launch the management class "for" "

Def __init__ (self, ai_settings, screen, ship) :
"" "on the ship's position to create a bullet object "" "
Super () __init__ ()
The self. The screen=screen

Def draw_bullet (self) :
"" "drawing on the screen shot "","
Pygame. The draw. The rect (self. The screen, the self. The color, the self. The rect)

Gf. Py:
# response keys
Def check_keydown_events (event, ai_settings, screen, ship, bullets) :
# create bullets and should be added to the marshalling bullets in
# I created two bullet here
New_bullet=Bullet (ai_settings, screen, ships)
Bullets. The add (new_bullet)
Print (bullets)
New_bullet2=Bullet (ai_settings, screen, ships)
Bullets. The add (new_bullet2)
Print (bullets)

# update the graphics on the screen, and switch to the new screen
Def update_screen (ai_settings, screen, ship, bullets) :
# redraw all bullets
For bullet in bullets. Sprites () :
Bullet. Draw_bullet ()

Problems: 1. The players press the space key, will create and add new instances in the bullets grouping of bullets, and then draw the bullet on update_screen function, this method will traverse the marshalling of the elves, and call for each elf draw_bullet (). If is traversal, why every time I press the space only generates a bullet but not increasing the number of bullets
2. I set in order to create two instances every keystroke, but why the actual or will only draw one?
O great god answer

CodePudding user response:

This is not a python programming from entry to practice in a

  • Related