Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random, pygame, sys
- from pygame.locals import *
- fps = 30
- window_width = 704
- window_height = 704
- fpsClock = pygame.time.Clock()
- map_width = 30
- map_height = 15
- green = (100, 255, 0)
- display_surf = pygame.display.set_mode((window_height, window_width), 0, 32)
- pygame.display.set_caption("Rl")
- class Object:
- # this is a generic object: the player, a monster, an item, the stairs...
- # it's always represented by a character on screen.
- def __init__(self, x, y, image):
- self.x = x
- self.y = y
- self.image = pygame.image.load(image)
- def move(self, dx, dy):
- if find_title(world[self.y+dy][self.x+dx]).solid is False: #this line checks if move is possible
- self.x += dx
- self.y += dy
- def draw(self):
- display_surf.blit(self.image, (self.x, self.y))
- class Title:
- # this is a class for titles like grass, road and so on
- def __init__(self, title_number, solid, image):
- self.title_number = title_number
- self.solid = solid
- self.image = pygame.image.load(image)
- def find_title(n):
- if n == 0:
- return nothing
- if n == 1:
- return grass
- if n == 2:
- return fance
- def world_gen():
- camera_x = scrolling_map(hero.x, 5, 10, map_width)
- camera_y = scrolling_map(hero.y, 5, 10, map_height)
- for x in range(camera_x, camera_x+11):
- for y in range(camera_y, camera_y+11):
- if world[y][x] != 0:
- display_surf.blit(find_title(world[y][x]).image, (64 * x, 64 * y))
- display_surf.blit(hero.image, (64 * 5, 64 * 5))
- def scrolling_map(p, hs, s, m):
- """
- Get the position of the camera in a scrolling map:
- - p is the position of the player.
- - hs is half of the screen size, and s is the full screen size.
- - m is the size of the map.
- """
- if p < hs:
- return 0
- elif p >= m - hs:
- return m - s
- else:
- return p - hs
- hero = Object(5, 5, "hero.png")
- grass = Title(1, False, "grass.png")
- fance = Title(2, True, "fance.png")
- nothing = Title(0, False, "road.png")
- world = [[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 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0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
- [2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
- while True:
- display_surf.fill(green)
- for event in pygame.event.get():
- if event.type == QUIT:
- pygame.quit()
- sys.exit()
- if event.type == KEYDOWN:
- if event.key == pygame.K_LEFT:
- if hero.x - 1 >= 0:
- hero.move(-1, 0)
- if event.key == pygame.K_RIGHT:
- if hero.x + 1 < map_width:
- hero.move(1, 0)
- if event.key == pygame.K_UP:
- if hero.y - 1 >= 0:
- hero.move(0, -1)
- if event.key == pygame.K_DOWN:
- if hero.y + 1 < map_height:
- hero.move(0, 1)
- world_gen()
- pygame.display.update()
- pygame.time.delay(50)
- fpsClock.tick(fps)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement