Expand|Select|Wrap|Line Numbers
- __author__ = 'MEIMEI'
- #imports system
- import sys
- #Main menu - problem is that opens in console not in a new window as hoped
- class Main:
- max_width = 5
- max_height = 5
- def __init__(self):
- self.display_menu()
- def display_menu(self):
- #options to choose from the menu
- menu_list = ['Start New Game', 'Exit']
- print('Type the number of your choice')
- print()
- #so that array starts at 1 not 0
- for i in range(1, len(menu_list) + 1):
- print(str(i) + ' ' + menu_list[i - 1])
- choice = input('Your Choice: ')
- self.menu_choice(choice)
- def menu_choice(self,choice):
- try:
- choice = int(choice)
- except ValueError:
- choice = 0
- #if 1 then run game
- if(choice == 1):
- pass
- #if 2 then do nothing
- elif(choice ==2):
- sys.exit(0)
- else:
- #in case they enter a number that isn't 1 or 2, like 4
- print("That wasn't a valid option, try again")
- self.display_menu()
- def draw_grid(self):
- height = self.max_height
- width = self.max_width
- for y in range(0, height):
- for x in range(0,width):
- y = str(y)
- x = str(x)
- #system standard output
- sys.stdout.write('?')
- sys.stdout.write('\r\n')
- menu = Main()
- #Import library
- import pygame
- from pygame.locals import *
- import math
- import random
- #Runs the game
- pygame.init()
- #size of the window
- size = width, height = 640, 500
- screen=pygame.display.set_mode(size)
- move = [False, False, False, False]
- #where the sniper starts
- sniperPosition=[150,150]
- acc=[0,0]
- GunFires=[]
- badtimer=100
- badtimer1=0
- badguys=[[640,100]]
- healthvalue=200
- pygame.mixer.init()
- #Load images from the folder
- #sniper image
- player = pygame.image.load("resources/images/Sniper.png")
- #background image
- darksky = pygame.image.load("resources/images/dark.png")
- #bunkers image
- bunker = pygame.image.load("resources/images/Bunker.png")
- #bullets from the gun
- GunFire = pygame.image.load("resources/images/Bullet.png")
- #the zombies
- badguyimg = pygame.image.load("resources/images/zombie.png")
- #the healthbar in the top left
- hp = pygame.image.load("resources/images/healthbar.png")
- #also health bar
- health = pygame.image.load("resources/images/health.png")
- #screen that says you have lost
- gameover = pygame.image.load("resources/images/gameover.png")
- #screen that says you have won
- YouWin = pygame.image.load("resources/images/youwin.png")
- #keep looping
- running = 1
- exitcode = 0
- while running:
- badtimer-=1
- #clear screen before redrawing it
- screen.fill(0)
- #draw the screen elements
- for x in range(width/darksky.get_width()+1):
- for y in range(height/darksky.get_height()+1):
- screen.blit(darksky,(x*100,y*100))
- screen.blit(bunker,(0,25))
- screen.blit(bunker,(0,225))
- #Rotate player
- position = pygame.mouse.get_pos()
- angle = math.atan2(position[1]-(sniperPosition[1]+32),position[0]-(sniperPosition[0]+26))
- sniperRotate = pygame.transform.rotate(player, 360-angle*57.29)
- sniperPosition1 = (sniperPosition[0]-sniperRotate.get_rect().width/2, sniperPosition[1]-sniperRotate.get_rect().height/2)
- screen.blit(sniperRotate, sniperPosition1)
- #Draw bullets
- for bullet in GunFires:
- index=0
- velocityX=math.cos(bullet[0])*10
- velocityY=math.sin(bullet[0])*10
- bullet[1]+=velocityX
- bullet[2]+=velocityY
- if bullet[1]<-64 or bullet[1]>640 or bullet[2]<-64 or bullet[2]>480:
- GunFires.pop(index)
- index+=1
- for projectile in GunFires:
- GunFire1 = pygame.transform.rotate(GunFire, 360-projectile[0]*57.29)
- screen.blit(GunFire1, (projectile[1], projectile[2]))
- #Draw zombies
- if badtimer==0:
- badguys.append([640, random.randint(50,430)])
- badtimer=100-(badtimer1*2)
- #increasing timer for release of the zombies
- if badtimer1>=35:
- badtimer1=35
- else:
- badtimer1+=5
- index=0
- for badguy in badguys:
- if badguy[0]<-64:
- badguys.pop(index)
- badguy[0]-=7
- #Attack bunker and damages health
- badrect=pygame.Rect(badguyimg.get_rect())
- badrect.top=badguy[1]
- badrect.left=badguy[0]
- if badrect.left<64:
- healthvalue -= random.randint(5,20)
- badguys.pop(index)
- #Check for collisions with the bullet and the zombie
- index1=0
- for bullet in GunFires:
- bulletRectangle=pygame.Rect(GunFire.get_rect())
- #unseen rectangle surrounding bullet
- bulletRectangle.left=bullet[1]
- bulletRectangle.top=bullet[2]
- if badrect.colliderect(bulletRectangle):
- acc[0]+=1
- badguys.pop(index)
- GunFires.pop(index1)
- index1+=1
- #Next bad guy coming out
- index+=1
- for badguy in badguys:
- screen.blit(badguyimg, badguy)
- #draw timer
- font = pygame.font.Font(None, 50)
- YouLivedtext = font.render(str((90000-pygame.time.get_ticks())/60000)+":"+str((90000-pygame.time.get_ticks())/1000%60).zfill(2), True, (0,0,0))
- textRectangle = YouLivedtext.get_rect()
- textRectangle.topright=[635, 5]
- screen.blit(YouLivedtext, textRectangle)
- #Draw health bar
- screen.blit(hp, (5,5))
- for health1 in range(healthvalue):
- screen.blit(health, (health1+8,8))
- #update the screen
- pygame.display.flip()
- #loop through the events
- for event in pygame.event.get():
- if event.type == pygame.KEYDOWN:
- if event.key==K_w:
- move[0]=True
- elif event.key==K_a:
- move[1]=True
- elif event.key==K_s:
- move[2]=True
- elif event.key==K_d:
- move[3]=True
- if event.type == pygame.KEYUP:
- if event.key==pygame.K_w:
- move[0]=False
- elif event.key==pygame.K_a:
- move[1]=False
- elif event.key==pygame.K_s:
- move[2]=False
- elif event.key==pygame.K_d:
- move[3]=False
- # check if the event is the X button
- if event.type==pygame.QUIT:
- # if it is quit the game
- pygame.quit()
- exit(0)
- if event.type==pygame.MOUSEBUTTONDOWN:
- position=pygame.mouse.get_pos()
- acc[1]+=1
- GunFires.append([math.atan2(position[1]-(sniperPosition1[1]+32),position[0]-(sniperPosition1[0]+26)),sniperPosition1[0]+32,sniperPosition1[1]+32])
- #Move player
- if move[0]:
- sniperPosition[1]-=5
- elif move[2]:
- sniperPosition[1]+=5
- if move[1]:
- sniperPosition[0]-=5
- elif move[3]:
- sniperPosition[0]+=5
- #Checking if you won or lost based on health and time
- if pygame.time.get_ticks()>=90000:
- running=0
- exitcode=1
- if healthvalue<=0:
- running=0
- exitcode=0
- if acc[1]!=0:
- accuracy=acc[0]*1.0/acc[1]*100
- else:
- accuracy=0
- #Win/lose display
- if exitcode==0:
- pygame.font.init()
- #Font and size of text
- font = pygame.font.Font(None, 50)
- #text that pops up on screen
- text = font.render("Your Accuracy Was: "+str(accuracy)+"%", True, (255,10,10))
- textRectangle = text.get_rect()
- textRectangle.centerx = screen.get_rect().centerx
- textRectangle.centery = screen.get_rect().centery+24
- screen.blit(gameover, (0,0))
- screen.blit(text, textRectangle)
- else:
- pygame.font.init()
- #Font and size of text
- font = pygame.font.Font(None, 50)
- #text that pops up on screen
- text = font.render("Your Accuracy Was: "+str(accuracy)+"%", True, (10,255,10))
- textRectangle = text.get_rect()
- textRectangle.centerx = screen.get_rect().centerx
- textRectangle.centery = screen.get_rect().centery+24
- screen.blit(YouWin, (0,0))
- screen.blit(text, textRectangle)
- while 1:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- exit(0)
- pygame.display.flip()