I am asking the question I did because I need a scrolling camera. I already tried a few methods on a camera, but none of them worked. Or they said that there was a global name error. This is the code I used that has a globalname error
import pygame
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
screen_width = 700
screen_height = 400
half_width = int(screen_width / 2)
half_height = int(screen_height / 2)
def main():
screen = pygame.display.set_mode([screen_width, screen_height])
pygame.display.set_caption("RPG")
global cameraX, cameraY
pygame.init()
player = pygame.image.load("Textures(Final)\Whoo_For_Testing.bmp").convert()
playerect = player.get_rect()
player.set_colorkey(WHITE)
grasstile = pygame.image.load("Textures(Final)\Grass_Tile.bmp").convert()
watertile = pygame.image.load("Textures(Final)\Water_Tile.bmp").convert()
waterbeach = pygame.image.load("Textures(Final)\Water_Beach.bmp").convert()
grassrect = grasstile.get_rect()
waterrect = watertile.get_rect()
waterb = waterbeach.get_rect()
TILE_WIDTH = 32
TILE_HEIGHT = 32
tilemap = [
[grasstile, grasstile, grasstile, grasstile, waterbeach, watertile, watertile, watertile, watertile],
[grasstile, grasstile, grasstile, grasstile, waterbeach, watertile, watertile, watertile, watertile],
[grasstile, grasstile, grasstile, grasstile, waterbeach, watertile, watertile, watertile, watertile],
[grasstile, grasstile, grasstile, grasstile, waterbeach, watertile, watertile, watertile, watertile],
[grasstile, grasstile, grasstile, grasstile, waterbeach, watertile, watertile, watertile, watertile],
[grasstile, grasstile, grasstile, grasstile, waterbeach, watertile, watertile, watertile, watertile],
[grasstile, grasstile, grasstile, grasstile, waterbeach, watertile, watertile, watertile, watertile],
[grasstile, grasstile, grasstile, grasstile, waterbeach, watertile, watertile, watertile, watertile],
[grasstile, grasstile, grasstile, grasstile, waterbeach, watertile, watertile, watertile, watertile],
[grasstile, grasstile, grasstile, grasstile, waterbeach, watertile, watertile, watertile, watertile],
[grasstile, grasstile, grasstile, grasstile, waterbeach, watertile, watertile, watertile, watertile],
[grasstile, grasstile, grasstile, grasstile, waterbeach, watertile, watertile, watertile, watertile],
[grasstile, grasstile, grasstile, grasstile, waterbeach, watertile, watertile, watertile, watertile]
]
total_level_width = len(tilemap[0]) * 32
total_level_height = len(tilemap)*32
camera = Camera(simple_camera ,total_level_width, total_level_height)
map_surface = pygame.Surface(( len(tilemap[0])*TILE_WIDTH, len(tilemap)*TILE_HEIGHT))
for y,row in enumerate(tilemap):
for x,tile_surface in enumerate(row):
map_surface.blit(tile_surface,(x*TILE_WIDTH,y*TILE_HEIGHT))
map_surface = pygame.transform.scale(map_surface, (1200, 800))
player = pygame.transform.scale(player, (50, 100))
done = False
clock = pygame.time.Clock()
move_speed = 5
x, y = 100, 100
entities = pygame.sprite.Group()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
y -= move_speed
elif keys[pygame.K_DOWN]:
y += move_speed
elif keys[pygame.K_LEFT]:
x -= move_speed
elif keys[pygame.K_RIGHT]:
x += move_speed
screen.fill(BLACK)
screen.blit(map_surface, grassrect)
screen.blit(player, (x,y))
camera.update(player)
clock.tick(20)
pygame.display.flip()
class Camera(object):
def __init__(self, camera_func, width, height):
self.camera_func = camera_func
self.state = Rect(0, 0, width, height)
def apply(self, target):
return target.rect.move(self.state.topleft)
def update(self, target):
self.state = self.camera_func(self.state, target.rect)
def simple_camera(camera, target_rect):
l, t, _, _ = target_rect
_, _, w, h = camera
return Rect(-l+HALF_WIDTH, -t+HALF_HEIGHT, w, h)
def complex_camera(camera, target_rect):
l, t, _, _ = target_rect
_, _, w, h = camera
l, t, _, _ = -l+HALF_WIDTH, -t+HALF_HEIGHT, w, h
l = min(0, l) # stop scrolling at the left edge
l = max(-(camera.width-WIN_WIDTH), l) # stop scrolling at the right edge
t = max(-(camera.height-WIN_HEIGHT), t) # stop scrolling at the bottom
t = min(0, t) # stop scrolling at the top
return Rect(l, t, w, h)
if __name__ == "__main__":
main()
pygame.quit()
So essentially what I am asking is how to fix this. Sorry, not that good at explaining stuff. Thank you!