Guest User

Untitled

a guest
Aug 4th, 2020
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 14.81 KB | None | 0 0
  1. import pygame,random,math
  2.  
  3. window = pygame.display.set_mode((800,800), pygame.NOFRAME)
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11. def blitRotate(surf, image, pos, originPos, angle):
  12.  
  13.         # calcaulate the axis aligned bounding box of the rotated image
  14.     w, h = image.get_size()
  15.     sin_a, cos_a = math.sin(math.radians(angle)), math.cos(math.radians(angle))
  16.     min_x, min_y = min([0, sin_a*h, cos_a*w, sin_a*h + cos_a*w]), max([0, sin_a*w, -cos_a*h, sin_a*w - cos_a*h])
  17.  
  18.         # calculate the translation of the pivot
  19.     pivot        = pygame.math.Vector2(originPos[0], -originPos[1])
  20.     pivot_rotate = pivot.rotate(angle)
  21.     pivot_move   = pivot_rotate - pivot
  22.  
  23.         # calculate the upper left origin of the rotated image
  24.     origin = (pos[0] - originPos[0] + min_x - pivot_move[0], pos[1] - originPos[1] - min_y + pivot_move[1])
  25.  
  26.         # get a rotated image
  27.     rotated_image = pygame.transform.rotate(image, angle)
  28.  
  29.         # rotate and blit the image
  30.     surf.blit(rotated_image, origin)
  31.  
  32.  
  33.  
  34. class handgun():
  35.     def __init__(self,x,y,height,width,color):
  36.         self.x = x
  37.         self.y = y
  38.         self.height = height
  39.         self.width = width
  40.         self.color = color
  41.         self.rect = pygame.Rect(x,y,height,width)
  42.  
  43.  
  44.         # LOL THESE IS THE HAND
  45.         self.shootsright = pygame.image.load("hands.png")
  46.         self.image = self.shootsright
  47.         self.rect  = self.image.get_rect(center = (self.x, self.y))
  48.         self.look_at_pos = (self.x, self.y)
  49.  
  50.         self.isLookingAtPlayer = False
  51.         self.look_at_pos = (x,y)
  52.            
  53.  
  54.  
  55.  
  56.         self.hitbox = (self.x + -18, self.y, 46,60)
  57.  
  58.  
  59.  
  60.     def draw(self,drawX,drawY):
  61.  
  62.         self.rect.topleft =  (drawX,drawY)
  63.  
  64.             # the guns hitbox
  65.  
  66.             # rotatiing the gun
  67.         dx = self.look_at_pos[0] - self.rect.centerx
  68.         dy = self.look_at_pos[1] - self.rect.centery
  69.            
  70.         angle = (180/math.pi) * math.atan2(-dy, dx)
  71.      
  72.         gun_size = self.image.get_size()
  73.         pivot = (8, gun_size[1]//2)
  74.            
  75.  
  76.         blitRotate(window, self.image, self.rect.center, pivot, angle)
  77.  
  78.     def lookAt( self, coordinate ):
  79.            
  80.         self.look_at_pos = coordinate
  81.  
  82.  
  83.  
  84. white = (255,255,255)
  85. handgun1 = handgun(300,300,10,10,white)
  86.        
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93. # player factory
  94. class player:
  95.     def __init__(self,x,y,height,width,color):
  96.         self.x = x
  97.         self.y = y
  98.         self.height = height
  99.         self.width = width
  100.         self.color = color
  101.         self.speed = 4
  102.         self.fall = 0
  103.         self.isJump = False
  104.         # collisions for platforms
  105.         self.moveleft = True
  106.         self.moveright = True
  107.  
  108.  
  109.         self.rect  = pygame.Rect(x,y,height,width)
  110.  
  111.  
  112.         # animation frame for right:
  113.         self.right = [pygame.image.load("player1.png"),
  114.                       pygame.image.load("player2.png"),
  115.                       pygame.image.load("player3.png"),
  116.                       pygame.image.load("player4.png"),
  117.                       pygame.image.load("player5.png")]
  118.  
  119.         # animation frame for left:
  120.         self.left = [pygame.image.load("player10.png"),
  121.                      pygame.image.load("player11.png"),
  122.                      pygame.image.load("player12.png"),
  123.                      pygame.image.load("player13.png"),
  124.                      pygame.image.load("player14.png")]
  125.  
  126.  
  127.         self.idleright = [pygame.image.load("idle1.png"),
  128.                           pygame.image.load("idle1.png"),
  129.                           pygame.image.load("idle1.png"),
  130.                           pygame.image.load("idle1.png")]
  131.                          
  132.  
  133.         self.idleleft = [pygame.image.load("id1.png"),
  134.                           pygame.image.load("id1.png"),
  135.                           pygame.image.load("id1.png"),
  136.                           pygame.image.load("id1.png")]
  137.  
  138.  
  139.  
  140.  
  141.         self.anim_index = 0
  142.                          
  143.         self.direction = "right"
  144.         self.direction = "left"
  145.         self.direction = "standright"
  146.         self.direction = "standleft"
  147.  
  148.         self.direction = "jumpright"
  149.         self.direction = "jumpleft"
  150.  
  151.         self.health = 10
  152.         self.hitbox = (self.x + 20, self.y, 26,60)
  153.         self.clock = pygame.time.Clock()
  154.         self.fps = 24
  155.         self.JumpCount = 10
  156.         self.rect = pygame.Rect(x,y,height,width)
  157.  
  158.  
  159.         self.gun = (self.x + -18, self.y, 46,60)
  160.  
  161.     def draw(self):
  162.         self.rect.topleft = (self.x,self.y)
  163.  
  164.         if self.direction == "right":
  165.             self.clock.tick(self.fps)
  166.  
  167.             image_list = self.right
  168.  
  169.         elif self.direction == "left":
  170.             self.clock.tick(self.fps)
  171.             image_list = self.left
  172.  
  173.         elif self.direction == "standright":
  174.             self.clock.tick(self.fps)
  175.             image_list = self.idleright
  176.  
  177.  
  178.         elif self.direction == "standleft":
  179.             self.clock.tick(self.fps)
  180.             image_list = self.idleleft
  181.  
  182.  
  183.  
  184.  
  185.  
  186.                
  187.         else:
  188.             self.clock.tick(self.fps)
  189.             image_list = self.idleright
  190.  
  191.  
  192.         if self.anim_index >= len(image_list):
  193.             self.anim_index = 0
  194.         player_image = image_list[self.anim_index]
  195.         self.anim_index += 1
  196.  
  197.         self.hitbox = (self.x + -18, self.y, 46,60)
  198.         pygame.draw.rect(window, (255,0,0), (self.hitbox[0], self.hitbox[1] - 40, 80, 10)) # NEW
  199.         pygame.draw.rect(window, (0,200,0), (self.hitbox[0], self.hitbox[1] - 40, 80 - (5 * (10 - self.health)), 13))
  200.  
  201.         player_rect = player_image.get_rect(center = self.rect.center)
  202.         player_rect.centerx += -2 # 10 is just an example
  203.         player_rect.centery += -25# 15 is just an example
  204.         window.blit(player_image, player_rect)
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214. class portal:
  215.     def __init__(self,x,y,height,width,color):
  216.         self.x = x
  217.         self.y = y
  218.         self.height = height
  219.         self.width = width
  220.         self.color = color
  221.         self.rightblob = [
  222.             pygame.image.load("port1.png"),
  223.             pygame.image.load("port2.png"),
  224.             pygame.image.load("port3.png"),
  225.             pygame.image.load("port4.png"),
  226.             pygame.image.load("port5.png"),
  227.             pygame.image.load("port6.png"),
  228.             pygame.image.load("port7.png"),
  229.             pygame.image.load("port8.png")]
  230.  
  231.  
  232.         self.rightblob = [pygame.transform.scale(image,(image.get_width()*3,image.get_height()*3)) for image in self.rightblob]
  233.    
  234.         self.rect = pygame.Rect(x,y,height,width)
  235.         self.direction = "blobright"
  236.  
  237.         self.anim_index = 0
  238.     def draw(self):
  239.         self.rect.topleft = (self.x,self.y)
  240.         pygame.draw.rect(window,self.color,self.rect)
  241.         if self.direction == "blobright":
  242.             window.blit(self.rightblob[self.anim_index], self.rect)
  243.             self.anim_index += 1
  244.             if self.anim_index == len(self.rightblob):
  245.                 self.anim_index = 0
  246.  
  247.  
  248. white = (255,255,255)
  249.  
  250. port1 = portal(420,388,50,50,white)
  251.  
  252.  
  253.  
  254. class box:
  255.     def __init__(self,x,y,height,width,color):
  256.         self.x = x
  257.         self.y = y
  258.         self.height = height
  259.         self.width = width
  260.         self.color = color
  261.         self.imagebox = pygame.image.load("box1.png")
  262.         self.imagebox = pygame.transform.scale(self.imagebox, (self.imagebox.get_width()//3,self.imagebox.get_height()//3))
  263.         self.rect = pygame.Rect(x,y,height,width)
  264.         self.hitbox = (self.x + 10, self.y + 10 , 46,60)
  265.         self.health = 10    
  266.     def draw(self):
  267.         self.rect.topleft = (self.x,self.y)
  268.         self.hitbox = (self.x + -5, self.y + 10 , 46,60)
  269.         window.blit(self.imagebox,self.rect)
  270.  
  271. white = (23, 32, 42)
  272. box1 = box(320,385,130,50,white)
  273.  
  274. boxes = [box1]
  275.  
  276.  
  277. pink = (34,32,52)
  278. playerman = player(300,300,40,40,pink)
  279.  
  280.  
  281. # player factory
  282. class platform22:
  283.     def __init__(self,x,y,height,width,color):
  284.         self.x = x
  285.         self.y = y
  286.         self.height = height
  287.         self.width = width
  288.         self.color = color
  289.         self.image = pygame.image.load("platform1.png")
  290.         self.image = pygame.transform.scale(self.image, (self.image.get_width()+60, self.image.get_height()+60))
  291.         self.rect = pygame.Rect(x,y,height,width)
  292.     def draw(self):
  293.         self.rect.topleft = (self.x,self.y)
  294.         pygame.draw.rect(window,self.color,self.rect)
  295.  
  296. platform2 = platform22(280,680,120,20,white)
  297.  
  298. bg1color = (59, 66, 66)
  299.  
  300. bg2color = (77, 86, 86)
  301.  
  302. bg1 = platform22(0,540,820,750,bg1color)
  303.  
  304. bg2 = platform22(100,100,120,750,bg2color)
  305.  
  306.  
  307. bg3 = platform22(300,100,120,750,bg2color)
  308.  
  309.  
  310. bg4 = platform22(400,100,120,750,bg2color)
  311.      
  312. # player factory
  313. class platform:
  314.     def __init__(self,x,y,height,width,color):
  315.         self.x = x
  316.         self.y = y
  317.         self.height = height
  318.         self.width = width
  319.         self.color = color
  320.         self.rect = pygame.Rect(x,y,height,width)
  321.     def draw(self):
  322.         self.rect.topleft = (self.x,self.y)
  323.         pygame.draw.rect(window,self.color,self.rect)
  324.  
  325.  
  326. white = (77, 86, 86)
  327. platform1 = platform(0,766,399990,3000,white)
  328.  
  329.  
  330.  
  331.  
  332. platforms = [platform1,platform2]
  333.  
  334. platformGroup = pygame.sprite.Group
  335.  
  336. level = [
  337. "                                                                                           c            ",
  338. "                                                                                                       ",
  339. "                      c                                            c                                     ",
  340. "     c                                      c                                                            ",
  341. "                                                                                                      ",
  342. "         c                                                                                             ",
  343. "                                                                                                      ",
  344. "                                                       c                                                ",
  345. "                                                                       c                                ",
  346. "                              c                                                                         ",
  347. "                                                                                                       ",
  348. "                                                                                                      ",
  349. "                                                     c                                                  ",
  350. "            c                                                                                c           "]
  351.  
  352.  
  353. for iy, row in enumerate(level):
  354.     for ix, col in enumerate(row):
  355.         if col == "c":
  356.             new_platforms = platform(ix*9.8, iy*50, 120,20,(23, 32, 42))
  357.             platforms.append(new_platforms)
  358.  
  359.  
  360.  
  361.  
  362.  
  363. # the game fps
  364. clock = pygame.time.Clock()
  365. FPS = 30
  366.  
  367.  
  368.  
  369.  
  370.                
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379. # redraw the window
  380.  
  381. def redraw():
  382.    
  383.     bg3.draw()
  384.     bg4.draw()
  385.     bg2.draw()
  386.     bg1.draw()
  387.  
  388.  
  389.    
  390.     playerman.draw()
  391.     for platform in platforms:
  392.         platform.draw()
  393.  
  394.  
  395.     platform2.draw()
  396.     port1.draw()
  397.  
  398.     for box in boxes:
  399.         box.draw()
  400.  
  401.  
  402.        
  403.     if box1.health > 25:
  404.        
  405.         handgun1.draw(playerman.x - 19,playerman.y - 29)
  406.  
  407.     if playerman.rect.colliderect(box1.rect):
  408.         if keys[pygame.K_p]:
  409.             pygame.draw.rect(window, (69,40,60), (box1.hitbox[0], box1.hitbox[1] - 40, 80, 20)) # NEW
  410.             pygame.draw.rect(window, (238,195,154), (box1.hitbox[0], box1.hitbox[1] - 40, 5  - (5 * (10 - box1.health)), 20))
  411.             if box1.health < 25:
  412.                 box1.health += 0.4
  413.             else:
  414.                 box1.x = -900
  415.  
  416.  
  417.        
  418.  
  419.  
  420.        
  421. # main loop
  422. runninggame = True
  423. while runninggame:
  424.     clock.tick(FPS)
  425.     for event in pygame.event.get():
  426.         if event.type == pygame.QUIT:
  427.             runningggame = False
  428.  
  429.     # key event
  430.     keys = pygame.key.get_pressed()
  431.  
  432.        
  433.  
  434.  
  435.  
  436.  
  437.         # gun rotation
  438.     if playerman.rect.colliderect(handgun1.rect):
  439.         mousex, mousey = pygame.mouse.get_pos()
  440.         if not handgun1.isLookingAtPlayer:
  441.             handgun1.lookAt((mousex, mousey))
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.     if playerman.direction == "right":
  450.         playerman.direction = "standright"
  451.  
  452.     if playerman.direction == "left":
  453.         playerman.direction = "standleft"
  454.  
  455.  
  456.  
  457.        
  458.  
  459.     if keys[pygame.K_d]:
  460.         if playerman.moveright:
  461.             playerman.direction = "right"
  462.             playerman.x += playerman.speed    
  463.  
  464.     if keys[pygame.K_a]:
  465.         if playerman.moveleft:
  466.             playerman.direction = "left"
  467.             playerman.x -= playerman.speed
  468.  
  469.  
  470.  
  471.        
  472.    
  473.          # move the player
  474.     collide = False
  475.     if not playerman.isJump:
  476.         if keys[pygame.K_SPACE]:
  477.             if playerman.direction == "left" and playerman.direction == "standleft":
  478.                 playerman.direction = "jumpleft"
  479.                 playerman.clock.tick(30)
  480.                 image_list = playerman.jumpleft
  481.  
  482.                
  483.             if playerman.direction == "right" and playerman.direction == "standright":
  484.                 playerman.direction = "jumpright"
  485.             playerman.isJump = True
  486.  
  487.  
  488.  
  489.         playerman.y += playerman.fall
  490.         playerman.fall += 1
  491.         playerman.isJump = False
  492.         collide = False
  493.  
  494.  
  495.         # collisions
  496.         for platform in platforms:
  497.             if playerman.rect.colliderect(platform.rect):
  498.                 collide = True
  499.                 playerman.isJump = False
  500.                 playerman.y = platform.rect.top - playerman.height + 1
  501.                 if playerman.rect.right > platform.rect.left and playerman.rect.left < platform.rect.left - playerman.width:
  502.                     playerman.x = platform.rect.left - playerman.width
  503.                 if playerman.rect.left < platform.rect.right and playerman.rect.right > platform.rect.right + playerman.width:
  504.                     playerman.x = platform.rect.right
  505.  
  506.  
  507.                    
  508.                         # this makes the player fall down up to
  509.         if playerman.rect.bottom >= 890:
  510.             collide = True
  511.             playerman.isJump = False
  512.             playerman.JumpCount = 10
  513.             playerman.y = 890 - playerman.height
  514.  
  515.  
  516.  
  517.         if collide:
  518.             if keys[pygame.K_SPACE]:
  519.                 playerman.isJump = True
  520.                    
  521.             playerman.fall = 0
  522.  
  523.  
  524.     else:
  525.         if playerman.JumpCount > 0:
  526.             playerman.y -= (playerman.JumpCount*abs(playerman.JumpCount)) * 0.3
  527.             playerman.JumpCount -= 1
  528.         else:
  529.             playerman.JumpCount = 10
  530.             playerman.isJump = False
  531.  
  532.  
  533.  
  534.  
  535.  
  536.     window.fill((81, 90, 90))
  537.     redraw()
  538.     pygame.display.update()
  539. pygame.quit()
  540.  
Add Comment
Please, Sign In to add comment