Advertisement
Guest User

Untitled

a guest
Sep 4th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.35 KB | None | 0 0
  1. class Player(object):
  2.     def __init__(self, x, y):
  3.         self.x = x
  4.         self.y = y
  5.         self.vel = tiles.tilesize / 12
  6.         self.xVel = 0
  7.         self.yVel = 0
  8.         self.left = False
  9.         self.right = False
  10.         self.up = False
  11.         self.down = False
  12.         self.walkCount = 0
  13.         self.dlugoscKroku = 32
  14.         #self.kordy = [round(self.x / 32), round(self.y / 32)]
  15.         self.hitbox = (self.x, self.y, 40, 52)
  16.         self.colision = pygame.draw.rect(surface, (0,0,0), (self.hitbox[0], self.hitbox[1], 40,52))
  17.         self.rect = self.colision
  18.         self.kolizjaTarm = False
  19.  
  20.  
  21.         self.basicfont = pygame.font.SysFont(None, 25)
  22.         self.text = self.basicfont.render(str(textMsg), True, (255, 0, 0), (255, 255, 255))
  23.         self.textrect = self.text.get_rect()
  24.         self.textrect.centerx = self.x + 20
  25.         self.textrect.centery = self.y - 15
  26.  
  27.         self.map2 = None
  28.  
  29.     def show_coordinates(self):
  30.         self.kordy = [round(self.x / 32), round(self.y / 32)]
  31.         return self.kordy
  32.  
  33.     def draw(self, win):
  34.         #self.map_handler()
  35.         if self.walkCount + 1 >= 12:
  36.             self.walkCount = 0
  37.  
  38.         if self.left:
  39.             win.blit(tiles.walkLeft[self.walkCount//3],(self.x,self.y))
  40.             self.walkCount += 1
  41.         elif self.right:
  42.             win.blit(tiles.walkRight[self.walkCount//3],(self.x,self.y))
  43.             self.walkCount += 1
  44.         elif self.up:
  45.             win.blit(tiles.walkUp[self.walkCount//3], (self.x,self.y))
  46.             self.walkCount += 1
  47.         elif self.down:
  48.             win.blit(tiles.walkDown[self.walkCount//3], (self.x, self.y))
  49.             self.walkCount += 1
  50.         else:
  51.             win.blit(tiles.char, (self.x, self.y))
  52.         self.hitbox = (self.x, self.y, 40, 52)
  53.         self.colision = pygame.draw.rect(surface, (0,0,0), (self.hitbox[0], self.hitbox[1], 40,52))
  54.         self.wall_collisions()
  55.        
  56.        
  57.     def wall_collisions(self):
  58.         """Handle collisions with walls."""
  59.         self.rect.centerx = self.x
  60.         if self.colision.colliderect(tarmus.colision):
  61.             self.kolizjaTarm = True
  62.             if self.xVel > 0:
  63.                 self.rect.right = tarmus.rect.left
  64.                 self.x = self.rect.x
  65.             elif self.xVel < 0:
  66.                 self.rect.left = tarmus.rect.right
  67.                 self.x = self.rect.x
  68.            
  69.  
  70.         self.rect.centery = self.y
  71.         if self.colision.colliderect(tarmus.colision):# and self.yVel != 0:
  72.             self.kolizjaTarm = True
  73.             if self.yVel > 0:
  74.                 self.rect.bottom = tarmus.rect.top
  75.                 self.y = self.rect.y
  76.             elif self.yVel < 0:
  77.                 self.rect.top = tarmus.rect.bottom
  78.                 self.y = self.rect.y
  79.  
  80.         blocksLength = len(blocks)
  81.         for item in range(blocksLength):
  82.             if self.colision.colliderect(blocks[item].colision):
  83.                 if self.xVel > 0:
  84.                     self.rect.right = blocks[item].rect.left
  85.                     self.x = self.rect.x
  86.                 elif self.xVel < 0:
  87.                     self.rect.left = blocks[item].rect.right
  88.                     self.x = self.rect.x
  89.  
  90.         for item in range(blocksLength):
  91.             if self.colision.colliderect(blocks[item].colision) and self.yVel != 0:
  92.                 if self.yVel > 0:
  93.                     self.rect.bottom = blocks[item].rect.top
  94.                     self.y = self.rect.y
  95.                 elif self.yVel < 0:
  96.                     self.rect.top = blocks[item].rect.bottom
  97.                     self.y = self.rect.y
  98.  
  99.     def move_handler(self):
  100.         self.keys = pygame.key.get_pressed()
  101.         #self.map_handler()
  102.         if self.keys[pygame.K_LEFT] and self.x >= self.vel - 10:
  103.             self.x -= self.vel
  104.             self.left = True
  105.             self.right = False
  106.             self.up = False
  107.             self.down = False
  108.             self.xVel = -3
  109.             self.yVel = 0
  110.             tiles.tilesize = 32
  111.             self.kolizjaTarm = False
  112.         elif self.keys[pygame.K_RIGHT] and self.x < 512 - self.vel - self.hitbox[2]:
  113.             self.x += player.vel
  114.             self.left = False
  115.             self.right = True
  116.             self.up = False
  117.             self.down = False
  118.             self.xVel = 3
  119.             self.yVel = 0
  120.             tiles.tilesize = 32
  121.             self.kolizjaTarm = False
  122.         elif self.keys[pygame.K_UP] and self.y >= self.vel - 10:
  123.             self.y -= self.vel
  124.             self.left = False
  125.             self.right = False
  126.             self.up = True
  127.             self.down = False
  128.             self.yVel = -3
  129.             self.xVel = 0
  130.             tiles.tilesize = 32
  131.             self.kolizjaTarm = False
  132.         elif self.keys[pygame.K_DOWN] and self.y < 512 - self.vel - self.hitbox[3]:
  133.             self.y += self.vel
  134.             self.left = False
  135.             self.right = False
  136.             self.up = False
  137.             self.down = True
  138.             self.yVel = 3
  139.             self.xVel = 0
  140.             tiles.tilesize = 32
  141.             self.kolizjaTarm = False
  142.         else:
  143.             self.left = False
  144.             self.right = False
  145.             self.up = False
  146.             self.down = False
  147.             self.walkCount = 0
  148.             tiles.tilesize = 32
  149.             self.xVel = 0
  150.             self.yVel = 0
  151.  
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement