Как отследить координаты объекта в PyGame
ООП начал осваивать недавно, так что вот. Сделал скелет Flappy Bird, но при попытке отследить ушла ли труба за птичку ничего не получается (координаты доходят до половины поля, а потом перескакивают на следующую трубу). Как отслеживать координаты трубы до самого конца игрового поля? Вот код:
import pygame
import random
WIDTH = 500
HEIGHT = 600
FPS = 60
# Задаем цвета
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
GRAVITY = 8
JUMP_POWER = 8
count = 0
jump = False
# Создаем игру и окно
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Shmup!")
clock = pygame.time.Clock()
class Player(pygame.sprite.Sprite):
def __init__(self, W, H, X, Y):
pygame.sprite.Sprite.__init__(self)
self.w = W
self.h = H
self.image = pygame.Surface((W, H))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.x = X
self.rect.y = Y
self.life = True
self.score = 0
self.count = 0
self.jump = False
def update(self):
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
self.jump = True
self.count = JUMP_POWER
if 0 < self.rect.y and self.rect.y + self.w < HEIGHT and self.life:
if self.count >= -JUMP_POWER and self.jump:
self.rect.y -= self.count
self.count -= 1
else:
self.rect.y += GRAVITY
self.jump = False
else:
self.life = False
self.rect.y += GRAVITY
class Tower(pygame.sprite.Sprite):
def __init__(self, X, Y):
pygame.sprite.Sprite.__init__(self)
self.interval = WIDTH // 3
self.dist = HEIGHT // 3
self.width = 80
self.height = 2 * HEIGHT // 3
self.image = pygame.Surface((self.width, self.height))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.x = X
self.rect.y = Y
self.score = 1
self.move_speed = 4
def update(self):
if tower.rect.x < player.rect.x and self.score == 1:
print(1)
player.score += 1
self.score = 0
if player.life and self.rect.x > -self.width:
self.rect.x -= self.move_speed
else:
self.kill()
all_sprites = pygame.sprite.Group()
player = Player(25, 25, WIDTH // 4, HEIGHT // 2)
tower = Tower(2 * WIDTH // 3, 200)
all_sprites.add(player, tower)
# Цикл игры
running = True
while running:
# Держим цикл на правильной скорости
clock.tick(FPS)
# Ввод процесса (события)
for event in pygame.event.get():
# проверка для закрытия окна
if event.type == pygame.QUIT:
running = False
if WIDTH - tower.rect.x - tower.width > tower.interval:
A = HEIGHT // 4
B = 3 * HEIGHT // 4
tower = Tower(WIDTH, random.randint(A, B))
all_sprites.add(tower)
all_sprites.update()
screen.fill(BLACK)
pygame.draw.line(screen, WHITE, [player.rect.x, player.rect.y], [tower.rect.x, tower.rect.y], 3)
all_sprites.draw(screen)
pygame.display.flip()
pygame.quit()
print("Очков:", player.score)