import pygame from pygame.locals import * import os pygame.init() screen_w, screen_h = 1000, 1000 screen = pygame.display.set_mode((screen_w, screen_h)) pygame.display.set_caption("PlatFormer :3") # Get the directory of the current script base_path = os.path.dirname(__file__) # Function to load an image def load_image(image_name): return pygame.image.load(os.path.join(base_path, 'img', image_name)) bg_img = load_image('bg.png') sun_img = load_image('sun.png') # Variable tile_size = 50 def draw_grid(): for line in range(0, 21): pygame.draw.line(screen, (255, 255, 255), (0, line * tile_size), (screen_w, line * tile_size)) pygame.draw.line(screen, (255, 255, 255), (line * tile_size, 0), (line * tile_size, screen_w)) class Player(): def __init__(self, x, y): img = load_image('guy.png') self.image = pygame.transform.scale(img, (40, 80)) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.vel_y = 0 self.width = self.image.get_width() self.height = self.image.get_height() self.jumped = False self.jump_key_pressed = False def update(self): dx = 0 dy = 0 key = pygame.key.get_pressed() if key[pygame.K_a] or key[pygame.K_LEFT]: dx -= 6 if key[pygame.K_d] or key[pygame.K_RIGHT]: dx += 6 if (key[pygame.K_SPACE] or key[pygame.K_UP]) and not self.jumped and not self.jump_key_pressed: self.vel_y = -15 self.jumped = True self.jump_key_pressed = True print("Jumped") if not key[pygame.K_SPACE] and not key[pygame.K_UP]: self.jump_key_pressed = False self.vel_y += 1 if self.vel_y > 10: self.vel_y = 10 dy += self.vel_y for tile in world.tile_list: if tile[1].colliderect(self.rect.x + dx, self.rect.y, self.width, self.height): dx = 0 if tile[1].colliderect(self.rect.x, self.rect.y + dy, self.width, self.height): if self.vel_y < 0: dy = tile[1].bottom - self.rect.top self.vel_y = 0 elif self.vel_y >= 0: dy = tile[1].top - self.rect.bottom self.vel_y = 0 self.jumped = False self.rect.x += dx self.rect.y += dy if self.rect.bottom > screen_h: self.rect.bottom = screen_h self.vel_y = 0 self.jumped = False if self.rect.top < 0: self.rect.top = 0 self.vel_y = 0 if self.rect.right > screen_w: self.rect.right = screen_w self.vel_y = 0 if self.rect.left < 0: self.rect.left = 0 self.vel_y = 0 screen.blit(self.image, self.rect) class World(): def __init__(self, data): self.tile_list = [] dirt_img = load_image('dirt.png') grass_img = load_image('grass.png') row_count = 0 for row in data: col_count = 0 for tile in row: if tile == 1: img = pygame.transform.scale(dirt_img, (tile_size, tile_size)) img_rect = img.get_rect() img_rect.x = col_count * tile_size img_rect.y = row_count * tile_size tile = (img, img_rect) self.tile_list.append(tile) if tile == 2: img = pygame.transform.scale(grass_img, (tile_size, tile_size)) img_rect = img.get_rect() img_rect.x = col_count * tile_size img_rect.y = row_count * tile_size tile = (img, img_rect) self.tile_list.append(tile) if tile == 3: blob = Enemy(col_count * tile_size, row_count * tile_size) blob_group.add(blob) col_count += 1 row_count += 1 def draw(self): for tile in self.tile_list: screen.blit(tile[0], tile[1]) class Enemy(pygame.sprite.Sprite): def __init__(self, x, y): pygame.sprite.Sprite.__init__(self) slime = load_image('slime.png') self.image = slime self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.move_direction = 1 self.move_counter = 0 def update(self): self.rect.x += self.move_direction self.move_counter -= -1 if self.move_counter > 50: self.move_direction *= -1 self.move_counter = 0 blob_group = pygame.sprite.Group() world_data = [ [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,0,0,0,0,0,3,0,0,3,0,0,0,0,0,2,2,1], [1,0,0,0,0,0,0,2,2,2,2,2,2,0,0,0,0,0,1,1], [1,0,0,0,0,2,2,1,0,0,0,0,1,2,2,0,0,0,0,1], [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,0,2,0,0,0,2,0,0,2,0,0,0,0,0,0,0,1], [1,0,0,0,1,2,0,0,1,0,0,0,0,0,2,0,0,0,0,1], [1,0,0,0,1,1,2,2,1,0,0,0,0,0,0,0,0,0,0,1], [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,0,1], [1,0,0,0,0,0,0,0,0,0,0,0,3,0,0,2,0,1,0,1], [1,0,0,0,0,0,0,0,0,0,2,2,2,2,2,1,0,1,0,1], [2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,2,2,2,2], ] world = World(world_data) player = Player(100, screen_h - 130) run = True while run: screen.blit(bg_img, (0, 0)) draw_grid() world.draw() player.update() blob_group.update() blob_group.draw(screen) screen.blit(sun_img, (100, 100)) for event in pygame.event.get(): if event.type == pygame.QUIT: print("Clicked X button to exit") run = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: print("Exit game through K_ESCAPE") run = False pygame.display.update() pygame.quit()