Home > other >  Novice/consult bosses python shuttle program error
Novice/consult bosses python shuttle program error

Time:10-04

New bosses, please guide
After playing box, according to the left and right arrows to play box disappear, feedback
Traceback (the most recent call last) :
The File "d:/py/pycode/alien_invasion/alien_invasion py", line 47, in & lt; module>
Run_game ()
The File "d:/py/pycode/alien_invasion/alien_invasion py", line 32, in run_game
Gf. Check_events (ai_settings, screen, ship, bullets)
The File "d: \ p y \ pycode \ alien_invasion \ game_functions py", 46, the line in check_events
Check_keydown_events (event, ship, ai_settings, screen, bullets)
The File "d: \ p y \ pycode \ alien_invasion \ game_functions py", 26, the line in check_keydown_events
Ship. Moving_left=True
AttributeError: 'pygame. Surface object has no attribute' moving_left '
After playing box, press the space play box disappear, feedback
Traceback (the most recent call last) :
The File "d:/py/pycode/alien_invasion/alien_invasion py", line 47, in & lt; module>
Run_game ()
The File "d:/py/pycode/alien_invasion/alien_invasion py", line 32, in run_game
Gf. Check_events (ai_settings, screen, ship, bullets)
The File "d: \ p y \ pycode \ alien_invasion \ game_functions py", 46, the line in check_events
Check_keydown_events (event, ship, ai_settings, screen, bullets)
The File "d: \ p y \ pycode \ alien_invasion \ game_functions py", line 29, in check_keydown_events
New_bullet=Bullet (ai_settings, screen, ships)
The File "d: \ p y \ pycode \ alien_invasion \ bullet py", line 15, in __init__
The self. The rect=pygame. The rect (0, 0, ai_settings. Bullet_width, ai_settings bullet_height)
AttributeError: 'Ship' object has no attribute 'bullet_width'
The code is as follows:

 
# alien_invasion. Py
The import sys
The import pygame
From the set import Setting
From the ship import ship
The import game_functions as gf
The import importlib
Importlib. Reload (sys)
The from pygame. Sprite import Group


Def run_game () :

# to initialize the game and create a screen object
Pygame. The init ()
Ai_settings=Setting ()
Screen=pygame. Display. Set_mode ((ai_settings. Screen_width, ai_settings. Screen_height)) # double brackets?

Pygame. Display. Set_caption (" Alien night ")


# to create a ship
The ship=ship (ai_settings, screen)
# to create the bullet marshalling
Bullets=Group ()

# set the background color in
# bg_color=(176224230)

# start playing the main loop of
While True:
# monitoring the keyboard and mouse
Gf. Check_events (ai_settings, screen, ship, bullets)
The ship. The update ()
Bullets. The update ()
Gf. Update_screen (ai_settings, screen, ship, bullets)
# for the event in pygame. Event. The get () :
# if event. Type==pygame. QUIT:
# sys. The exit ()

# screen redraw each cycle
Screen. The fill (ai_settings. Bg_color)
Ship. Blitme ()

# let recently painted screen visible
Pygame. Display. Flip ()

Run_game ()



 
# game_functions. Py
The import sys
The import pygame
The from bullet import bullet

# def check_events (ship) :
# for the event in pygame. Event. The get () :
# if event. Type==pygame. QUIT:
# sys. The exit ()

# elif event. Type==pygame. KEYDOWN: # every button is registered as a KEYDOWN event has occurred
# if the event. The key==pygame. K_RIGHT:
# ship. Moving_right=True
# elif event. The key==pygame. K_LEFT:
# ship. Moving_left=True

# elif event. Type==pygame. KEYUP:
# if the event. The key==pygame. K_RIGHT:
# ship. Moving_right=False
# elif event. The key==pygame. K_LEFT:
# ship. Moving_left=False

Def check_keydown_events (event, ai_settings, screen, ship, bullets) :
If the event. The key==pygame. K_RIGHT:
Ship. Moving_right=True
Elif event. The key==pygame. K_LEFT:
Ship. Moving_left=True
Elif event. The key==pygame. K_SPACE:
# create a bullet and should be added to the marshalling bullets in
New_bullet=Bullet (ai_settings, screen, ships)
Bullets. The add (new_bullet)


Def check_keyup_events (event, ship) :
"" "response to loosen "" "
If the event. The key==pygame. K_RIGHT:
Ship. Moving_right=False
Elif event. The key==pygame. K_LEFT:
Ship. Moving_left=False

Def check_events (ai_settings, screen, ship, bullets) :
Response keys and mouse events. "" "" "" "
For the event in pygame. Event. The get () :
If the event. The type==pygame. QUIT:
Sys. The exit ()
Elif event. Type==pygame. KEYDOWN:
Check_keydown_events (event, ship, ai_settings, screen, bullets)
Elif event. Type==pygame. KEYUP:
Check_keyup_events (event, ships)


Def update_screen (ai_settings, screen, ship, bullets) :
# update the image on the screen, and switch to the new screen
# every loop redrawing the screen
Screen. The fill (ai_settings. Bg_color)

# behind the shuttle and alien redraw all bullets
For bullet in bullets. Sprites () :
Bullet. Draw_bullet ()

Ship. Blitme ()




# let recently painted screen visible
Pygame. Display. Flip ()



 
# ship. Py
The import pygame

The class Ship () :
Def __init__ (self, ai_settings, screen) :

# to initialize the spacecraft and set its initial position
The self. The screen=screen
Self. Ai_settings=ai_settings

# to load the spacecraft images and obtain its external rectangular
Self. Image=pygame. Image. The load (r 'D: \ \ p y \ pycode \ alien_invasion \ images ship. BMP')
The self. The rect=self. Image. Get_rect ()
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related