我希望能够移动2D游戏的地图和角色,我正在通过以下操作制作图块:
for x in range(20):
for y in range(20):
pygame.draw.rect(screen, white, (x*32,y*32,32,32), 2)
并移动地图,我会y + = 32,但这不起作用,地图只会保持原样。因此,如何使用上面的代码移动地图!我的问题与标记的问题不同,因为我只是想在玩家使用上述代码按下W,A,S或D时简单地移动地图!我尝试将图块的宽度和高度dd乘以x和y的整数,即32像素,但是没有出现某些框。
这是我的完整代码: Main.py
import pygame, math
from scripts.Engine import *
black = (0,0,0)
white = (255,255,255)
blue = (0,0,255)
pygame.init()
size = (600,600)
width = size[0]
height = size[1]
screen = pygame.display.set_mode((size), pygame.RESIZABLE)
face = "North"
playerN = pygame.image.load("Sprites\\Uplayer.png")
playerS = pygame.image.load("Sprites\\Dplayer.png")
playerE = pygame.image.load("Sprites\\Rplayer.png")
playerW = pygame.image.load("Sprites\\Lplayer.png")
grass = pygame.image.load("Tiles\\grass.png")
camera_x = 0
camera_y = 0
TileSize = 32
player_w, player_h = 40, 52
player_x = (width / 2 - player_w / 2 - camera_x) / TileSize
player_y = (height / 2 - player_h / 2 - camera_y) / TileSize
while True:
screen.fill(blue)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
Engine.Map(screen, 20, 20, grass, 32)
move = pygame.key.get_pressed()
if move[pygame.K_w]:
face = "North"
camera_y -= 32 # Move map This doesnt work
player_y -= 10 # Move Sprite around map This works
elif move[pygame.K_s]:
camera_y += 32
player_y += 10
face = "South"
elif move[pygame.K_a]:
face = "West"
camera_x -= 32
player_x -= 10
elif move[pygame.K_d]:
face = "East"
camera_x += 32
player_x += 10
if face == "North":
screen.blit(playerN, (player_x, player_y))
elif face == "South":
screen.blit(playerS, (player_x, player_y))
elif face == "East":
screen.blit(playerE, (player_x, player_y))
elif face == "West":
screen.blit(playerW, (player_x, player_y))
pygame.display.flip()
游戏引擎:在此制作的图块
import pygame
class Engine:
def Map(surface, w, h, tileType,TileSize):
global x,y
for x in range(w):
for y in range(h):
surface.blit(tileType, (x*TileSize,y*TileSize))
到目前为止,这是我的完整代码,我可以移动播放器,但不能移动磁贴本身,我不知道为什么
答案 0 :(得分:0)
我已经弄清楚了,但是当我向上或向左移动时,地图会在当前地图数量之后继续生成,但地图数量为40,但是它将继续创建图块。但是当我向下或向右走时,到达最后一个图块时,它会在图块后面显示蓝屏。我会发布另一个问题来解决该问题。
Main.py:
import pygame, math
from scripts.Engine import *
black = (0,0,0)
white = (255,255,255)
blue = (0,0,255)
pygame.init()
size = (600,600)
width = size[0]
height = size[1]
screen = pygame.display.set_mode((size), pygame.RESIZABLE)
face = "North"
playerN = pygame.image.load("Sprites\\Uplayer.png")
playerS = pygame.image.load("Sprites\\Dplayer.png")
playerE = pygame.image.load("Sprites\\Rplayer.png")
playerW = pygame.image.load("Sprites\\Lplayer.png")
grass = pygame.image.load("Tiles\\grass.png")
TileSize = 32
player_w, player_h = 40, 52
player_x = (width / 2 - player_w / 2 - camera_x) / TileSize
player_y = (height / 2 - player_h / 2 - camera_y) / TileSize
mapW = camera_x
mapH = camera_y
while True:
print(camera_x, camera_y)
screen.fill(blue)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
Engine.Map(screen, camera_x, camera_y, grass, 32, camera_x, camera_y)
move = pygame.key.get_pressed()
if move[pygame.K_w]:
if camera_x > mapH:
pass
else:
face = "North"
camera_y += 1 # Move map This doesnt work
elif move[pygame.K_s]:
camera_y -= 1
face = "South"
elif move[pygame.K_a]:
face = "West"
if camera_x > mapW:
pass
else:
camera_x += 1
elif move[pygame.K_d]:
face = "East"
camera_x -= 1
#player_x += 10
if face == "North":
screen.blit(playerN, (player_x, player_y))
elif face == "South":
screen.blit(playerS, (player_x, player_y))
elif face == "East":
screen.blit(playerE, (player_x, player_y))
elif face == "West":
screen.blit(playerW, (player_x, player_y))
pygame.display.flip()
Game Engine.py:
import pygame
class Engine:
def Map(surface, w, h, tileType,TileSize, camera_x, camera_y):
for x in range(w):
for y in range(h):
surface.blit(tileType, (x*TileSize,y*TileSize))