Home > other >  Python from entry to practice the alien war has research for more than two days... For a great god
Python from entry to practice the alien war has research for more than two days... For a great god

Time:09-17

Problem is that move around as long as a normal error AttributeError: press space to 'pygame. Surface object has no attribute' the rect '

Don't know much about the great god, please identify the following code is

Settings. Py

The class Settings () :

Def __init__ (self) :
Self. Screen_width=1200
Self. Screen_height=600
Self. Bg_color=(230230230)
Self. Ship_speed=1.5
Self. Bullet_speed=1
Self. Bullet_width=3
Self. Bullet_height=15
Self. 60,60,60 bullet_color=

ship. Py

The import pygame

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

"" "initialize ship and set its initial position "" "
The self. The screen=screen
Self. Ai_settings=ai_settings
The self. The image=pygame. Image. The load (' images/ship. BMP)
The self. The rect=self. Image. Get_rect ()
Self. Screen_rect=screen. Get_rect ()

The self. The rect. Centerx=self. Screen_rect. Centerx
The self. The rect. Bottom=self. Screen_rect. Bottom

# in the shuttle's attribute center stored in the smaller numerical
Self center=float (self. The rect. Centerx)

Self. Moving_right=False
Self. Moving_left=False



Def update (self) :

"" "according to the moving signs to adjust the position of the ship ", "

# update center value of the ship, and not the rect
If self. Moving_right and self. The rect. Right & lt; Self. Screen_rect. Right:
The self. The center +=self. Ai_settings. Ship_speed
If self. Moving_left and self. The rect. Left & gt; 0:
The self. The center -=self. Ai_settings. Ship_speed

# according to the self. The center to update the rect object
The self. The rect. Centerx=self. Center

Def blitme (self) :
The self. The screen. The blit (self image, self. The rect)

bullet. Py

The import pygame

The from Settings import Settings

The from pygame. Sprite import Sprite

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


# said in (0, 0) to create a rectangle of bullets,
to set the correct positionThe self. The rect=pygame. The rect (0, 0, ai_settings. Bullet_width, ai_settings bullet_height)
The self. The rect. Centerx=ship. The rect. Centerx
The self. The rect. Top=ship. The rect. Top

# stored in the decimal representation of the bullets position
Self. Y=float (self. The rect. Y)

The self. The color=ai_settings. Bullet_color


Def update (self) :

"" "move up the bullet "" "

Position # update said bullets small number
Self. - y=self. Ai_settings. Bullet_speed

# update is the location of the bullet of the rect
The self. The rect. Y=self. Y


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)

game_functions. Py

The import sys
The import pygame
The from Settings import Settings
From the ship import ship
The from bullet import bullet

Def check_keydown_events (event, ai_settings, screen, ship, bullets) :

Response buttons "" "" ""
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, ship, screen)
Bullets. The add (new_bullet)

Def check_keyup_events (event, ai_settings, screen, ship, bullets) :

"" "response to loosen "" "
If the event. The key==pygame. K_LEFT:
Ship. Moving_left=False

Elif event. The key==pygame. K_RIGHT:
Ship. Moving_right=False


Def check_events (ai_settings, screen, ship, bullets) :

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, ai_settings, screen, ship, bullets)

Elif event. Type==pygame. KEYUP:
Check_keyup_events (event, ai_settings, screen, ship, bullets)

Def update_screen (ai_settings, screen, ship, bullets) :

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

Screen. The fill (ai_settings. Bg_color)
Ship. Blitme ()
Pygame. Display. Flip ()

alien_invasion. Py

The import sys

The import pygame

The import game_functions

The from pygame. Sprite import Group

The from Settings import Settings

From the ship import ship

The from bullet import bullet

Def run_game () :

Pygame. The init ()
Ai_settings=Settings ()
Screen=pygame. Display. Set_mode ((ai_settings. Screen_width, ai_settings. Screen_height))
Pygame. Display. Set_caption (" Alien night ")

The ship=ship (ai_settings, screen)

# to create a is used to store the bullet marshalling
Bullets=Group ()

While True:
Game_functions. Check_events (ai_settings, screen, ship, bullets)

The ship. The update ()

Bullets. The update ()

Game_functions. Update_screen (ai_settings, screen, ship, bullets)

Run_game ()

CodePudding user response:

Ah, really,nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related