我试图制作一款精灵自由移动并探索2d世界(背景图像)的游戏,灵感来自xkcd的点击和拖动(但使用箭头键)。我从youtuber代码中找到了这段代码。Pylet是最像我想要的:
#import math, random,
import sys
import pygame
from pygame.locals import *
W, H = 513,513
HW, HH =W/2, H/2
AREA = W*H
#initializing display
CLOCK = pygame.time.Clock()
DS = pygame.display.set_mode((W, H))
pygame.display.set_caption("astral prohjecting horizontally")
FPS = 500
def events():
for event in pygame.event.get():
if event.type==QUIT or (event.type==KEYDOWN and event.key==K_ESCAPE):
pygame.quit()
sys.exit()
#colours
BLACK = (0, 0, 0, 255)
WHITE = (255, 255, 255, 255)
bg = pygame.image.load("stage.png").convert()
bgWidth, bgHeight = bg.get_rect().size
stageWidth = bgWidth*2
stagePosX = 0
startScrollingPosX = HW
circleRadius = 25
circlePosX = circleRadius
playerPosX = circleRadius
playerPosY = 220
playerVelocityX = 0
#stageHeight = bgHeight*2
while True:
events()
k=pygame.key.get_pressed()
if k[K_RIGHT]:
playerVelocityX= 1
elif k[K_LEFT]:
playerVelocityX= -1
else:
playerVelocityX=0
playerPosX += playerVelocityX
if playerPosX > stageWidth-circleRadius:
playerPosX = stageWidth - circleRadius
if playerPosX < circleRadius:
playerPosX = circleRadius
if playerPosX < startScrollingPosX:
circlePosX = playerPosX-stageWidth
elif playerPosX > stageWidth - startScrollingPosX:
circlePosX = playerPosX-stageWidth + W
else:
circlePosX = startScrollingPosX
stagePosX += -playerVelocityX
rel_x = stagePosX % bgWidth
DS.blit(bg,(rel_x - bgWidth,0))
if rel_x < W:
DS.blit(bg, (rel_x,0))
pygame.draw.circle(DS, WHITE, (circlePosX, playerPosY - circleRadius), circleRadius, 0)
pygame.display.update()
CLOCK.tick(FPS)
DS.fill(BLACK)
然而,它只在从左到右和向后移动时起作用,当我试图复制并更改所有变量到它们的垂直版本时,结果笨拙地上下移动。即:
import sys
import pygame
from pygame.locals import *
W, H = 513,513
HW, HH =W/2, H/2
AREA = W*H
#initializing display
CLOCK = pygame.time.Clock()
DS = pygame.display.set_mode((W, H))
pygame.display.set_caption("astral prohjecting horizontally")
FPS = 500
def events():
for event in pygame.event.get():
if event.type==QUIT or (event.type==KEYDOWN and event.key==K_ESCAPE):
pygame.quit()
sys.exit()
#colours
BLACK = (0, 0, 0, 255)
WHITE = (255, 255, 255, 255)
bg = pygame.image.load("stage.png").convert()
bgWidth, bgHeight = bg.get_rect().size
stageWidth = bgWidth*2
stageHeight = bgHeight*2
stagePosX = 0
stagePosY = 0
startScrollingPosX = HW
startScrollingPosY = HH
circleRadius = 25
circlePosX = circleRadius
circlePosY = circleRadius
playerPosX = circleRadius
playerPosY = circleRadius
#playerPosY = 2308.5
playerVelocityX = 0
playerVelocityY=0
while True:
events()
k=pygame.key.get_pressed()
if k[K_RIGHT]:
playerVelocityX= 1
elif k[K_LEFT]:
playerVelocityX= -1
else:
playerVelocityX=0
if k[K_UP]:
playerVelocityY = -1
elif k[K_DOWN]:
playerVelocityY = 1
else:
playerVelocityY = 0
playerPosX += playerVelocityX
if playerPosX > stageWidth-circleRadius:
playerPosX = stageWidth - circleRadius
if playerPosX < circleRadius:
playerPosX = circleRadius
if playerPosX < startScrollingPosX:
circlePosX = playerPosX-stageWidth
elif playerPosX > stageWidth - startScrollingPosX:
circlePosX = playerPosX-stageWidth + W
else:
circlePosX = startScrollingPosX
stagePosX += -playerVelocityX
playerPosY += playerVelocityY
if playerPosY > stageHeight -circleRadius:
playerPosY = stageHeight - circleRadius
if playerPosY < circleRadius:
playerPosY = circleRadius
if playerPosY < startScrollingPosY:
circlePosY = playerPosY - stageHeight
elif playerPosY > stageHeight - startScrollingPosY:
circlePosY = playerPosY - stageHeight + H
else:
circlePosY = startScrollingPosY
stagePosY += -playerVelocityY
rel_x = stagePosX % bgWidth
DS.blit(bg,(rel_x - bgWidth,0))
if rel_x < W:
DS.blit(bg, (rel_x,0))
rel_y = stagePosY % bgHeight
DS.blit(bg,(0,(rel_y - bgHeight)))
if rel_y < H:
DS.blit(bg,(0, rel_y))
#stagePosY += 1
pygame.draw.circle(DS, BLACK, (circlePosX, playerPosY+circleRadius), circleRadius, 0)
pygame.display.update()
CLOCK.tick(FPS)
DS.fill(BLACK)
我不知道错误在哪里;任何帮助都是感激的。谢谢你。
当您在相似的坐标处对图像进行多次blit时,图像会堆叠在彼此的顶部,因此您只能看到最后一个。
你的错误是由于没有考虑到组合的两个维度来对舞台图片进行blit。
这里是blit部分,照顾它,也正确显示在角落的4张图片:
rel_x = stagePosX % bgWidth
rel_y = stagePosY % bgHeight
DS.blit(bg,(rel_x - bgWidth,rel_y - bgHeight))
if rel_x < W:
DS.blit(bg, (rel_x,rel_y - bgHeight))
if rel_y < H:
DS.blit(bg,(rel_x, rel_y))
if rel_y < H:
DS.blit(bg,(rel_x - bgWidth, rel_y))