python - How can I create a 2-d scrolling tiled map in pygame? -
i creating rpg game, however, have absolutely no idea how scroll through map player in centre. other suggestions may have appreciated too! help! here's code:
import pygame pytmx import load_pygame pygame.init() transparent = (0,0,0,0) black = (0,0,0) white = (255,255,255) x,y = 0,0 movespeed = 5 #create window screensize = (800,600) screen = pygame.display.set_mode(screensize) pygame.display.set_caption("frozen") gamemap = load_pygame("frozen.tmx") def getmap(layer): #creates list of single tiles images = [] y in range(50): x in range(50): image = gamemap.get_tile_image(x,y,layer) images.append(image) #displays tiles in locations = 0 y in range(50): x in range(50): screen.blit(images[i],(x*32,y*32)) += 1 def getproperties(): #gets properties of tiles in map properties = [] y in range(50): x in range(50): tileproperties = gamemap.get_tile_properties(x,y,3) properties.append(tileproperties) return properties def createboxes(): #creates collision rectangles unpassable tiles nowalklocs = [] rects = [] = 0 testsurf = pygame.surface(screensize).convert_alpha() testsurf.fill(transparent) y in range(50): x in range(50): tileproperties = properties[i] walkable = tileproperties.get("walkable") if walkable == "false": rect = pygame.draw.rect(testsurf,(0,0,0, 0),(x*32,y*32,32,32)) rects.append(rect) += 1 return rects def getcollisions(): global x global y collisionloc = player.rect.collidelist(rects) collision = rects[collisionloc] if player.rect.collidepoint(collision.bottomleft): if x > 0 , y < 0: player.rect.topright = collision.bottomleft x,y = 0, 0 elif x > 0: player.rect.right = collision.left x =0 elif y < 0: player.rect.top = collision.bottom y = 0 elif player.rect.collidepoint(collision.topleft): if x > 0 , y > 0: player.rect.bottomright = collision.topleft x,y = 0, 0 elif x > 0: player.rect.right = collision.left x =0 elif y > 0: player.rect.bottom = collision.top y = 0 elif player.rect.collidepoint(collision.bottomright): if x < 0 , y <0: player.rect.topleft = collision.bottomright x,y = 0, 0 elif x < 0: player.rect.left = collision.right x =0 elif y < 0: player.rect.top = collision.bottom y = 0 elif player.rect.collidepoint(collision.topright): if x < 0 , y > 0: player.rect.bottomleft = collision.topright x,y = 0, 0 elif x < 0: player.rect.left = collision.right x =0 elif y > 0: player.rect.bottom = collision.top y = 0 elif player.rect.collidepoint(collision.midbottom): player.rect.top = collision.bottom y = 0 elif player.rect.collidepoint(collision.midtop): player.rect.bottom = collision.top y = 0 elif player.rect.collidepoint(collision.midright): player.rect.left = collision.right x = 0 elif player.rect.collidepoint(collision.midleft): player.rect.right = collision.left x = 0 return player.rect.x, player.rect.y, x, y class sprite(pygame.sprite.sprite): def __init__(self,x,y): pygame.sprite.sprite.__init__(self) self.x,self.y = x,y self.image = standfront self.rect = self.image.get_rect() self.frontimages = [standfront,walkfront1,walkfront2,walkfront3] self.backimages = [standback,walkback1,walkback2,walkback3] self.leftimages = [standleft,walkleft1,walkleft2,walkleft3] self.rightimages = [standright,walkright1,walkright2,walkright3] self.index = 0 def walkfront(self): self.index += 1 if self.index >= len(self.frontimages): self.index=0 self.image = self.frontimages[self.index] def walkback(self): self.index += 1 if self.index >= len(self.backimages): self.index=0 self.image = self.backimages[self.index] def walkleft(self): self.index += 1 if self.index >= len(self.leftimages): self.index=0 self.image = self.leftimages[self.index] def walkright(self): self.index += 1 if self.index >= len(self.rightimages): self.index=0 self.image = self.rightimages[self.index] def render(self): screen.blit(self.image,(self.x,self.y)) self.rect.x, self.rect.y = self.x, self.y #loads images standfront = pygame.image.load("images/elsa sprite/walkfront/standfront.png").convert_alpha() walkfront1 = pygame.image.load("images/elsa sprite/walkfront/walkfront1.png").convert_alpha() walkfront2 = pygame.image.load("images/elsa sprite/walkfront/walkfront2.png").convert_alpha() walkfront3 = pygame.image.load("images/elsa sprite/walkfront/walkfront3.png").convert_alpha() standback = pygame.image.load("images/elsa sprite/walkback/standback.png").convert_alpha() walkback1 = pygame.image.load("images/elsa sprite/walkback/walkback1.png").convert_alpha() walkback2 = pygame.image.load("images/elsa sprite/walkback/walkback2.png").convert_alpha() walkback3 = pygame.image.load("images/elsa sprite/walkback/walkback3.png").convert_alpha() standleft = pygame.image.load("images/elsa sprite/walkleft/standleft.png").convert_alpha() walkleft1 = pygame.image.load("images/elsa sprite/walkleft/walkleft1.png").convert_alpha() walkleft2 = pygame.image.load("images/elsa sprite/walkleft/walkleft2.png").convert_alpha() walkleft3 = pygame.image.load("images/elsa sprite/walkleft/walkleft3.png").convert_alpha() standright = pygame.image.load("images/elsa sprite/walkright/standright.png").convert_alpha() walkright1 = pygame.image.load("images/elsa sprite/walkright/walkright1.png").convert_alpha() walkright2 = pygame.image.load("images/elsa sprite/walkright/walkright2.png").convert_alpha() walkright3 = pygame.image.load("images/elsa sprite/walkright/walkright3.png").convert_alpha() #creates tick clock = pygame.time.clock() #creates player player = sprite(250, 300) properties = getproperties() rects = createboxes() #main loop running = true while running: event in pygame.event.get(): if event.type == pygame.quit: running = false #gets keypresses if event.type == pygame.keydown: if event.key == pygame.k_left: x, y = 0, 0 x -= movespeed elif event.key == pygame.k_right: x, y = 0, 0 x += movespeed elif event.key == pygame.k_up: x, y = 0, 0 y -= movespeed elif event.key == pygame.k_down: x, y = 0, 0 y += movespeed if event.type == pygame.keyup: if event.key == pygame.k_left: x = 0 player.image = player.leftimages[0] elif event.key == pygame.k_right: x = 0 player.image = player.rightimages[0] elif event.key == pygame.k_up: y = 0 player.image = player.backimages[0] elif event.key == pygame.k_down: y = 0 player.image = player.frontimages[0] player.x += x player.y += y if y > 0: player.walkfront() if y < 0: player.walkback() if x > 0: player.walkright() if x < 0: player.walkleft() getmap(0) getmap(1) getmap(2) player.render() pygame.display.flip() player.rect.x, player.rect.y, x, y = getcollisions() player.x, player.y = player.rect.x, player.rect.y clock.tick(10) pygame.quit()
you use pygames .scroll()
method. instead of changing x
, y
coordinates of player
, call tieldmap.scroll()
function , pass new x
, y
(i.e. mx
, my
) values.
to need two surfaces, main screen
surface (you have), , can "scroll" (e.g. tieldmap
).
add lines somewhere after pygame.init()
function call:
mapsize = (120,120) #change values tieldmap = pygame.surface(mapsize) mx, = 0, 0
and change code in getmap()
function (where blit tiles onto main screen
) to:
#displays tiles in locations = 0 y in range(50): x in range(50): tieldmap.blit(images[i],(x*32,y*32)) += 1
now can "scroll" tieldmap
surface in main game loop:
change lines
player.x += x player.y += y
to
mx += x += y
add 2 lines create copy called
_tieldmap
oftieldmap
surface (.copy
) , call.scroll
function._tieldmap = tieldmap.copy() _tieldmap.scroll(xm, ym)
finally blit in main game loop tieldmap
surface onto screen
@ (0,0)
, update it:
screen.blit(_tieldmap,(0,0)) pygame.display.flip()
important: must check if xm
or ym
smaller or grater image height or width!
i hope helps :)
Comments
Post a Comment