pacotaco651

culmisnigin

Jan 20th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.63 KB | None | 0 0
  1. import pygame
  2. import random
  3.  
  4.  
  5.  
  6. #####################################################################
  7. # MOUSE INPUT #
  8. #####################################################################
  9. def mouseDown (mouseX, mouseY):
  10. #global variables needed?
  11. print ("Mouse is down at " + str(mouseX) + ", " + str(mouseY))
  12. mouseLoc = pygame.mouse.get_pos()
  13. global x,y,xspeed,yspeed,gamestate
  14. if mouseX == 545:
  15. print ("Secret")
  16. def mouseUp (mouseX, mouseY):
  17. #global variables needed?
  18. print ("Mouse is up at " + str(mouseX) + ", " + str(mouseY))
  19. #####################################################################
  20. # KEYBOARD INPUT #
  21. #####################################################################
  22. def keyDown (key):
  23. #global variables needed?
  24. global xspeed,yspeed,gamestate,tank
  25. print ("Key Down")
  26. if gamestate == "Menu":
  27. if event.key == pygame.K_SPACE:
  28. gamestate = "In Game"
  29. elif event.key == pygame.K_o:
  30. gamestate = "Controls"
  31. elif gamestate == "Controls":
  32. if event.key == pygame.K_o:
  33. gamestate = "Menu"
  34.  
  35. elif gamestate == "In Game":
  36. if key == pygame.K_UP:
  37. tank[5] = -0.50
  38. tank[9] = -0.50
  39. tank[10] = tank[10] /3
  40. tank[11] = tank[11] *-2.5
  41. tank[10] = tank[10] /1.5
  42. tank[14] = tank[14] + 16
  43. elif event.key == pygame.K_DOWN:
  44. tank[5] = 0.50
  45. tank[9] = 0.50
  46. tank[10] = tank[10] /3
  47. tank[11] = tank[11] *-2.5
  48. tank[10] = tank[10] /1.5
  49. tank[14] = tank[14] + 16
  50. tank[15] = tank[15] + 56
  51. elif event.key == pygame.K_LEFT:
  52. tank[4] = -0.50
  53. tank[8] = -0.50
  54. tank[14] = tank[14] - 32
  55. elif event.key == pygame.K_RIGHT:
  56. tank[4] = 0.50
  57. tank[8] = 0.50
  58.  
  59. elif event.key == pygame.K_SPACE:
  60. tank[8] = 5
  61. print ("P1 Tank Shot")
  62. elif gamestate == "End Game":
  63. if event.key == pygame.K_SPACE:
  64. gamestate = "Menu"
  65.  
  66. #Add keys you need, remove ones you don't
  67.  
  68. def keyUp (key):
  69. #global variables needed?
  70. global xspeed,yspeed,tank,gamestate
  71. print ("Key is up")
  72. if gamestate == "In Game":
  73. if key == pygame.K_UP:
  74. tank[5] = 0
  75. tank[9] = 0
  76. tank[10] = tank[10] *3
  77. tank[11] = tank[11] /-2.5
  78. tank[10] = tank[10] *1.5
  79. tank[14] = tank[14] - 16
  80.  
  81.  
  82.  
  83.  
  84. elif event.key == pygame.K_DOWN:
  85. tank[5] = 0
  86. tank[9] = 0
  87. tank[10] = tank[10] *3
  88. tank[11] = tank[11] /-2.5
  89. tank[10] = tank[10] *1.5
  90. tank[14] = tank[14] - 16
  91. tank[15] = tank[15] - 56
  92. elif event.key == pygame.K_LEFT:
  93. tank[4] = 0
  94. tank[8] = 0
  95. tank[14] = tank[14] + 32
  96.  
  97. elif event.key == pygame.K_RIGHT:
  98. tank[4] = 0
  99. tank[8] = 0
  100. elif event.key == pygame.K_m:
  101. print ("m")
  102. elif event.key == pygame.K_SPACE:
  103. tank[6] = tank[0]
  104.  
  105.  
  106.  
  107.  
  108.  
  109. #Add keys you need, remove ones you don't
  110.  
  111.  
  112. #####################################################################
  113. # YOUR FUNCTIONS #
  114. #####################################################################
  115. def collideRectRect (x1,y1,w1,h1,x2,y2,w2,h2):
  116. return x1 <= x2+w2 and x1+w1 >= x2 and y1 <= y2+h2 and y1+h1 >= y2
  117. def collideRectRectLocation (x1,y1,w1,h1,x2,y2,w2,h2):
  118. if x1 <= x2+w2 and x1+w1 >= x2 and y1 <= y2+h2 and y1+h1 >= y2:
  119. collisionLeft = x2 + w2 - x1
  120. collisionRight = x1 + w1 - x2
  121. collisionTop = y2 + h2 - y1
  122. collisionBottom = y1 + h1 - y2
  123. if (collisionLeft <= collisionBottom and collisionLeft <= collisionTop and collisionLeft <= collisionRight):
  124. return "left"
  125. elif (collisionRight <= collisionBottom and collisionRight <= collisionTop and collisionRight <= collisionLeft):
  126. return "right"
  127. elif (collisionTop <= collisionBottom and collisionTop <= collisionLeft and collisionTop <= collisionRight):
  128. return "top"
  129. elif (collisionBottom <= collisionTop and collisionBottom <= collisionLeft and collisionBottom <= collisionRight):
  130. return "bottom"
  131. else:
  132. return
  133. "none"
  134.  
  135.  
  136.  
  137. #####################################################################
  138. # ONE TIME SETUP #
  139. #####################################################################
  140. pygame.init()
  141. screen = pygame.display.set_mode([1024,768])
  142. pygame.display.set_caption("")
  143.  
  144. #Global Variables
  145. gamestate = "Menu"
  146. x = 80
  147. y = 368
  148. xspeed = 0
  149. yspeed = 0
  150. font20 = pygame.font.SysFont("Due Date", 20)
  151. font40 = pygame.font.SysFont("Due Date", 40)
  152. font1 = pygame.font.SysFont("Due Date", 100)
  153. font2 = pygame.font.SysFont("Arial", 40)
  154. font10 = pygame.font.SysFont("Due Date", 40)
  155. font11 = pygame.font.SysFont("Due Date", 80)
  156. font3 = pygame.font.SysFont("Due Date", 30)
  157. font4 = pygame.font.SysFont("Due Date", 30)
  158.  
  159. #player1 spawn barricade
  160. p1bX = 200
  161. p1bY = 288
  162. p1bW = 50
  163. p1bH = 192
  164. p1b = [p1bX, p1bY, p1bW, p1bH]
  165.  
  166. #player1 spawn barricade
  167. ebX = 784
  168. ebY = 288
  169. ebW = 50
  170. ebH = 192
  171. eb = [ebX, ebY, ebW, ebH]
  172.  
  173.  
  174. tankX = 80
  175. tankY = 368
  176. tankW = 50
  177. tankH = 50
  178. tankXS = 0
  179. tankYS = 0
  180. tankSX = 80
  181. tankSY = 368
  182. tankSXS = 0
  183. tankSYS = 0
  184. tankCX = 80
  185. tankCY = 368
  186. tankCW = 50 #Barrel
  187. tankCH = 50
  188. tankSW = 25 #bullet
  189. tankSH = 50
  190. tank = [tankX, tankY, tankW, tankH, tankXS, tankYS, tankSX,tankSY, tankSXS, tankSYS, tankCW, tankCH, tankSW, tankSH, tankCX, tankCY]
  191.  
  192. etankX = 904
  193. etankY = 368
  194. etankW = 50
  195. etankH = 50
  196. etankXS = 0
  197. etankYS = 0
  198. etank = [etankX, etankY, etankW, etankH, etankXS, etankYS]
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208. #####################################################################
  209. # GAME LOOP #
  210. #####################################################################
  211. while True:
  212.  
  213. # ================== HANDLE EVENTS (DO NOT EDIT) ======================= #
  214. done = False
  215. for event in pygame.event.get():
  216. if event.type == pygame.QUIT:
  217. done = True
  218. break
  219. elif event.type == pygame.MOUSEBUTTONDOWN:
  220. mouseDown (pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1])
  221. elif event.type == pygame.MOUSEBUTTONUP:
  222. mouseUp (pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1])
  223. elif event.type == pygame.KEYDOWN:
  224. keyDown (event.key)
  225. elif event.type == pygame.KEYUP:
  226. keyUp (event.key)
  227. if done == True:
  228. break
  229.  
  230. # ============================== MOVE STUFF ============================ #
  231. if gamestate == "In Game":
  232. tank[0] = tank[0] + tank[4]
  233. tank[1] = tank[1] + tank[5]
  234. tank[6] = tank[6] + tank[8]
  235. tank[7] = tank[7] + tank[9]
  236. tank[14] = tank[14] + tank[4]
  237. tank[15] = tank[15] + tank[5]
  238.  
  239.  
  240.  
  241. # ============================== COLLISION ============================= #
  242. #PLAYER COLLISION
  243. if tank[0] <=50:
  244. tank[4] = tank[4] * 0
  245. elif tank[0] + tank[2] >= 974:
  246. tank[4] = tank[4] * 0
  247.  
  248. if tank[1] <=50:
  249. tank[5] = tank[5] * 0
  250. elif tank[1] + tank[3] >= 718:
  251. tank[5] = tank[5] * 0
  252.  
  253. #Player1 Spawn Barricade
  254. if collideRectRect (tank[0],tank[1],tank[2],tank[3],p1b[0],p1b[1],p1b[2],p1b[3]) == True:
  255. tank[4] = 0
  256. tank[5] = 0
  257. tank[8] = 0
  258. tank[9] = 0
  259.  
  260.  
  261.  
  262. if collideRectRect (tank[14],tank[15],tank[10],tank[11],p1b[0],p1b[1],p1b[2],p1b[3]) == True:
  263. tank[8] = 0
  264. tank[9] = 0
  265. print ("collision")
  266.  
  267.  
  268.  
  269.  
  270.  
  271. #BULLET COLLISION
  272. if tank[6] <=50:
  273. tank[8] = tank[8] * 0.1
  274. tank[6] = tank[0]
  275. elif tank[6] + tank[2] >= 974:
  276. tank[8] = tank[8] * 0
  277. tank[6] = tank[0]
  278.  
  279. if tank[7] <=50:
  280. tank[9] = tank[9] * 0
  281. elif tank[7] + tank[3] >= 718:
  282. tank[9] = tank[9] * 0
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295. # ============================== DRAW STUFF ============================ #
  296. if gamestate == "Menu":
  297. screen.fill ((0,100,0))
  298. text = font1.render('No Mans Land', True, (0,0,0))
  299. text2 = font2.render('Press Space Bar To Start', True, (0,0,0))
  300. text3 = font2.render('Press O For Controls', True, (0,0,0))
  301. pygame.draw.ellipse (screen, (0,0,0), [540,250,10,10])
  302. screen.blit(text, [100, 250])
  303. pygame.draw.rect (screen, (255,255,255), [300,450,380,50])
  304. pygame.draw.rect (screen, (255,255,255), [300,550,380,50])
  305. screen.blit(text2, [300, 450])
  306. screen.blit(text3, [330, 550])
  307.  
  308. elif gamestate == "Controls":
  309. screen.fill ((0,100,0))
  310. text4 = font1.render('Controls', True, (0,0,0))
  311. screen.blit(text4, [100, 50])
  312. text4 = font10.render('You must destroy all enemy', True, (0,0,0))
  313. screen.blit(text4, [100, 200])
  314. text4 = font10.render('tanks to win the game.The ', True, (0,0,0))
  315. screen.blit(text4, [100, 240])
  316. text4 = font10.render('person with the best time', True, (0,0,0))
  317. screen.blit(text4, [100, 280])
  318. text4 = font10.render('will be added to The LeaderBoard.', True, (0,0,0))
  319. screen.blit(text4, [100, 320])
  320. text4 = font10.render('Move your tank With the arrow Keys.', True, (0,0,0))
  321. screen.blit(text4, [50, 520])
  322. text4 = font10.render('Shoot with Spacebar.', True, (0,0,0))
  323. screen.blit(text4, [50, 580])
  324.  
  325.  
  326.  
  327.  
  328. elif gamestate == "In Game":
  329. screen.fill((85,107,47))
  330.  
  331. pygame.draw.rect (screen, (189,183,107), [0,0,1024,50])#Top border
  332. pygame.draw.rect (screen, (189,183,107), [0,50,50,974])#Left border
  333. pygame.draw.rect (screen, (189,183,107), [50,718,974,50])#Bottom Border
  334. pygame.draw.rect (screen, (189,183,107), [974,50,50,674])#Right Border
  335. text = font4.render('Tachanka', True, (0,0,0))
  336. screen.blit(text, [800, 10])
  337. text2 = font4.render('Player1', True, (0,0,0))
  338. screen.blit(text2, [14, 10])
  339. text = font4.render('VS', True, (0,0,0))
  340. screen.blit(text, [480, 10])
  341. pygame.draw.rect (screen, (0,255,0), [tank[0],tank[1],tank[2],tank[3]])#p1Tank
  342. pygame.draw.rect (screen, (0,255,0), [tank[14],tank[15] + 16,tank[10]*1.5,tank[11]/3])#p1TankCannon
  343. pygame.draw.rect (screen, (0,255,0), [tank[6],tank[7] + 16,tank[12]*1.5,tank[13]/3])#p1TankCannonBullet
  344. pygame.draw.rect (screen, (169,169,169), [etank[0],etank[1],etank[2],etank[3]])#eTank
  345.  
  346. pygame.draw.rect (screen, (169,169,169), [etank[0]-25,etank[1]+16,etank[2]*1.5,etank[3]/3])#eTankCannon
  347.  
  348.  
  349.  
  350. #Obsticales
  351. pygame.draw.rect (screen, (189,183,107), [p1b[0],p1b[1],p1b[2],p1b[3]])#p1 spawn barrricade
  352. pygame.draw.rect (screen, (189,183,107), [eb[0],eb[1],eb[2],eb[3]])
  353.  
  354.  
  355.  
  356.  
  357. elif gamestate == "End Game":
  358. screen.fill ((255,255,0))
  359.  
  360.  
  361.  
  362. # ===================== PYGAME STUFF (NO NOT EDIT) ===================== #
  363. pygame.display.flip()
  364. pygame.time.delay(10)
  365.  
  366. pygame.quit ()
Add Comment
Please, Sign In to add comment