alien_invasion.py
import sys
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
from pygame.sprite import Group
def run_game():
pygame.init()
-sharpinitialize screen and settings
ai_settings=Settings()
screen=pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion")
-sharpcreate a ship
ship=Ship(screen,ai_settings)
bullets=Group()
while True:
gf.check_events(ai_settings,screen,ship,bullets)
ship.update()
bullets.update()
gf.update_screen(ai_settings,screen,ship,bullets)
run_game()
bullet.py
import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
def __init__(self,ai_settings,screen,ship):
super(Bullet,self).__init__
self.screen=screen
self.rect=pygame.Rect(0,0,ai_settings.bullet_width,ai_settings.bullet_height)
self.rect.centerx=ship.rect.centerx
self.rect.top=ship.rect.top
self.color=ai_settings.bullet_color
self.speed=ai_settings.bullet_speed_factor
self.y=float(self.rect.y)
def update(self):
self.y-=self.speed
self.rect.y=self.y
def draw_bullet(self):
pygame.draw.rect(self.screen,self.color,self.rect)
game_functions.py
import sys
import pygame
from bullet import Bullet
def check_keyup_event(event,ship):
if event.key==pygame.K_RIGHT:
ship.move_right=False
elif event.key==pygame.K_LEFT:
ship.move_left=False
def check_keydown_event(event,ai_settings,screen,ship,bullets):
if event.key==pygame.K_RIGHT:
ship.move_right=True
elif event.key==pygame.K_LEFT:
ship.move_left=True
elif event.key==pygame.K_SPACE:
new_bullet=Bullet(ai_settings,screen,ship)
bullets.add(new_bullet)
def check_events(ai_settings,screen,ship,bullets):
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
elif event.type==pygame.KEYDOWN:
check_keydown_event(event,ai_settings,screen,ship,bullets)
elif event.type==pygame.KEYUP:
check_keyup_event(event,ship)
def update_screen(ai_settings,screen,ship,bullets):
screen.fill(ai_settings.bk_color)
for bullet in bullets.sprites():
bullet.draw_bullet()
ship.blitme()
pygame.display.flip()
settings.py
class Settings():
def __init__(self):
self.screen_width=1200
self.screen_height=600
self.bk_color=(230,230,230)
self.ship_speed_factor=1.5
self.bullet_height=15
self.bullet_width=3
self.bullet_speed_factor=1
self.bullet_color=(255,0,0)
ship.py
import pygame
class Ship():
def __init__(self,screen,ai_settings):
self.screen=screen
self.image=pygame.image.load("images/ship.bmp")
self.rect=self.image.get_rect()
self.ai_settings=ai_settings
self.screen_rect=screen.get_rect()
self.move_right=False
self.move_left=False
self.rect.centerx=self.screen_rect.centerx
self.location= float(self.rect.centerx)
self.rect.bottom=self.screen_rect.bottom
def blitme(self):
self.screen.blit(self.image,self.rect)
def update(self):
if self.move_right==True and self.screen_rect.right > self.rect.right:
self.location+=self.ai_settings.ship_speed_factor
if self.move_left==True and 0 < self.rect.left:
self.location-=self.ai_settings.ship_speed_factor
self.rect.centerx=self.location
< hr >
what"s wrong with this problem!
AttributeError: "Bullet" object has no attribute" _ Sprite__g"