【问题标题】:Pygame camera follow in a 2d tile game [duplicate]Pygame相机跟随二维瓷砖游戏[重复]
【发布时间】:2023-04-01 08:59:02
【问题描述】:
 import pygame, sys 
 from pygame.locals import * 

 pygame.init()

 size = width, height = 480,320
 screen = pygame.display.set_mode(size)
 r = 0
 bif = pygame.image.load("map5.png") 
 pygame.display.set_caption("Pygame 2D RPG !")
 x,y=0,0
 movex, movey=0,0
 character="boy.png"
 player=pygame.image.load(character).convert_alpha()
 while True:
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        pygame.quit()
        sys.exit()
    if event.type==KEYDOWN:
        if event.key==K_a:
            movex=-1
        elif event.key==K_d:
            movex=+1
        elif event.key==K_w:
            movey=-1
        elif event.key==K_s:
            movey=+1
    if event.type==KEYUP:        
        if event.key==K_a:
            movex=0
        elif event.key==K_d:
            movex=0
        elif event.key==K_w:
            movey=0
        elif event.key==K_s:
            movey=0    

    x+=movex
    y+=movey    

    screen.fill((r,0,0))
    screen.blit(bif,(0,0))
    screen.blit(player,(x,y))
    pygame.display.flip()

一切都很好,除了我想知道我到底如何才能将相机移动到玩家所在的位置抱歉,我无法向您显示地图文件,因为您无法向其中添加图像。但是感谢您的宝贵时间

地图在这里:https://dl.dropboxusercontent.com/u/110087275/2d%20pygame/map5.png 最后代码在这里:https://dl.dropboxusercontent.com/u/110087275/2d%20pygame/2d_pygame.py

再次感谢您的时间和精力!!!!!

【问题讨论】:

标签: python camera 2d pygame


【解决方案1】:

一般来说,要创建“相机效果”,您可以尝试以下方法:

创建两个变量“CameraX,CameraY”(或给他们自己的名字),每次你在屏幕上进行 blit 时,请执行以下操作:

screen.blit(bif,(0 -CameraX,0 -CameraY))
screen.blit(player,(x -CameraX,y -CameraY))
pygame.display.flip()

现在每次你想移动屏幕(用相机)只需使用

CameraX += 10 #Or any value you want

请注意,如果 CameraX 为负数,这种方式不起作用,并且会显示错误行为,因此请考虑将 CameraX = 0 和 CameraY = 0 作为地图的左上角

同样使用它,您可能需要尝试一些其他选项,例如相机何时移动?以及如何限制相机超出“bif”(您的地图)的限制

提示:

#after input inside the while:
if x > get_width /4 *3:
    CameraX += 10

如果“男孩”角色移动超过屏幕宽度的 3/4,则此代码使相机向右移动

【讨论】:

  • 什么是bif?它对我来说显示为错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-06
  • 2011-10-17
  • 2013-01-26
相关资源
最近更新 更多