简体   繁体   English

如何在pygame中移动相机

[英]How to move the camera in pygame

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()

Ok, so cameras don't really exist in pygame.好的,所以相机在 pygame 中并不存在。 They are just illusions created by blitting the game elements with an offset, usually set by the player.它们只是通过偏移量(通常由玩家设置)对游戏元素进行 blitting 所产生的幻觉。 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.至于它是鼠标控制的,我建议将它设置为鼠标位置除以或减去一些量,这样它就不会只是快速而盲目地跟随鼠标位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM