Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.78 KB | None | 0 0
  1. class Maze(pygame.sprite.Sprite):
  2.     def __init__(self):
  3.         self.W = 25
  4.         self.H = 14
  5.         self.maze = [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,
  6.                      1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
  7.                      1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1,
  8.                      1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1,
  9.                      1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1,
  10.                      1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1,
  11.                      1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
  12.                      1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1,
  13.                      1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1,
  14.                      1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1,
  15.                      1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1,
  16.                      1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1,
  17.                      1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1,
  18.                      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, ]
  19.     def draw(self,screen):
  20.         block_surf = pygame.image.load(os.path.join(img_folder, "new.png")).convert()
  21.         bx = 0
  22.         by = 0
  23.         for i in range(0,self.W*self.H):
  24.             if self.maze[ bx + (by*self.W) ] == 1:
  25.                 screen.blit(block_surf,( bx * 40 , by * 40))
  26.             bx = bx + 1
  27.             if bx > self.W-1:
  28.                 bx = 0
  29.                 by = by + 1
  30.  
  31.     def collisions(self):
  32.         bx = 0
  33.         by = 0
  34.         for i in range(0, self.W * self.H):
  35.             if self.maze[ bx + (by * self.W) ] == 1:
  36.  
  37.                 rect = pygame.Rect(bx * 40, by * 40, 40, 40)
  38.  
  39.                 if rect.colliderect(Player.get_rect):
  40.                     pass  # Collision!
  41.                     return False
  42.  
  43.                 bx = bx + 1
  44.                 if bx > self.W-1:
  45.                     bx = 0
  46.                     by = by + 1
  47. class Player(pygame.sprite.Sprite):
  48.     def __init__(self): #sprite for a player
  49.         pygame.sprite.Sprite.__init__(self) #for sprite working
  50.         self.image = pygame.image.load(os.path.join(img_folder, "player.png")).convert() #look of the sprite
  51.         self.image.set_colorkey(white) #to delete black things around rect img
  52.         self.rect = self.image.get_rect() #kind of border around it
  53.         self.rect.centerx = width/2 + 40
  54.         self.rect.bottom = height- 120
  55.         self.speedx = 0
  56.         self.speedy = 0
  57.     def update(self): #moving player
  58.         maze = Maze()
  59.         collisions = maze.collisions
  60.         self.speedx = 0
  61.         self.speedy = 0
  62.         keystate = pygame.key.get_pressed()
  63.         if keystate[pygame.K_LEFT]:
  64.             if collisions == False:
  65.                 self.speedx = 0
  66.             else:
  67.                 self.speedx = -1
  68.         elif keystate[pygame.K_RIGHT]:
  69.             if self.rect.x > 920:
  70.                 self.speedx = 0
  71.             # elif self.rect.y <= 636 and self.rect.y > 580:
  72.                 # self.speedy = 0
  73.             elif self.rect.x == 40 and self.rect.y < 482 and self.rect.y > 401:
  74.                 self.speedy = 0
  75.             elif self.rect.x == 40 and self.rect.y < 399 and self.rect.y > 282:
  76.                 self.speedy = 0
  77.             elif self.rect.x == 40 and self.rect.y < 279 and self.rect.y > 160:
  78.                 self.speedy = 0
  79.             elif self.rect.x == 40 and self.rect.y < 157 and self.rect.y > 41:
  80.                 self.speedy = 0
  81.             else:
  82.                 self.speedx = 1
  83.         elif keystate[pygame.K_UP]:
  84.             if self.rect.y < 41:
  85.                 self.speedy = 0
  86.             elif self.rect.y == 159 and self.rect.x > 46 and self.rect.x < 160:
  87.                 self.speedy = 0
  88.             elif self.rect.y == 281 and self.rect.x > 45 and self.rect.x < 154:
  89.                 self.speedy = 0
  90.             else:
  91.                 self.speedy = -1
  92.         elif keystate[pygame.K_DOWN]:
  93.             if self.rect.y > 480:
  94.                 self.speedy = 0
  95.             elif self.rect.x > 405 and self.rect.x < 634 and self.rect.y > 440:
  96.                 self.speedy = 0
  97.             elif self.rect.y == 280 and self.rect.x > 46 and self.rect.x < 155:
  98.                 self.speedy = 0
  99.             else:
  100.                 self.speedy = 1
  101.         self.rect.x += self.speedx
  102.         self.rect.y += self.speedy
  103.     def get_rect(self):
  104.         return pygame.Rect(self.rect.x, self.rect.y, 40, 40)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement