main file (main.py):
import pygame import normalroom import im pygame.init() WindowWidth = 800 WindowHeight = 600 win = pygame.display.set_mode((WindowWidth, WindowHeight)) display = pygame.Surface((400, 300)) rx = 536.5 ry = 85 scroll = [0, 0] class Player: def __init__(self, x, y, width, height): self.px = x self.py = y self.width = width self.height = height self.rect = pygame.Rect(x, y, width, height) self.vel = 5 self.walkCount = 0 self.left = False self.right = False self.up = False self.down = False def draw(self, display): walkLeft = [im.RL1, im.RL2, im.RL3, im.RL4, im.RL5, im.RL6, im.RL7, im.RL8, im.RL9] walkRight = [im.RR1, im.RR2, im.RR3, im.RR4, im.RR5, im.RR6, im.RR7, im.RR8, im.RR9] walkUp = [im.RU1, im.RU2, im.RU3, im.RU4, im.RU5, im.RU6, im.RU7, im.RU8, im.RU9] walkDown = [im.RD1, im.RD2, im.RD3, im.RD4, im.RD5, im.RD6, im.RD7, im.RD8, im.RD9] char = [im.RS1, im.RS1, im.RS2, im.RS2, im.RS1, im.RS1, im.RS2, im.RS2, im.RS1] if self.walkCount + 1 >= 9: self.walkCount = 0 if self.left: display.blit(walkLeft[self.walkCount], (self.px - scroll[0], self.py - scroll[1])) self.walkCount += 1 elif self.right: display.blit(walkRight[self.walkCount], (self.px - scroll[0], self.py - scroll[1])) self.walkCount += 1 elif self.down: display.blit(walkDown[self.walkCount], (self.px - scroll[0], self.py - scroll[1])) self.walkCount += 1 elif self.up: display.blit(walkUp[self.walkCount], (self.px - scroll[0], self.py - scroll[1])) self.walkCount += 1 else: display.blit(char[self.walkCount//3], (self.px - scroll[0], self.py - scroll[1])) self.walkCount += 1 def move(self): keys = pygame.key.get_pressed() if keys[pygame.K_a]: self.px -= self.vel self.left = True else: self.left = False if keys[pygame.K_d]: self.px += self.vel self.right = True else: self.right = False if keys[pygame.K_w]: self.py -= self.vel self.up = True else: self.up = False if keys[pygame.K_s]: self.py += self.vel self.down = True else: self.down = False self.rect = pygame.Rect(self.px - scroll[0], self.py - scroll[1], self.width, self.height) scroll[0] += (self.px - scroll[0] - 192)/10 scroll[1] += (self.py - scroll[1] - 142)/10 def redrawWindow(display, player): display.fill((100, 100, 100)) tile_rects = [] y = 0 for row in normalroom.room: x = 0 for tile in row: if tile == 0: pass elif tile == 1: display.blit(im.Ground, (x * 64 - scroll[0], y * 64 - scroll[1])) x += 1 y += 1 player.draw(display) pygame.display.update() def main(): run = True p = Player(rx, ry, 16, 16) clock = pygame.time.Clock() while run: clock.tick(25) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False pygame.quit() p.move() win.blit(pygame.transform.scale(display, (WindowWidth, WindowHeight)), (0, 0)) redrawWindow(display, p) main()
image file (im.py):
import pygame RS1 = pygame.image.load("RS1.png") RS2 = pygame.image.load("RS2.png") RR1 = pygame.image.load("RR1.png") RR2 = pygame.image.load("RR2.png") RR3 = pygame.image.load("RR3.png") RR4 = pygame.image.load("RR4.png") RR5 = pygame.image.load("RR5.png") RR6 = pygame.image.load("RR6.png") RR7 = pygame.image.load("RR7.png") RR8 = pygame.image.load("RR8.png") RR9 = pygame.image.load("RR9.png") RL1 = pygame.transform.flip(RR1, True, False) RL2 = pygame.transform.flip(RR2, True, False) RL3 = pygame.transform.flip(RR3, True, False) RL4 = pygame.transform.flip(RR4, True, False) RL5 = pygame.transform.flip(RR5, True, False) RL6 = pygame.transform.flip(RR6, True, False) RL7 = pygame.transform.flip(RR7, True, False) RL8 = pygame.transform.flip(RR8, True, False) RL9 = pygame.transform.flip(RR9, True, False) RD1 = pygame.image.load("RD1.png") RD2 = pygame.image.load("RD2.png") RD3 = pygame.image.load("RD3.png") RD4 = pygame.image.load("RD4.png") RD5 = pygame.image.load("RD5.png") RD6 = pygame.image.load("RD6.png") RD7 = pygame.image.load("RD7.png") RD8 = pygame.image.load("RD8.png") RD9 = pygame.image.load("RD9.png") RU1 = pygame.image.load("RU1.png") RU2 = pygame.image.load("RU2.png") RU3 = pygame.image.load("RU3.png") RU4 = pygame.image.load("RU4.png") RU5 = pygame.image.load("RU5.png") RU6 = pygame.image.load("RU6.png") RU7 = pygame.image.load("RU7.png") RU8 = pygame.image.load("RU8.png") RU9 = pygame.image.load("RU9.png") Ground = pygame.image.load("Ground.png")
and the tilemap (normalroom.py):
room = [[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0]]
Now I want the player (Robot) to be able to only walk when he is on the platform/ground and not if he is off of it as the rest is the planet and he only can stay in the base. So he can only walk when he is on the ground (1's in tilemap(64x64)). I hope you can help. I can give you more detail if you need