1

I'm trying to build a menu screen where the camera reacts to the mouse slightly, moving a little bit in the direction of the camera. Here's what I have so far, but I can't find anything on how to move the camera.

import pygame
import button
import game
#create display window
SCREEN_HEIGHT = 720
SCREEN_WIDTH = 1280

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Menu')

#load button images
start_img = pygame.image.load('start_btn.bmp').convert_alpha()
exit_img = pygame.image.load('exit_btn.bmp').convert_alpha()

#create button instances
start_button = button.Button(600, 360, start_img, 0.5)
exit_button = button.Button(600, 500, exit_img, 0.6)


game = game()


#game loop
run = True
while run:

    screen.fill((255, 242, 203))

    if start_button.draw(screen):
        print('START')
    if exit_button.draw(screen):
        print('EXIT')

    #event handler
    for event in pygame.event.get():
        #quit game
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEMOTION:
            if game.state == "menu":
                position = pygame.mouse.get_pos()
                #move camera slightly in the direction of the mouse

    pygame.display.update()
    

pygame.quit()

thedankboi
  • 53
  • 1
  • 8
  • Which camera? Pygame has no camera. Moving the camera means that all objects in the scene are moved in the opposite direction of the direction in which the camera is to be moved. – Rabbid76 Jul 22 '22 at 21:20

1 Answers1

0

Ok, so cameras don't really exist in pygame. They are just illusions created by blitting the game elements with an offset, usually set by the player. So you should handle your events before the buttons and whatever else you want to move are drawn, and then set the coordinates of whatever you are drawing to where they usually go, but + or - the offset. And as for it being mouse controlled, I would recommend setting it to the position of the mouse divided or subtracted by some amount so it doesn't just quickly and blindly follow the mouse pos.

Dominic B.
  • 77
  • 7