Я все еще очень плохо знаком с Python и программированием в целом. Я хотел проверить, как работают мозаичные карты. И после понимания этого я теперь хочу знать, как плитки могут сталкиваться с другим объектом, таким как мяч или игрок. Это вообще возможно? Любые советы очень ценятся!
import pygame
# Initialize the pygame
pygame.init()
# Creating the game screen
tilesize = 80
mapwidth, mapheight = 20, 13
screen = pygame.display.set_mode((mapwidth*tilesize, mapheight*tilesize))
pygame.display.set_caption("Aspen")
WALLTOP, WALLTOP1, WALLTOP2, WALLTOP3, WALLTOP4, WALLTOPU1, WALLTOPD1, WALLTOPL1, WALLTOPR1, WALLTOPUD, WALLTOPLR, WALLBOT, GRASS = range(13)
tileimages = {
WALLTOP : pygame.transform.scale(pygame.image.load("Sprites/WallTop.png").convert(), (tilesize, tilesize)),
WALLTOP1 : pygame.transform.scale(pygame.image.load("Sprites/WallTop1.png").convert(), (tilesize, tilesize)),
WALLTOP2 : pygame.transform.scale(pygame.image.load("Sprites/WallTop2.png").convert(), (tilesize, tilesize)),
WALLTOP3 : pygame.transform.scale(pygame.image.load("Sprites/WallTop3.png").convert(), (tilesize, tilesize)),
WALLTOP4 : pygame.transform.scale(pygame.image.load("Sprites/WallTop4.png").convert(), (tilesize, tilesize)),
WALLTOPU1: pygame.transform.scale(pygame.image.load("Sprites/WallTopU1.png").convert(), (tilesize, tilesize)),
WALLTOPD1: pygame.transform.scale(pygame.image.load("Sprites/WallTopD1.png").convert(), (tilesize, tilesize)),
WALLTOPL1: pygame.transform.scale(pygame.image.load("Sprites/WallTopL1.png").convert(), (tilesize, tilesize)),
WALLTOPR1: pygame.transform.scale(pygame.image.load("Sprites/WallTopR1.png").convert(), (tilesize, tilesize)),
WALLTOPUD: pygame.transform.scale(pygame.image.load("Sprites/WallTopUD.png").convert(), (tilesize, tilesize)),
WALLTOPLR: pygame.transform.scale(pygame.image.load("Sprites/WallTopLR.png").convert(), (tilesize, tilesize)),
WALLBOT : pygame.transform.scale(pygame.image.load("Sprites/WallBot.png").convert(), (tilesize, tilesize)),
GRASS : pygame.transform.scale(pygame.image.load("Sprites/Grass.png").convert(), (tilesize, tilesize)),
}
# Tilemap
tilemap = [
[WALLTOP1, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOP2],
[WALLTOPLR, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLBOT, WALLTOPLR],
[WALLTOPLR, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOPLR],
[WALLTOPLR, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOPLR],
[WALLTOPLR, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOP1, WALLTOPU1, WALLTOP2, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOPLR],
[WALLTOPLR, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOPL1, WALLTOPR1, WALLBOT, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOPLR],
[WALLTOPLR, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLBOT, WALLBOT, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOPLR],
[WALLTOPLR, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOPLR],
[WALLTOPLR, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOPLR],
[WALLTOPLR, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOPLR],
[WALLTOPLR, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOPLR],
[WALLTOPLR, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, GRASS, WALLTOPLR],
[WALLTOP3, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOPUD, WALLTOP4],
]
# Player
playerHeight = 75
playerWidth = 33
playerImg = pygame.image.load('Sprites\Player.png')
playerImg = pygame.transform.scale(playerImg, (playerWidth, playerHeight))
playerX = 500
playerY = 400
vel = 1
moving_up = False
moving_down = False
moving_right = False
moving_left = False
def player(x, y):
screen.blit(playerImg , (playerX, playerY))
# Main loop
running = True
while running:
# Loop through the rows of the tilemap
for row in range(mapheight):
# Loop through the columns of the tilemap
for column in range(mapwidth):
# Draw the picture at the position in the map
screen.blit(tileimages[tilemap[row][column]], (column*tilesize, row*tilesize))
#Movement
if moving_up == True:
playerY -= vel
if moving_down == True:
playerY += vel
if moving_right == True:
playerX += vel
if moving_left == True:
playerX -= vel
# Border collision
if playerX < 0:
playerX += vel
if playerX > tilesize*mapwidth-playerWidth:
playerX -= vel
if playerY > tilesize*mapheight-playerHeight:
playerY -= vel
if playerY < 0:
playerY += vel
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
moving_up = True
if event.key == pygame.K_s:
moving_down = True
if event.key == pygame.K_d:
moving_right = True
if event.key == pygame.K_a:
moving_left = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_w:
moving_up = False
if event.key == pygame.K_s:
moving_down = False
if event.key == pygame.K_d:
moving_right = False
if event.key == pygame.K_a:
moving_left = False
player(playerX, playerY)
pygame.display.update()
Я прошу прощения за некоторые длинные и уродливые строки. Я все еще новичок. Любые подсказки очень нравятся!