Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- #####################################################################
- # MOUSE INPUT #
- #####################################################################
- def mouseDown (mouseX, mouseY):
- #global variables needed?
- print ("Mouse is down at " + str(mouseX) + ", " + str(mouseY))
- mouseLoc = pygame.mouse.get_pos()
- global x,y,xspeed,yspeed,gamestate
- if mouseX == 545:
- print ("Secret")
- def mouseUp (mouseX, mouseY):
- #global variables needed?
- print ("Mouse is up at " + str(mouseX) + ", " + str(mouseY))
- #####################################################################
- # KEYBOARD INPUT #
- #####################################################################
- def keyDown (key):
- #global variables needed?
- global xspeed,yspeed,gamestate,tank
- print ("Key Down")
- if gamestate == "Menu":
- if event.key == pygame.K_SPACE:
- gamestate = "In Game"
- elif event.key == pygame.K_o:
- gamestate = "Controls"
- elif gamestate == "Controls":
- if event.key == pygame.K_o:
- gamestate = "Menu"
- elif gamestate == "In Game":
- if key == pygame.K_UP:
- tank[5] = -0.50
- tank[9] = -0.50
- tank[10] = tank[10] /3
- tank[11] = tank[11] *-2.5
- tank[10] = tank[10] /1.5
- tank[14] = tank[14] + 16
- elif event.key == pygame.K_DOWN:
- tank[5] = 0.50
- tank[9] = 0.50
- tank[10] = tank[10] /3
- tank[11] = tank[11] *-2.5
- tank[10] = tank[10] /1.5
- tank[14] = tank[14] + 16
- tank[15] = tank[15] + 56
- elif event.key == pygame.K_LEFT:
- tank[4] = -0.50
- tank[8] = -0.50
- tank[14] = tank[14] - 32
- elif event.key == pygame.K_RIGHT:
- tank[4] = 0.50
- tank[8] = 0.50
- elif event.key == pygame.K_SPACE:
- tank[8] = 5
- print ("P1 Tank Shot")
- elif gamestate == "End Game":
- if event.key == pygame.K_SPACE:
- gamestate = "Menu"
- #Add keys you need, remove ones you don't
- def keyUp (key):
- #global variables needed?
- global xspeed,yspeed,tank,gamestate
- print ("Key is up")
- if gamestate == "In Game":
- if key == pygame.K_UP:
- tank[5] = 0
- tank[9] = 0
- tank[10] = tank[10] *3
- tank[11] = tank[11] /-2.5
- tank[10] = tank[10] *1.5
- tank[14] = tank[14] - 16
- elif event.key == pygame.K_DOWN:
- tank[5] = 0
- tank[9] = 0
- tank[10] = tank[10] *3
- tank[11] = tank[11] /-2.5
- tank[10] = tank[10] *1.5
- tank[14] = tank[14] - 16
- tank[15] = tank[15] - 56
- elif event.key == pygame.K_LEFT:
- tank[4] = 0
- tank[8] = 0
- tank[14] = tank[14] + 32
- elif event.key == pygame.K_RIGHT:
- tank[4] = 0
- tank[8] = 0
- elif event.key == pygame.K_m:
- print ("m")
- elif event.key == pygame.K_SPACE:
- tank[6] = tank[0]
- #Add keys you need, remove ones you don't
- #####################################################################
- # YOUR FUNCTIONS #
- #####################################################################
- def collideRectRect (x1,y1,w1,h1,x2,y2,w2,h2):
- return x1 <= x2+w2 and x1+w1 >= x2 and y1 <= y2+h2 and y1+h1 >= y2
- def collideRectRectLocation (x1,y1,w1,h1,x2,y2,w2,h2):
- if x1 <= x2+w2 and x1+w1 >= x2 and y1 <= y2+h2 and y1+h1 >= y2:
- collisionLeft = x2 + w2 - x1
- collisionRight = x1 + w1 - x2
- collisionTop = y2 + h2 - y1
- collisionBottom = y1 + h1 - y2
- if (collisionLeft <= collisionBottom and collisionLeft <= collisionTop and collisionLeft <= collisionRight):
- return "left"
- elif (collisionRight <= collisionBottom and collisionRight <= collisionTop and collisionRight <= collisionLeft):
- return "right"
- elif (collisionTop <= collisionBottom and collisionTop <= collisionLeft and collisionTop <= collisionRight):
- return "top"
- elif (collisionBottom <= collisionTop and collisionBottom <= collisionLeft and collisionBottom <= collisionRight):
- return "bottom"
- else:
- return
- "none"
- #####################################################################
- # ONE TIME SETUP #
- #####################################################################
- pygame.init()
- screen = pygame.display.set_mode([1024,768])
- pygame.display.set_caption("")
- #Global Variables
- gamestate = "Menu"
- x = 80
- y = 368
- xspeed = 0
- yspeed = 0
- font20 = pygame.font.SysFont("Due Date", 20)
- font40 = pygame.font.SysFont("Due Date", 40)
- font1 = pygame.font.SysFont("Due Date", 100)
- font2 = pygame.font.SysFont("Arial", 40)
- font10 = pygame.font.SysFont("Due Date", 40)
- font11 = pygame.font.SysFont("Due Date", 80)
- font3 = pygame.font.SysFont("Due Date", 30)
- font4 = pygame.font.SysFont("Due Date", 30)
- #player1 spawn barricade
- p1bX = 200
- p1bY = 288
- p1bW = 50
- p1bH = 192
- p1b = [p1bX, p1bY, p1bW, p1bH]
- #player1 spawn barricade
- ebX = 784
- ebY = 288
- ebW = 50
- ebH = 192
- eb = [ebX, ebY, ebW, ebH]
- tankX = 80
- tankY = 368
- tankW = 50
- tankH = 50
- tankXS = 0
- tankYS = 0
- tankSX = 80
- tankSY = 368
- tankSXS = 0
- tankSYS = 0
- tankCX = 80
- tankCY = 368
- tankCW = 50 #Barrel
- tankCH = 50
- tankSW = 25 #bullet
- tankSH = 50
- tank = [tankX, tankY, tankW, tankH, tankXS, tankYS, tankSX,tankSY, tankSXS, tankSYS, tankCW, tankCH, tankSW, tankSH, tankCX, tankCY]
- etankX = 904
- etankY = 368
- etankW = 50
- etankH = 50
- etankXS = 0
- etankYS = 0
- etank = [etankX, etankY, etankW, etankH, etankXS, etankYS]
- #####################################################################
- # GAME LOOP #
- #####################################################################
- while True:
- # ================== HANDLE EVENTS (DO NOT EDIT) ======================= #
- done = False
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- done = True
- break
- elif event.type == pygame.MOUSEBUTTONDOWN:
- mouseDown (pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1])
- elif event.type == pygame.MOUSEBUTTONUP:
- mouseUp (pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1])
- elif event.type == pygame.KEYDOWN:
- keyDown (event.key)
- elif event.type == pygame.KEYUP:
- keyUp (event.key)
- if done == True:
- break
- # ============================== MOVE STUFF ============================ #
- if gamestate == "In Game":
- tank[0] = tank[0] + tank[4]
- tank[1] = tank[1] + tank[5]
- tank[6] = tank[6] + tank[8]
- tank[7] = tank[7] + tank[9]
- tank[14] = tank[14] + tank[4]
- tank[15] = tank[15] + tank[5]
- # ============================== COLLISION ============================= #
- #PLAYER COLLISION
- if tank[0] <=50:
- tank[4] = tank[4] * 0
- elif tank[0] + tank[2] >= 974:
- tank[4] = tank[4] * 0
- if tank[1] <=50:
- tank[5] = tank[5] * 0
- elif tank[1] + tank[3] >= 718:
- tank[5] = tank[5] * 0
- #Player1 Spawn Barricade
- if collideRectRect (tank[0],tank[1],tank[2],tank[3],p1b[0],p1b[1],p1b[2],p1b[3]) == True:
- tank[4] = 0
- tank[5] = 0
- tank[8] = 0
- tank[9] = 0
- if collideRectRect (tank[14],tank[15],tank[10],tank[11],p1b[0],p1b[1],p1b[2],p1b[3]) == True:
- tank[8] = 0
- tank[9] = 0
- print ("collision")
- #BULLET COLLISION
- if tank[6] <=50:
- tank[8] = tank[8] * 0.1
- tank[6] = tank[0]
- elif tank[6] + tank[2] >= 974:
- tank[8] = tank[8] * 0
- tank[6] = tank[0]
- if tank[7] <=50:
- tank[9] = tank[9] * 0
- elif tank[7] + tank[3] >= 718:
- tank[9] = tank[9] * 0
- # ============================== DRAW STUFF ============================ #
- if gamestate == "Menu":
- screen.fill ((0,100,0))
- text = font1.render('No Mans Land', True, (0,0,0))
- text2 = font2.render('Press Space Bar To Start', True, (0,0,0))
- text3 = font2.render('Press O For Controls', True, (0,0,0))
- pygame.draw.ellipse (screen, (0,0,0), [540,250,10,10])
- screen.blit(text, [100, 250])
- pygame.draw.rect (screen, (255,255,255), [300,450,380,50])
- pygame.draw.rect (screen, (255,255,255), [300,550,380,50])
- screen.blit(text2, [300, 450])
- screen.blit(text3, [330, 550])
- elif gamestate == "Controls":
- screen.fill ((0,100,0))
- text4 = font1.render('Controls', True, (0,0,0))
- screen.blit(text4, [100, 50])
- text4 = font10.render('You must destroy all enemy', True, (0,0,0))
- screen.blit(text4, [100, 200])
- text4 = font10.render('tanks to win the game.The ', True, (0,0,0))
- screen.blit(text4, [100, 240])
- text4 = font10.render('person with the best time', True, (0,0,0))
- screen.blit(text4, [100, 280])
- text4 = font10.render('will be added to The LeaderBoard.', True, (0,0,0))
- screen.blit(text4, [100, 320])
- text4 = font10.render('Move your tank With the arrow Keys.', True, (0,0,0))
- screen.blit(text4, [50, 520])
- text4 = font10.render('Shoot with Spacebar.', True, (0,0,0))
- screen.blit(text4, [50, 580])
- elif gamestate == "In Game":
- screen.fill((85,107,47))
- pygame.draw.rect (screen, (189,183,107), [0,0,1024,50])#Top border
- pygame.draw.rect (screen, (189,183,107), [0,50,50,974])#Left border
- pygame.draw.rect (screen, (189,183,107), [50,718,974,50])#Bottom Border
- pygame.draw.rect (screen, (189,183,107), [974,50,50,674])#Right Border
- text = font4.render('Tachanka', True, (0,0,0))
- screen.blit(text, [800, 10])
- text2 = font4.render('Player1', True, (0,0,0))
- screen.blit(text2, [14, 10])
- text = font4.render('VS', True, (0,0,0))
- screen.blit(text, [480, 10])
- pygame.draw.rect (screen, (0,255,0), [tank[0],tank[1],tank[2],tank[3]])#p1Tank
- pygame.draw.rect (screen, (0,255,0), [tank[14],tank[15] + 16,tank[10]*1.5,tank[11]/3])#p1TankCannon
- pygame.draw.rect (screen, (0,255,0), [tank[6],tank[7] + 16,tank[12]*1.5,tank[13]/3])#p1TankCannonBullet
- pygame.draw.rect (screen, (169,169,169), [etank[0],etank[1],etank[2],etank[3]])#eTank
- pygame.draw.rect (screen, (169,169,169), [etank[0]-25,etank[1]+16,etank[2]*1.5,etank[3]/3])#eTankCannon
- #Obsticales
- pygame.draw.rect (screen, (189,183,107), [p1b[0],p1b[1],p1b[2],p1b[3]])#p1 spawn barrricade
- pygame.draw.rect (screen, (189,183,107), [eb[0],eb[1],eb[2],eb[3]])
- elif gamestate == "End Game":
- screen.fill ((255,255,0))
- # ===================== PYGAME STUFF (NO NOT EDIT) ===================== #
- pygame.display.flip()
- pygame.time.delay(10)
- pygame.quit ()
Add Comment
Please, Sign In to add comment