Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math, random, sys
- import pygame
- from pygame.locals import *
- # exit the program
- def quit():
- for event in pygame.event.get():
- if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
- return True
- return False
- class gameMap:
- def __init__(self, dimensions, mapData):
- global W, H
- self.width, self.height = dimensions
- self.data = mapData
- self.cellWidth = W / self.width
- self.cellHeight = H / self.height
- def draw(self, display):
- global WHITE
- for y in range(self.height):
- for x in range(self.width):
- if self.data[y][x] == 1:
- pygame.draw.rect(display, WHITE, (x * self.cellWidth, y * self.cellHeight, self.cellWidth, self.cellHeight), 0)
- def testCollision(self, x, y):
- return self.data[y][x]
- class player:
- def __init__(self):
- self.x = 0
- self.y = 0
- self.dx = 0
- self.dy = 0
- def setLocation(self, x, y):
- self.x = x
- self.y = y
- def draw(self, display, map):
- pygame.draw.rect(display, BLUE, (self.x * map.cellWidth, self.y * map.cellHeight, map.cellWidth, map.cellHeight), 0)
- def keys(self):
- k = pygame.key.get_pressed()
- if k[K_UP]: self.dy = -1
- elif k[K_DOWN]: self.dy = 1
- else: self.dy = 0
- if k[K_LEFT]: self.dx = -1
- elif k[K_RIGHT]: self.dx = 1
- else: self.dx = 0
- def move(self, map):
- if not map.testCollision(self.x + self.dx, self.y + self.dy):
- self.x += self.dx
- self.y += self.dy
- if self.x > map.width: self.x = map.width
- if self.x < 0: self.x = 0
- if self.y > map.height: self.y = map.height
- if self.y < 0: self.y = 0
- def do(self, display, map):
- self.draw(display, map)
- self.keys()
- self.move(map)
- # define display surface
- W, H = 1280, 720
- HW, HH = W / 2, H / 2
- # initialise display
- pygame.init()
- FONT = pygame.font.SysFont(None, 72)
- CLOCK = pygame.time.Clock()
- DS = pygame.display.set_mode((W, H))
- pygame.display.set_caption("code.Pylet - Pacman Walls")
- FPS = 30
- # define some colors
- BLACK = (0, 0, 0, 255)
- WHITE = (255, 255, 255, 255)
- BLUE = (0, 0, 255, 255)
- level = [
- [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
- [1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
- [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
- [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
- [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1],
- [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
- [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
- [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1],
- [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
- ]
- M = gameMap((16, 9), level)
- P = player()
- P.setLocation(1, 1)
- # main loop
- while not quit():
- M.draw(DS)
- P.do(DS, M)
- pygame.display.update()
- CLOCK.tick(FPS)
- DS.fill(BLACK)
- pygame.quit()
- sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement