qeqwew

Untitled

Oct 6th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. import pygame
  2. from pygame import *
  3. import random
  4.  
  5. # class Collisions:
  6. # def __init__(self, bullets1_x, bullets1_y, bullets2_x, bullets2_y):
  7. # self.bullets1_y = bullets1_y
  8. # self.bullets2_x = bullets2_x
  9. # self.bullets1_x = bullets1_x
  10. # self.bullets2_y = bullets2_y
  11. # def bul(self, col_check_bul):
  12. # if abs(self.bullets1_x - self.bullets2_x) < 10 :
  13. # col_check_bul = 1
  14.  
  15. class Platform:
  16. def __init__(self, x, y, w, h):
  17. self.x = x
  18. self.y = y
  19. self.w = w
  20. self.h = h
  21.  
  22. def draw_platform(self):
  23. pygame.draw.rect(win,(250,2,145), (self.x, self.y, self.w, self.h))
  24.  
  25. class Bullet:
  26. def __init__(self,x,y,radius,color,facing):
  27. self.x = x
  28. self.y = y
  29. self.radius = radius
  30. self.color = color
  31. self.facing = facing
  32. self.vel = 8 * facing
  33.  
  34. def draw(self,win):
  35. pygame.draw.circle(win,self.color,(self.x,self.y),self.radius)
  36.  
  37.  
  38.  
  39.  
  40. class Player:
  41. def __init__(self, x, y, w, h,speed):
  42. self.x = x
  43. self.y = y
  44. self.w = w
  45. self.h = h
  46. self.speed = speed
  47. self.isJump = False
  48. self.jumpCount = 10
  49. self.bullets = []
  50. self.lastMove ="R"
  51. self.facing = 1
  52.  
  53.  
  54.  
  55. def draw_player(self):
  56. pygame.draw.rect(win,(255,255,255), (self.x, self.y, self.h, self.w))
  57.  
  58. def shoot(self):
  59. for bullet in self.bullets:
  60. if bullet.x < win_w and bullet.x > 0:
  61. bullet.x += bullet.vel
  62. else:
  63. self.bullets.pop(self.bullets.index(bullet))
  64.  
  65.  
  66. def move_player1(self):
  67. keys = pygame.key.get_pressed()
  68.  
  69. if keys[pygame.K_SPACE]:
  70. if self.lastMove == "R":
  71. self.facing = 1
  72. else:
  73. self.facing = -1
  74.  
  75. if len(self.bullets) < 1:
  76. self.bullets.append(Bullet(round(self.x +self.w // 2),round(self.y +self.h // 2),5,(255,0,0),self.facing))
  77.  
  78. if keys[pygame.K_a] and self.x > 5:
  79. self.x -= self.speed
  80. self.lastMove = "L"
  81. if keys[pygame.K_d] and self.x < win_w - self.w - 5:
  82. self.x += self.speed
  83. self.lastMove = "R"
  84. if not(self.isJump):
  85. if keys[pygame.K_w] and self.y > 5 :
  86. self.isJump = True
  87. if keys[pygame.K_s] and self.y < win_h - self.h - 5:
  88. self.y += self.speed
  89. else:
  90. if self.jumpCount >= -10:
  91. if self.jumpCount < 0:
  92. self.y += (self.jumpCount ** 2)/2
  93. else:
  94. self.y -= (self.jumpCount ** 2)/2
  95. self.jumpCount -= 1
  96. else:
  97. self.isJump = False
  98. self.jumpCount = 10
  99.  
  100.  
  101.  
  102. def move_player2(self):
  103. keys = pygame.key.get_pressed()
  104. if keys[pygame.K_z]:
  105. if self.lastMove == "R":
  106. self.facing = 1
  107. else:
  108. self.facing = -1
  109.  
  110. if len(self.bullets) < 1:
  111. self.bullets.append(Bullet(round(self.x +self.w // 2),round(self.y +self.h // 2),5,(255,0,0),self.facing))
  112.  
  113. if keys[pygame.K_LEFT] and self.x > 5:
  114. self.x -= self.speed
  115. self.lastMove = "L"
  116. if keys[pygame.K_RIGHT] and self.x < win_w - self.w - 5:
  117. self.x += self.speed
  118. self.lastMove = "R"
  119. if not(self.isJump):
  120. if keys[pygame.K_UP] and self.y > 5 :
  121. self.isJump = True
  122. if keys[pygame.K_DOWN] and self.y < win_h - self.h - 5:
  123. self.y += self.speed
  124. else:
  125. if self.jumpCount >= -10:
  126. if self.jumpCount < 0:
  127. self.y += (self.jumpCount ** 2)/2
  128. else:
  129. self.y -= (self.jumpCount ** 2)/2
  130. self.jumpCount -= 1
  131. else:
  132. self.isJump = False
  133. self.jumpCount = 10
  134.  
  135. pygame.init()
  136.  
  137. pygame.display.set_caption("Dun")
  138.  
  139. player1 = Player(50, 445, 50, 50, 5)
  140. player2 = Player(450,445, 50, 50, 5)
  141.  
  142. platform1 = Platform (0,100,50,50)
  143. platform2 = Platform (250,100,200,50)
  144. platform3 = Platform (650,100,50,50)
  145. platform4 = Platform (100,200,150,50)
  146. platform5 = Platform (450,200,150,50)
  147. platform6 = Platform (0,300,50,50)
  148. platform7 = Platform (650,300,50,50)
  149. platform8 = Platform (100,400,100,100)
  150. platform9 = Platform (500,400,100,100)
  151. platform10 = Platform (300,300,100,400)
  152.  
  153. win_w = 700
  154. win_h = 500
  155.  
  156. win = pygame.display.set_mode((win_w,win_h))
  157.  
  158. col_check_bul = 0
  159.  
  160.  
  161. start = 1
  162. game = 2
  163. death = 3
  164. winner = 4
  165. buff = 5
  166. bos = 6
  167. state = game
  168.  
  169.  
  170.  
  171. run = True
  172. while run:
  173. pygame.time.delay(20)
  174. for event in pygame.event.get():
  175. if event.type == pygame.QUIT:
  176. pygame.quit()
  177.  
  178.  
  179. if state == game:
  180.  
  181. player1.move_player1()
  182. player1.shoot()
  183. player2.move_player2()
  184. player2.shoot()
  185.  
  186. if player1.bullets or player2.bullets:
  187. col_check_bul = 0
  188.  
  189.  
  190. if player1.bullets and player2.bullets:
  191. if (abs(player1.bullets[0].x - player2.bullets[0].x ) < 10) and (abs(player1.bullets[0].y - player2.bullets[0].y )< 10):
  192. col_check_bul = 1
  193. player1.bullets = []
  194. player2.bullets =[]
  195.  
  196. # if player1.bullets :
  197. # (abs(player1.bullets[0].x - player2.x)) < 30 and (abs(player1.))
  198.  
  199.  
  200.  
  201. win.fill((0,0,0))
  202.  
  203. platform1.draw_platform()
  204. platform2.draw_platform()
  205. platform3.draw_platform()
  206. platform4.draw_platform()
  207. platform5.draw_platform()
  208. platform6.draw_platform()
  209. platform7.draw_platform()
  210. platform8.draw_platform()
  211. platform9.draw_platform()
  212. platform10.draw_platform()
  213.  
  214.  
  215. player1.draw_player()
  216. if(col_check_bul == 0):
  217. for bullet in player1.bullets:
  218. bullet.draw(win)
  219. player2.draw_player()
  220. if(col_check_bul == 0):
  221. for bullet in player2.bullets:
  222. bullet.draw(win)
  223. pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment