Advertisement
cookertron

Map/Maze Python Pygame Test

Jul 19th, 2018
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.73 KB | None | 0 0
  1. import math, random, sys
  2. import pygame
  3. from pygame.locals import *
  4.  
  5. # exit the program
  6. def quit():
  7.     for event in pygame.event.get():
  8.         if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
  9.             return True
  10.     return False
  11.  
  12. class gameMap:
  13.     def __init__(self, dimensions, mapData):
  14.         global W, H
  15.         self.width, self.height = dimensions
  16.         self.data = mapData
  17.        
  18.         self.cellWidth = W / self.width
  19.         self.cellHeight = H / self.height
  20.    
  21.     def draw(self, display):
  22.         global WHITE
  23.         for y in range(self.height):
  24.             for x in range(self.width):
  25.                 if self.data[y][x] == 1:
  26.                     pygame.draw.rect(display, WHITE, (x * self.cellWidth, y * self.cellHeight, self.cellWidth, self.cellHeight), 0)
  27.  
  28.     def testCollision(self, x, y):
  29.         return self.data[y][x]
  30.  
  31. class player:
  32.     def __init__(self):
  33.         self.x = 0
  34.         self.y = 0
  35.         self.dx = 0
  36.         self.dy = 0
  37.    
  38.     def setLocation(self, x, y):
  39.         self.x = x
  40.         self.y = y
  41.        
  42.     def draw(self, display, map):
  43.         pygame.draw.rect(display, BLUE, (self.x * map.cellWidth, self.y * map.cellHeight, map.cellWidth, map.cellHeight), 0)
  44.    
  45.     def keys(self):
  46.         k = pygame.key.get_pressed()
  47.         if k[K_UP]: self.dy = -1
  48.         elif k[K_DOWN]: self.dy = 1
  49.         else: self.dy = 0
  50.        
  51.         if k[K_LEFT]: self.dx = -1
  52.         elif k[K_RIGHT]: self.dx = 1
  53.         else: self.dx = 0
  54.    
  55.     def move(self, map):
  56.         if not map.testCollision(self.x + self.dx, self.y + self.dy):
  57.             self.x += self.dx
  58.             self.y += self.dy
  59.            
  60.         if self.x > map.width: self.x = map.width
  61.         if self.x < 0: self.x = 0
  62.        
  63.         if self.y > map.height: self.y = map.height
  64.         if self.y < 0: self.y = 0
  65.    
  66.     def do(self, display, map):
  67.         self.draw(display, map)
  68.         self.keys()
  69.         self.move(map)
  70.        
  71. # define display surface           
  72. W, H = 1280, 720
  73. HW, HH = W / 2, H / 2
  74.  
  75. # initialise display
  76. pygame.init()
  77. FONT = pygame.font.SysFont(None, 72)
  78. CLOCK = pygame.time.Clock()
  79. DS = pygame.display.set_mode((W, H))
  80. pygame.display.set_caption("code.Pylet - Pacman Walls")
  81. FPS = 30
  82.  
  83. # define some colors
  84. BLACK = (0, 0, 0, 255)
  85. WHITE = (255, 255, 255, 255)
  86. BLUE = (0, 0, 255, 255)
  87.  
  88. level = [
  89.     [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
  90.     [1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  91.     [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
  92.     [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
  93.     [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1],
  94.     [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
  95.     [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
  96.     [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1],
  97.     [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
  98. ]
  99.  
  100. M = gameMap((16, 9), level)
  101. P = player()
  102. P.setLocation(1, 1)
  103.  
  104. # main loop
  105. while not quit():
  106.     M.draw(DS)
  107.     P.do(DS, M)
  108.    
  109.     pygame.display.update()
  110.     CLOCK.tick(FPS)
  111.     DS.fill(BLACK)
  112. pygame.quit()
  113. sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement