Merhaba aşağıdaki kodu çalıştırdığımda başlıktaki hatayı alıyorum bunun çözümü nedir. Kod tamamen alıntıdır bir videodan bakarak öğrenmeye çalışıyorum.
Not: Zemini yüklememe izin vermediği için koddan o bölümleri yorum satırı yapmak zorunda kaldım.
Kod:
import pygame, sys
from random import randint
class Tree(pygame.sprite.Sprite):
def __init__(self,pos,group):
super().__init__(group)
self.image = pygame.image.load('Camera/graphics/tree.png').convert_alpha()
self.rect = self.image.get_rect(topleft = pos)
class Player(pygame.sprite.Sprite):
def __init__(self,pos,group):
super().__init__(group)
self.image = pygame.image.load('Camera/graphics/player.png').convert_alpha()
self.rect = self.image.get_rect(center = pos)
self.direction = pygame.math.Vector2()
self.speed = 5
def input(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
self.direction.y = -1
elif keys[pygame.K_DOWN]:
self.direction.y = 1
else:
self.direction.y = 0
if keys[pygame.K_RIGHT]:
self.direction.x = 1
elif keys[pygame.K_LEFT]:
self.direction.x = -1
else:
self.direction.x = 0
def update(self):
self.input()
self.rect.center += self.direction * self.speed
class CameraGroup(pygame.sprite.Group):
def __init__(self) -> None:
super().__init__()
self.display_surface = pygame.display.get_surface()
# kutu kurulumu
self.offset = pygame.math.Vector2()
self.camera_borders = {"left": 200, "right": 200, "top": 100, "bottom": 100}
l = self.camera_borders["left"]
t = self.camera_borders["top"]
w = self.display_surface.get_size()[0] - (self.camera_borders["left"] + self.camera_borders["right"])
h = self.display_surface.get_size()[1] - (self.camera_borders["top"] + self.camera_borders["bottom"])
self.camera_rect = pygame.Rect(l,t,w,h)
# yer
#self.ground_surface = pygame.image.load("Camera/graphics/ground.png").convert_alpha()
#self.ground_rect = self.ground_surface.get_rect(topleft = (0,0))
def box_target_camera(self, target):
if target.rect.left < self.camera_rect.left:
self.camera_rect = target.rect.left
self.offset.x = self.camera_rect.left - self.camera_borders["left"]
self.offset.y = self.camera_rect.top - self.camera_borders["top"]
def costum_draw(self):
self.box_target_camera(player)
# yer
#self.display_surface.blit(self.ground_surface, self.ground_rect)
# aktif elemanlar
for sprite in sorted(self.sprites(), key = lambda sprite: sprite.rect.centery):
self.display_surface.blit(sprite.image, sprite.rect)
pygame.draw.rect(self.display_surface, "yellow", self.camera_rect, 5)
pygame.init()
pencere = pygame.display.set_mode((1280,720))
clock = pygame.time.Clock()
# setup
camera_group = CameraGroup()
player = Player((640,360), camera_group)
for i in range(20):
random_x = randint(1000, 2000)
random_y = randint(1000, 2000)
Tree((random_x, random_y), camera_group)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
tus = pygame.key.get_pressed()
if tus[pygame.K_ESCAPE]:
pygame.quit()
sys.exit()
pencere.fill(("#71ddee"))
camera_group.update()
camera_group.costum_draw()
pygame.display.update()
clock.tick(60)
Resimler: