Pygame相机跟随在2d平铺游戏 [英] Pygame camera follow in a 2d tile game
问题描述
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()
一切都正常,除非我想知道如何在地球上我将能够移动相机的玩家对不起,我不能显示你的地图文件,因为你不能添加图像到它。但感谢您的时间
Everything works fine except I was wondering how on earth I was going to be able to move the camera where the player goes sorry that I can't show you the map file as you can't add images to it. But Thanks for your time
地图在这里: https://dl.dropboxusercontent.com/u/110087275/2d%20pygame/map5.png
最后,代码在这里: https://dl.dropboxusercontent.com/u/110087275/2d%20pygame/2d_pygame.py a>
The map is here: https://dl.dropboxusercontent.com/u/110087275/2d%20pygame/map5.png And finally the code is here: https://dl.dropboxusercontent.com/u/110087275/2d%20pygame/2d_pygame.py
再次感谢您的时间和精力!!!!!
Thanks again for your time and effort!!!!!
推荐答案
一般来说,创建一个相机效果你可以尝试以下:
In general to create a "camera effect" you can try the following:
创建两个变量CameraX,CameraY(或给他们自己的名字)并且每次你在屏幕上blit的东西做以下:
create two variables "CameraX, CameraY" (or give them your own names) and everytime you blit something on the screen do the following:
screen.blit(bif,(0 -CameraX,0 -CameraY))
screen.blit(player,(x -CameraX,y -CameraY))
pygame.display.flip()
现在每次你想移动屏幕(使用相机)只需使用
Now everytime you want to move the screen (with the camera) just use
CameraX += 10 #Or any value you want
注意,如果CameraX是一个负数,并会显示错误的行为,所以考虑CameraX = 0和CameraY = 0作为地图的左上角
Note that this way does not work if CameraX is a negative number and will show buggy behaviour so consider CameraX = 0 and CameraY = 0 as the top left corner of the map
同样使用这个,你可能需要播放一点与一些其他选项,如什么时候相机移动?以及如何限制相机超过bif(您的地图)的限制
Also using this you may need to play a bit with some other options like when will the camera move? and how to limit the camera from going over the limits of "bif" (your map)
提示:
#after input inside the while:
if x > get_width /4 *3:
CameraX += 10
此代码使相机移动到如果男孩角色移动屏幕宽度的3/4以上,则right
this Code makes camera move to the "right" if the "boy" character moves more than 3/4 of the screen's width
这篇关于Pygame相机跟随在2d平铺游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!