создал кривой код но дело не в этом.Ошибка name alien is not defind не знаю в чем проблема вроде имя передавал по разному всеравно писало что alien not defind скорее всего я запутался и не могу понять в чем проблема вот код:
Python:
import sys
import pygame
from pygame.sprite import Sprite
from pygame.sprite import Group
pygame.init()
bullets = Group()
aliens = Group()
screen_width = 1200
screen_height = 800
screen = pygame.display.set_mode((screen_width, screen_height))
backgrounde = pygame.image.load("image_for_python\dirt.bmp")
ship_image = pygame.image.load("image_for_python\ship_alien_#4.png")
ship = ship_image.get_rect()
screen_rect = screen.get_rect()
ship.center = screen_rect.center
ship.bottom = screen_rect.bottom
ship_speed_factor = 6
center = float(ship.centerx)
#settings
bullet_height = 15
bullet_widht = 3
bullet_speed = 6
bullet_color = (20, 20, 20)
moving_right = False
moving_left = False
bullets_allowed = 3
class Bullet(Sprite):
global bullet_height, bullet_widht, bullet_speed, ship, screen, bullet_color
def __init__(self):
super(Bullet, self).__init__()
self.screen = screen
self.rect = pygame.Rect(0, 0, bullet_widht, bullet_height)
self.rect.centerx = ship.centerx
self.rect.top = ship.top
self.y = float(self.rect.y)
self.bullet_speed_factor = bullet_speed
self.color = bullet_color
def update(self):
self.y -= self.bullet_speed_factor
self.rect.y = self.y
def draw_bullet(self):
pygame.draw.rect(self.screen, self.color, self.rect )
bulet = Bullet()
class Alien(Sprite):
def __init__(self, screen):
super(Alien, self).__init__()
self.screen = screen
self.image_alien = pygame.image.load('image_for_python/brain_alien.png')
self.alien = self.image_alien.get_rect()
self.alien_width = self.alien.width
self.alien_height = self.alien.height
self.x = float(self.alien.x)
def blitme(self):
self.screen.blit(self.image_alien, self.alien)
def update():
global center,moving_right, moving_left
if moving_right and ship.right < 1248:
center += ship_speed_factor
if moving_left and ship.left > -57:
center -= ship_speed_factor
ship.centerx = center
def blitme(screen, backgrounde, ship, aliens):
calien = Alien(screen)
screen.blit(backgrounde,(0, 0))
screen.blit(ship_image, ship)
aliens.draw(screen)
for bullet in bullets.sprites():
bullet.draw_bullet()
pygame.display.flip()
def create_fleet(screen, aliens, alien):
alien = alien
screen = screen
calien = Alien(screen)
alien_width = calien.alien.width
avaliable_space = screen_width - 2 * alien_width
number_alien_x = int(avaliable_space/(2 * alien_width))
for number_alien in range(number_alien_x):
calien = Alien(screen)
calien.x = alien_width + 2 * alien_width * number_alien
calien.alien.x = alien.x
aliens.add(calien)
def run():
global moving_right, moving_left, bullets_allowed, bulet, bullets
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.mixer.music.stop()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
moving_right = True
elif event.key == pygame.K_LEFT:
moving_left = True
elif event.key == pygame.K_SPACE :
if len(bullets) < bullets_allowed:
new_bullet = Bullet()
bullets.add(new_bullet)
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT:
moving_right = False
elif event.key == pygame.K_LEFT:
moving_left = False
while True:
run()
update()
bulet.draw_bullet()
bullets.update()
create_fleet(screen, aliens, alien)
blitme(screen, backgrounde, ship, aliens)
for bullet in bullets.copy():
if bullet.rect.bottom <= 0:
bullets.remove(bullet)