У меня есть этот класс камеры, над которым я работаю и пытаюсь заставить камеру следовать за игроком и прокручивать фон.Я смотрел десятки видео на YouTube и читал похожие вопросы на SO, но я застрял здесь, вот исходный код: (main.py)
import pygame
import player
import enemy
import environment
import collision as collision
import camera
pygame.init()
size = WIDTH, HEIGHT = (640, 480)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Game 2")
FPS = 60
fpsclock = pygame.time.Clock()
player.player1 = player.player(300, 410, 32, 32)
player.player1.initialize()
enemyVector = []
enemy.enemy1 = enemy.enemy(160, 170, 32, 32)
enemy.enemy1.initialize()
enemyVector.append(enemy.enemy1)
environmentVector = []
#environment.environment1.load_map(environmentVector)
counter1 = 0
counter2 = 0
string = ''
with open('map.txt', 'r') as f:
for line in f:
for character in line:
if character == '0':
pass
elif character == '1':
environment.environment1 = environment.environment(160, 100, 32, 32)
environmentVector.append(environment.environment1)
environmentVector[-1].x = counter1 * 32
environmentVector[-1].y = counter2 * 32
counter1 += 1
counter1 = 0
counter2 += 1
print(environmentVector[1].x)
run = True
while run:
pygame.time.delay(50)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if player.player1.active:
player.player1.frame += 1
if player.player1.frame >= 2:
player.player1.frame = 0
for i in enemyVector:
if i.ready_to_move():
i.move()
if i.ready_to_animate():
i.animate()
player.player1.playerImage = player.player1.cells[player.player1.frame + player.player1.animation]
for i in enemyVector:
i.enemyImage = i.cells[i.frame + i.animation]
if keys[pygame.K_LEFT] and player.player1.canMoveLeft and player.player1.x > player.player1.vel:
player.player1.x -= player.player1.vel
player.player1.animation = 3
player.player1.active = True
elif keys[pygame.K_RIGHT] and player.player1.canMoveRight and player.player1.x < 640 - player.player1.width - player.player1.vel:
player.player1.x += player.player1.vel
player.player1.animation = 6
player.player1.active = True
elif keys[pygame.K_UP] and player.player1.canMoveUp and player.player1.y > player.player1.vel:
player.player1.y -= player.player1.vel
player.player1.animation = 9
player.player1.active = True
elif keys[pygame.K_DOWN] and player.player1.canMoveDown and player.player1.y < 480 - player.player1.height - player.player1.vel:
player.player1.y += player.player1.vel
player.player1.animation = 0
player.player1.active = True
elif keys[pygame.K_s]:
player.player1.attack = True
player.player1.animation = 12
else:
player.player1.active = False
collision.check_collision_enemy(enemyVector, environmentVector)
collision.check_collision_player(player.player1, environmentVector)
player.player1.update_xy()
enemy.enemy1.update_xy()
for i in environmentVector:
i.update_xy()
screen.fill((0, 0, 0))
for i in enemyVector:
screen.blit(i.enemyImage, (i.x, i.y))
screen.blit(player.player1.playerImage, (player.player1.x, player.player1.y))
for i in environmentVector:
pygame.draw.rect(screen, (255,255,255), (i.x, i.y, 32, 32))
pygame.display.flip()
fpsclock.tick(FPS)
pygame.quit()
(camera.py):
class Camera:
def __init__(self, players):
self.left_viewbox = display_width/2 - display_width/8
self.right_viewbox = display_width/2 + display_width/10
def follow(self, shift_x):
virtualwindow.x += shift_x
print virtualwindow.x
for i in players:
i.rect.x += shift_x
def viewbox(self):
if player.x <= self.left_viewbox:
view_difference = self.left_viewbox - player.x
player.x = self.left_viewbox
self.follow(view_difference)
if player.x >= self.right_viewbox:
view_difference = self.right_viewbox - player.x
player.x = self.right_viewbox
self.follow(view_difference)
howможет ли этот класс камеры быть изменен и реализован так, чтобы камера следовала за игроком и прокручивала список записей на заднем плане