Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.27 KB | None | 0 0
  1. import pygame,random
  2.  
  3. window = pygame.display.set_mode((800,800), pygame.NOFRAME)
  4.  
  5. # player factory
  6. class player:
  7.     def __init__(self,x,y,height,width,color):
  8.         self.x = x
  9.         self.y = y
  10.         self.height = height
  11.         self.width = width
  12.         self.color = color
  13.         self.speed = 4
  14.         self.fall = 0
  15.         self.isJump = False
  16.         # collisions for platforms
  17.         self.moveleft = True
  18.         self.moveright = True
  19.  
  20.         # animation frame for right:
  21.         self.right = [pygame.image.load("player1.png"),
  22.                       pygame.image.load("player2.png"),
  23.                       pygame.image.load("player3.png"),
  24.                       pygame.image.load("player4.png"),
  25.                       pygame.image.load("player5.png")]
  26.  
  27.         # animation frame for left:
  28.         self.left = [pygame.image.load("player10.png"),
  29.                      pygame.image.load("player11.png"),
  30.                      pygame.image.load("player12.png"),
  31.                      pygame.image.load("player13.png"),
  32.                      pygame.image.load("player14.png")]
  33.  
  34.  
  35.         self.idleright = [pygame.image.load("idle1.png"),
  36.                           pygame.image.load("idle1.png"),
  37.                           pygame.image.load("idle1.png"),
  38.                           pygame.image.load("idle1.png")]
  39.                          
  40.  
  41.         self.idleleft = [pygame.image.load("id1.png"),
  42.                           pygame.image.load("id1.png"),
  43.                           pygame.image.load("id1.png"),
  44.                           pygame.image.load("id1.png")]
  45.  
  46.  
  47.  
  48.  
  49.         self.anim_index = 0
  50.                          
  51.         self.direction = "right"
  52.         self.direction = "left"
  53.         self.direction = "standright"
  54.         self.direction = "standleft"
  55.  
  56.         self.direction = "jumpright"
  57.         self.direction = "jumpleft"
  58.  
  59.         self.health = 10
  60.         self.hitbox = (self.x + 20, self.y, 26,60)
  61.         self.clock = pygame.time.Clock()
  62.         self.fps = 24
  63.         self.JumpCount = 10
  64.         self.rect = pygame.Rect(x,y,height,width)
  65.     def draw(self):
  66.         self.rect.topleft = (self.x,self.y)
  67.  
  68.         if self.direction == "right":
  69.             self.clock.tick(self.fps)
  70.  
  71.             image_list = self.right
  72.  
  73.         elif self.direction == "left":
  74.             self.clock.tick(self.fps)
  75.             image_list = self.left
  76.  
  77.         elif self.direction == "standright":
  78.             self.clock.tick(self.fps)
  79.             image_list = self.idleright
  80.  
  81.  
  82.         elif self.direction == "standleft":
  83.             self.clock.tick(self.fps)
  84.             image_list = self.idleleft
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.                
  93.         else:
  94.             self.clock.tick(self.fps)
  95.             image_list = self.idleright
  96.  
  97.  
  98.         if self.anim_index >= len(image_list):
  99.             self.anim_index = 0
  100.         player_image = image_list[self.anim_index]
  101.         self.anim_index += 1
  102.  
  103.         self.hitbox = (self.x + -18, self.y, 46,60)
  104.         pygame.draw.rect(window, (255,0,0), (self.hitbox[0], self.hitbox[1] - 40, 80, 10)) # NEW
  105.         pygame.draw.rect(window, (0,200,0), (self.hitbox[0], self.hitbox[1] - 40, 80 - (5 * (10 - self.health)), 10))
  106.  
  107.         player_rect = player_image.get_rect(center = self.rect.center)
  108.         player_rect.centerx += -2 # 10 is just an example
  109.         player_rect.centery += -25# 15 is just an example
  110.         window.blit(player_image, player_rect)
  111.  
  112.  
  113.  
  114.  
  115. class box:
  116.     def __init__(self,x,y,height,width,color):
  117.         self.x = x
  118.         self.y = y
  119.         self.height = height
  120.         self.width = width
  121.         self.color = color
  122.         self.imagebox = pygame.image.load("box1.png")
  123.         self.imagebox = pygame.transform.scale(self.imagebox, (self.imagebox.get_width()//3,self.imagebox.get_height()//3))
  124.         self.rect = pygame.Rect(x,y,height,width)
  125.         self.hitbox = (self.x + 10, self.y + 10 , 46,60)
  126.         self.health = 10    
  127.     def draw(self):
  128.         self.rect.topleft = (self.x,self.y)
  129.         self.hitbox = (self.x + -5, self.y + 10 , 46,60)
  130.         window.blit(self.imagebox,self.rect)
  131.  
  132. white = (34,32,52)
  133. box1 = box(280,520,150,50,white)
  134.  
  135.  
  136.  
  137.  
  138. pink = (34,32,52)
  139. playerman = player(300,300,40,40,pink)
  140.  
  141.  
  142. # player factory
  143. class platform22:
  144.     def __init__(self,x,y,height,width,color):
  145.         self.x = x
  146.         self.y = y
  147.         self.height = height
  148.         self.width = width
  149.         self.color = color
  150.         self.image = pygame.image.load("platform1.png")
  151.         self.image = pygame.transform.scale(self.image, (self.image.get_width()+60, self.image.get_height()+60))
  152.         self.rect = pygame.Rect(x,y,height,width)
  153.     def draw(self):
  154.         self.rect.topleft = (self.x,self.y)
  155.         pygame.draw.rect(window,self.color,self.rect)
  156.  
  157. platform2 = platform22(280,580,180,50,white)
  158.  
  159.        
  160. # player factory
  161. class platform:
  162.     def __init__(self,x,y,height,width,color):
  163.         self.x = x
  164.         self.y = y
  165.         self.height = height
  166.         self.width = width
  167.         self.color = color
  168.         self.rect = pygame.Rect(x,y,height,width)
  169.     def draw(self):
  170.         self.rect.topleft = (self.x,self.y)
  171.         pygame.draw.rect(window,self.color,self.rect)
  172.  
  173.  
  174. white = (34,32,52)
  175. platform1 = platform(0,766,399990,3000,white)
  176.  
  177.  
  178. platforms = [platform1,platform2]
  179.  
  180.  
  181.  
  182.  
  183. # the game fps
  184. clock = pygame.time.Clock()
  185. FPS = 30
  186.  
  187.  
  188.  
  189.  
  190.                
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199. # redraw the window
  200.  
  201. def redraw():
  202.     playerman.draw()
  203.     for platform in platforms:
  204.         platform.draw()
  205.  
  206.     box1.draw()
  207.  
  208.     platform2.draw()
  209.  
  210.  
  211.     if playerman.rect.colliderect(box1.rect):
  212.         if keys[pygame.K_p]:
  213.             pygame.draw.rect(window, (69,40,60), (box1.hitbox[0], box1.hitbox[1] - 40, 80, 20)) # NEW
  214.             pygame.draw.rect(window, (238,195,154), (box1.hitbox[0], box1.hitbox[1] - 40, 5  - (5 * (10 - box1.health)), 20))
  215.             if box1.health < 25:
  216.                 box1.health += 0.4
  217.        
  218.  
  219.  
  220.        
  221. # main loop
  222. runninggame = True
  223. while runninggame:
  224.     clock.tick(FPS)
  225.     for event in pygame.event.get():
  226.         if event.type == pygame.QUIT:
  227.             runningggame = False
  228.  
  229.     # key event
  230.     keys = pygame.key.get_pressed()
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.     if playerman.direction == "right":
  239.         playerman.direction = "standright"
  240.  
  241.     if playerman.direction == "left":
  242.         playerman.direction = "standleft"
  243.  
  244.  
  245.  
  246.        
  247.  
  248.     if keys[pygame.K_d]:
  249.         if playerman.moveright:
  250.             playerman.direction = "right"
  251.             playerman.x += playerman.speed    
  252.  
  253.     if keys[pygame.K_a]:
  254.         if playerman.moveleft:
  255.             playerman.direction = "left"
  256.             playerman.x -= playerman.speed
  257.  
  258.  
  259.  
  260.        
  261.    
  262.          # move the player
  263.     collide = False
  264.     if not playerman.isJump:
  265.         if keys[pygame.K_SPACE]:
  266.             if playerman.direction == "left" and playerman.direction == "standleft":
  267.                 playerman.direction = "jumpleft"
  268.                 playerman.clock.tick(30)
  269.                 image_list = playerman.jumpleft
  270.  
  271.                
  272.             if playerman.direction == "right" and playerman.direction == "standright":
  273.                 playerman.direction = "jumpright"
  274.             playerman.isJump = True
  275.  
  276.  
  277.  
  278.         playerman.y += playerman.fall
  279.         playerman.fall += 1
  280.         playerman.isJump = False
  281.         collide = False
  282.  
  283.  
  284.         # collisions
  285.         for platform in platforms:
  286.             if playerman.rect.colliderect(platform.rect):
  287.                 collide = True
  288.                 playerman.isJump = False
  289.                 if (platform.rect.collidepoint(playerman.rect.right, playerman.rect.bottom) or
  290.                     platform.rect.collidepoint(playerman.rect.left, playerman.rect.bottom)):
  291.                     playerman.y = platform.rect.top - playerman.height + 1
  292.                     playerman.moveright = True
  293.                     playerman.moveleft = True
  294.                
  295.                 if (platform.rect.collidepoint(playerman.rect.right, playerman.rect.top) or
  296.                     platform.rect.collidepoint(playerman.rect.right, playerman.rect.bottom - 10)):
  297.                     playerman.moveright = False
  298.                 elif (platform.rect.collidepoint(playerman.rect.left, playerman.rect.top) or
  299.                       platform.rect.collidepoint(playerman.rect.left, playerman.rect.bottom - 10)):
  300.                     playerman.moveleft = False
  301.             else:
  302.                 playerman.moveright = True
  303.                 playerman.moveleft = True
  304.                
  305.                        
  306.                         # this makes the player fall down up to
  307.         if playerman.rect.bottom >= 890:
  308.             collide = True
  309.             playerman.isJump = False
  310.             playerman.JumpCount = 10
  311.             playerman.y = 890 - playerman.height
  312.  
  313.  
  314.  
  315.         if collide:
  316.             if keys[pygame.K_SPACE]:
  317.                 playerman.isJump = True
  318.                    
  319.             playerman.fall = 0
  320.  
  321.  
  322.     else:
  323.         if playerman.JumpCount >0:
  324.             playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount)) * 0.3
  325.             playerman.JumpCount -= 1
  326.         else:
  327.             playerman.JumpCount = 10
  328.             playerman.isJump = False
  329.  
  330.  
  331.  
  332.  
  333.  
  334.     window.fill((0,0,0))
  335.     redraw()
  336.     pygame.display.update()
  337. pygame.quit()
  338.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement