微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

pygame-如何使用停在边缘的相机使视差滚动正常工作

如何解决pygame-如何使用停在边缘的相机使视差滚动正常工作

说我们有一个用pygame制作的游戏,在这个游戏中,我们有“滚动”对象,该对象基本上将相机在其中滚动,并停在边缘。当播放器离开滚动并进入新的滚动时,相机将从一个滚动移动到另一滚动。在这种情况下,要获得视差效果,我们需要将相机滚动的量除以一个值。但是,如果我们只是单独执行此操作,则当我们移至新的“滚动”时,相对于其在第一个“滚动”中滚动了多少,背景看起来似乎偏移了不正确的量,如何解决此问题? ?这是我的相机代码和滚动代码

import pygame
from pygame.locals import *

class scroll_handler(list):
    def __init__(self):
        self.primary_scroll = None
        self.append(scroll_obj((50+1)*16,(2)*16,65*16,30*16))
        self.append(scroll_obj((51+65)*16,30*16))

    def update(self,player):
        for i in self:
            if i.entity_in_scroll(player):
                self.primary_scroll = i

class scroll_obj(pygame.Rect):
    def __init__(self,x,y,sx,sy,enable_backgrounds = True):
        super().__init__((x,y),(sx,sy))
        self.enable_backgrounds = enable_backgrounds

    def entity_in_scroll(self,entity):
        return self.contains(entity)

class camera(object):
    def __init__(self,camera_func,widthy,heighty):
        self.state = pygame.Rect((16,16),(widthy,heighty))
        self.camera_func = camera_func
        self.scroll = scroll_obj(16,16,heighty)

    def update_scroll(self,scroll):
        self.state = pygame.Rect(scroll.x,scroll.y,scroll.width,scroll.height)

    def apply(self,target,state = 0,parallax = False):
        if not parallax:
            return target.move(self.state.topleft)
        if parallax:
            return target.move(state.topleft)

    def update(self,parallax_x = 1,parallax_y = 1):
        self.update_scroll(self.scroll)
        self.state = self.camera_func( self.state,parallax_x,parallax_y)
    
def complex_camera(camera,target_rect,parallax_x=1,parallax_y=1):
    l,t,_,_ = target_rect
    _,w,h = camera
    l,_ = -l+(320),-t+(180),h
    
    l = min(-camera.x,l)                                       # stop scrolling at the left edge
    l = max(-(camera.x + camera.width-640),l)                  # stop scrolling at the right edge
    l = round(l/(parallax_x),2)                         # apply parallax in x direction
        
    t = max(-(camera.y + camera.height-360),t)                # stop scrolling at the bottom   
    t = min(-camera.y,t)                                       # stop scrolling at the top
    t = round(t/(parallax_y),2)                         # apply parallax in y direction
    return pygame.Rect(l,h)

这是我的背景代码,并将视差效果应用于背景

import pygame
class background(pygame.Rect):
    def __init__(self,image):
        super().__init__(x*16,y*16,1024,512)
        self.image = image
        self.CAX = self.x
        self.CAY = self.y
        self.rect = pygame.Rect(self.CAX,self.CAY,512) # an additional rect object used elsewhere in another code file,ignore this
        
    def apply_camera(self,state,GAME):
        cam = GAME.camera.apply(self,True)
        self.CAX = cam[0]
        self.CAY = cam[1]
        self.rect.x = self.CAX
        self.rect.y = self.CAY
        
        
class backgrounds(dict):
    def __init__(self,GAME):
        self.GAME = GAME
        

        BH = self.GAME.background_handler
        self["background_0"] = [background(0,BH[0])]
        self["background_1"] = [background(0,1,BH[3])]
        self["background_2"] = [background(0,2,BH[2])]
        self["background_3"] = [background(0,3,BH[1])]

    def get_background(self):
        self.background_0 = []
        self.background_1 = []
        self.background_2 = []
        self.background_3 = []
        
        original = self.GAME.camera.state
        half = pygame.Rect(int(original[0]/2),int(original[1]/2),self.GAME.camera.state.width,self.GAME.camera.state.height)
        quater = pygame.Rect(int(original[0]/4),int(original[1]/4),self.GAME.camera.state.height)
        eighth = pygame.Rect(int(original[0]/8),int(original[1]/8),self.GAME.camera.state.height)
        fortieth = pygame.Rect(int(original[0]/40),int(original[1]/40),self.GAME.camera.state.height)

        for i in self["background_0"]:
            
                i.apply_camera(fortieth,self.GAME)
                self.background_0.append([(i.CAX,i.CAY),(i.image)])
        
        for i in self["background_1"]:
            
                i.apply_camera(eighth,self.GAME)
                self.background_1.append([(i.CAX,(i.image)])
    
        for i in self["background_2"]:

                i.apply_camera(quater,self.GAME)
                self.background_2.append([(i.CAX,(i.image)])

        for i in self["background_3"]:
                i.apply_camera(half,self.GAME)
                self.background_3.append([(i.CAX,(i.image)])
                
        return (self.background_0),(self.background_1),(self.background_2),(self.background_3)

我需要进行哪些更改,以使其在输入新滚动条时背景将处于适当的位置,而不会明显向右移动?

注意:相机和滚动代码位于单独的模块中,如果看起来很奇怪,请不要担心

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。