python-3.x - 如何使用停在边缘的相机使视差滚动正常工作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,(2)*16,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,widthy,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, target, parallax_x = 1, parallax_y = 1):
self.update_scroll(self.scroll)
self.state = self.camera_func( self.state, target, parallax_x, parallax_y)
def complex_camera(camera, target_rect, parallax_x=1, parallax_y=1):
l, t, _, _ = target_rect
_, _, w, h = camera
l, t, _, _ = -l+(320), -t+(180), w, 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, t, w, h)
这是我的背景代码并将视差效果应用于它们
import pygame
class background(pygame.Rect):
def __init__(self,x,y,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,1024,512) # an additional rect object used elsewhere in another code file, ignore this
def apply_camera(self, state, GAME):
cam = GAME.camera.apply(self, state, 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,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.width, self.GAME.camera.state.height)
eighth = pygame.Rect(int(original[0]/8), int(original[1]/8), self.GAME.camera.state.width, self.GAME.camera.state.height)
fortieth = pygame.Rect(int(original[0]/40), int(original[1]/40), self.GAME.camera.state.width, 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.CAY),(i.image)])
for i in self["background_2"]:
i.apply_camera(quater,self.GAME)
self.background_2.append([(i.CAX,i.CAY),(i.image)])
for i in self["background_3"]:
i.apply_camera(half,self.GAME)
self.background_3.append([(i.CAX,i.CAY),(i.image)])
return (self.background_0), (self.background_1), (self.background_2), (self.background_3)
我需要更改什么才能使其在输入新滚动时,背景将处于适当的位置并且不会显着向右移动?
注意:相机和滚动代码在单独的模块中,如果看起来很奇怪,请不要担心
解决方案
推荐阅读
- c# - MVC 5 - 使用字符串作为模型渲染的视图
- python - SymPy 中的维度分析
- java - 我在 Android 中知道这段代码,但在 Swift 语言中获得相同的输出时遇到了麻烦
- python - 在python上的对象名称中使用变量
- python - 如何修复返回 TypeError 的 len() 命令?
- macos - Cytoscape:使用函数生成器帮助创建 IF 语句
- git - 在家和办公室交替工作的好 git 进程
- java - 在另一个单例构造函数中获取单例实例
- c# - 如何更改拉绳中文本的流动方向?
- azure - 最佳实践 - 以不同方式查询的外部参考数据的存储选项