..
class Bullet(pygame.sprite.Sprite):
def __init__(self, screen, speed_x):
pygame.sprite.Sprite.__init__(self)
self.x, self.y = 550.0, 450.0
self.screen = screen
self.image = pygame.image.load("img/bullet.png").convert()
self.rect = self.image.get_rect(center=(self.x, self.y))
# для хранения наших координат
self.rx, self.ry = self.rect.x, self.rect.y
self.speed_x = speed_x
self.speed_y = 0.1
def update(self):
if self.ry > 0:
self.ry -= self.speed_y
self.rx += self.speed_x
# и вот теперь уже присваиваем значения
self.rect.x, self.rect.y = self.rx, self.ry
else:
self.kill()
..
def update(self):
if self.y > 0:
self.y -= self.speed_y
self.x += self.speed_x
self.rect.centerx, self.rect.centery = self.x, self.y
else:
self.kill()
font = font.Font("freesansbold.ttf", 15)
...
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x -= velocity
if event.key == pygame.K_RIGHT:
x += velocity
if event.key == pygame.K_UP:
y -= velocity
if event.key == pygame.K_DOWN:
y += velocity
# здесь же и проверка на выход из программы
elif event.type==pygame.QUIT:
run = False
pygame.quit()
quit()
screen.blit(player_img,(x,y))
...
class Projectile(pygame.sprite.Sprite):
def __init__(self, weigth, heigth, corner):
...
# добавляем каждому снаряду свою скорость. вычисляем поворотом на передаваемый угол
self.projectile_speed = pygame.math.Vector2((4, 0)).rotate(-corner)
def update(self):
# и тут соответственно используем вектор скорости собственный для каждого снаряда
self.projectile += self.projectile_speed
if hunter_pos_y < pos_y_rand:
hunter_pos_y += 1
if hunter_1.y < pos_y_rand:
hunter_1.y += 1
self.fire = False
def check_keydown_events(event, ship, settings, screen, bullets):
if event.key == pygame.K_LEFT:
# left arrow keydown events
ship.move_left = True # moves ship left when key's keeping pressed
if event.key == pygame.K_RIGHT:
# right arrow keydown events
ship.move_right = True # moves ship right when key's keeping pressed
if event.key == pygame.K_DOWN:
# down arrow keydown events
ship.move_down = True # moves ship down when key's keeping pressed
if event.key == pygame.K_UP:
# up arrow keydown events
ship.move_up = True # moves ship up when key's keeping pressed
if event.key == pygame.K_SPACE:
ship.fire = True
def check_events(ship, settings, screen, bullets):
"""main event checker function"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
# working on keydown events
check_keydown_events(event, ship, settings, screen, bullets)
# function that checks keydown event type and works according event type
elif event.type == pygame.KEYUP:
check_keyup_events(event, ship)
# function that checks keyup event type and works according event type
print(len(bullets)) # to check amount of bullets on screen
if ship.fire and len(bullets) < settings.bullets_allowed:
# continues firing until amount of bullets reaches limit
new_bullet = Bullet(settings, ship, screen) # creating new bullet
bullets.add(new_bullet) # adding created bullet to Group
иногда еле-ели вертится!)
bmp_дерево_rot = pygame.transform.rotate(bmp_дерево, rotate(bmp_image))
bmp_image = bmp_дерево_rot.get_rect(center=(150, 700))
sc.blit(bmp_дерево_rot, bmp_image)
def rotate(oject2):
mouse_x, mouse_y = pygame.mouse.get_pos()
x, y = bmp_image.center
rel_x, rel_y = mouse_x - x, mouse_y - y
angle = (180 / math.pi) * -math.atan2(rel_y, rel_x)
return angle
def __init__(self):
....
self.speed = SPEED
def update(self):
self.rect.x += self.speed
if self.rect.left < 0 or self.rect.right > WIDTH:
self.speed = -self.speed
win.fill((0,0,0))
self.x >= -self.width:
TypeError: '>=' not supported between instances of 'NoneType' and 'int'
self.x = None
self.x = radius