I according to the b black programmers write a tutorial of plane war, but the problem is always has a double
Wait on you teach may be part of the code for this problem!
Problem picture

The main program:





Class:





CodePudding user response:
Didn't see what problem, paste the code to comeCodePudding user response:
But but this is all the codeCodePudding user response:
Use copy and paste the code, don't use picturesCodePudding user response:
Game background didn't updateCodePudding user response:
After the update location update wrongCodePudding user response:

I added an update on this side of the update background still has a double
CodePudding user response:
Class document source:Import the random
The import pygame
SCREEN_RECT=pygame. The Rect (0, 0, 480, 700)
FPS=60
CREATE_ENEMY_EVENT=pygame. USEREVENT
HERO_FIRE_EVENT=pygame. USEREVENT + 1
The class GameSprite (pygame. Sprite. Sprite) :
"" "game spirit base class "" "
Def __init__ (self, image_name, speed=1) :
Super () __init__ ()
The self. The image=pygame. Image. The load (image_name)
The self. The rect=self. Image. Get_rect ()
The self. The speed=speed
Def update (self) :
# the default in the vertical direction
The self. The rect. + y=self. Speed
Class Background (GameSprite) :
"" "the game background "" "
Def __init__ (self, is_alt=False) :
Image_name="./images/background. PNG "
Super () __init__ (image_name)
If is_alt:
The self. The rect. Y=- self. The rect. Height
The class Enemy (GameSprite) :
Def __init__ (self) :
Super () __init__ ("./images/enemy1. PNG ")
The self. The speed=random. Randint (1, 3)
The self. The rect. Bottom=0
Max_x=SCREEN_RECT. Width - self. The rect. Width
The self. The rect. X=random. Randint (0, max_x)
Def update (self) :
Super (). The update ()
If the self. The rect. Y & gt; SCREEN_RECT. Height:
The self. The kill ()
Def __del__ (self) :
Print (" enemy planes flying in and hang up ")
The class Hero (GameSprite) :
Def __init__ (self) :
Super () __init__ ("./images/me1. PNG ", 0)
The self. The rect. Centerx=SCREEN_RECT. Centerx
The self. The rect. Bottom=SCREEN_RECT. Bottom - 120
The self. The bullets=pygame. Sprite. Group ()
Def update (self) :
The self. The rect. X +=self. Speed
If the self. The rect. Left & lt; 0:
The self. The rect. Left=0
If the self. The rect. Right & gt; SCREEN_RECT. Right:
The self. The rect. Right.=SCREEN_RECT right
Def the fire (self) :
For I in (0) :
Bullet=bullet ()
Bullet. The rect. Bottom=self. The rect. Y - I * 20
Bullet. The rect. Centerx=self. The rect. Centerx
Self. Bullets. The add (bullet)
The class Bullet (GameSprite) :
Def __init__ (self) :
Super () __init__ ("./images/bullet1. PNG ", 2)
Def update (self) :
Super (). The update ()
If the self. The rect. Bottom & lt; 0:
The self. The kill ()
Def __del__ (self) :
Print (" the bullet be destroyed ")
Main function:
The import pygame
The from pygame. Sprite import Group
The from class_gamesprite import *
The class PlaneGame (object) :
Def __init__ (self) :
Print (" game initialization ")
The self. The screen=pygame. Display. Set_mode (SCREEN_RECT. Size)
The self. The clock=pygame. Time. Clock ()
Self. __create_sprites ()
Pygame. Time. Set_timer CREATE_ENEMY_EVENT, (1000)
Pygame. Time. Set_timer HERO_FIRE_EVENT, (500)
Def __create_sprites (self) :
Bg1=Background ()
Bg2=Background (True)
Self. Bg_group=pygame. Sprite. Group (bg1 and bg2)
Self. Enemy_group=pygame. Sprite. Group () # enemy planes flying in and out at the beginning so don't need to add the object inside the
The self. The hero=hero ()
Self. Hero_group=pygame. Sprite. Group (self. Hero)
Def start_game (self) :
Print (" game ")
While True:
The self. The clock. Tick (FPS)
Self. __event_handler ()
Self. __check_collide ()
Self. __update_sprites ()
Pygame. Display. The update ()
Def __event_handler (self) :
# event listenersFor the event in pygame. Event. The get () :
If the event. The type==pygame. QUIT:
PlaneGame. __game_over ()
Elif event. Type==CREATE_ENEMY_EVENT:
Enemy=enemy ()
Self. Enemy_group. Add (Enemy ())
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull