Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import itertools
- from pygame.sprite import Group, Sprite
- map_data = [
- [ 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,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
- [ 1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,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],
- [ 1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,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],
- [ 1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,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],
- [ 1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,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],
- [ 1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,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],
- [ 1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,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],
- [ 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0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 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],
- [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 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],
- ]
- class Engine:
- def __init__(self, caption, width, height, flags=0):
- pygame.display.set_caption(caption)
- self.surface = pygame.display.set_mode((width, height), flags)
- self.rect = self.surface.get_rect()
- self.clock = pygame.time.Clock()
- self.running = False
- self.delta = 0
- self.fps = 60
- def idle(self):
- self.delta = self.clock.tick(self.fps)
- class Point:
- def __init__(self, x, y):
- self.x = x
- self.y = y
- def __add__(self, rhs):
- return Point(self.x + rhs.x, self.y + rhs.y)
- def __sub__(self, rhs):
- return Point(self.x - rhs.x, self.y - rhs.y)
- def __mul__(self, rhs):
- return Point(self.x * rhs.x, self.y * rhs.y)
- def __truediv__(self, rhs):
- return Point(self.x / rhs.x, self.y / rhs.y)
- def __floordiv__(self, rhs):
- return Point(self.x // rhs.x, self.y // rhs.y)
- def __iter__(self):
- yield self.x
- yield self.y
- class MovementKeys:
- def __init__(self, up, down, left, right):
- self.up = up
- self.down = down
- self.left = left
- self.right = right
- class Player(Sprite):
- def __init__(self, image, movement_keys, position, anchor="topleft"):
- Sprite.__init__(self)
- self.image = image
- self.rect = image.get_rect(**{anchor: position})
- self.center = pygame.Vector2(self.rect.center)
- self.speed = 8 * 0.01
- self.key = movement_keys
- def get_position(self, size):
- return Point(int(self.center.x // size.x), int(self.center.y // size.y))
- def update(self, delta, keys_pressed, grid):
- direction = pygame.Vector2(0, 0)
- if keys_pressed[self.key.up]:
- direction.y -= 1
- if keys_pressed[self.key.down]:
- direction.y += 1
- if keys_pressed[self.key.left]:
- direction.x -= 1
- if keys_pressed[self.key.right]:
- direction.x += 1
- if direction != pygame.Vector2(0, 0):
- direction.normalize_ip()
- old_center = pygame.Vector2(self.center)
- self.center += direction * self.speed * delta
- self.rect.center = self.center
- # Collision
- if grid.collide(self):
- self.center = old_center
- self.rect.center = self.center
- class Grid:
- def __init__(self, screen_size, block_size):
- self.block_range = Point(int(screen_size.x / block_size.x) + 1,
- int(screen_size.y / block_size.y) + 1)
- self.block_size = block_size
- self.position = Point(0, 0)
- def collide(self, player):
- rects = self.get_rects(player.get_position(self.block_size))
- for rect in rects:
- if rect.colliderect(player.rect):
- return True
- return False
- def collide_list(self, player):
- rect_list = []
- rects = self.get_rects(player.get_position(self.block_size))
- for rect in rects:
- if rect.colliderect(player.rect):
- rect_list.append(rect)
- return rect_list
- def get_rects(self, position):
- rect = pygame.Rect(0, 0, *self.block_size)
- rect_list = []
- for x, y in itertools.product(range(position.x - 1, position.x + 2), range(position.y - 1, position.y + 2)):
- try:
- if map_data[y][x] == 1:
- rect.topleft = x * self.block_size.x, y * self.block_size.y
- rect_list.append(rect.copy())
- except:
- pass
- return rect_list
- def draw(self, surface):
- rect = pygame.Rect(0, 0, *self.block_size)
- color = pygame.Color('gray30')
- for x, y in itertools.product(range(self.position.x, self.block_range.x), range(self.position.y, self.block_range.y)):
- try:
- if map_data[y][x] == 1:
- rect.topleft = x * self.block_size.x, y * self.block_size.y
- surface.fill(color, rect)
- except:
- pass
- class GameVariables:
- def __init__(self, engine):
- self.engine = engine
- self.sprites = Group()
- self.block_size = Point(32, 32)
- self.player_size = Point(24, 24)
- self.grid = Grid(Point(*engine.rect.size), self.block_size)
- self.player_blue = self.create_player(pygame.Color('dodgerblue'), engine.rect.center, "center")
- self.sprites.add(self.player_blue)
- def create_player(self, color, position, anchor):
- surface = pygame.Surface(tuple(self.player_size))
- surface.fill(pygame.Color('dodgerblue'))
- movement_keys = MovementKeys(pygame.K_w, pygame.K_s, pygame.K_a, pygame.K_d)
- return Player(surface, movement_keys, position, anchor)
- def main():
- pygame.init()
- engine = Engine("Player Movement And Collision", 500, 500)
- background = pygame.Color('black')
- game = GameVariables(engine)
- engine.running = True
- while engine.running:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- engine.running = False
- # Logic
- keys_pressed = pygame.key.get_pressed()
- game.sprites.update(engine.delta, keys_pressed, game.grid)
- # Drawing
- engine.surface.fill(background)
- game.grid.draw(engine.surface)
- game.sprites.draw(engine.surface)
- pygame.display.update()
- # Sleeping
- engine.idle()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement