Advertisement
C-Gian

MyGame

Dec 31st, 2019
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 17.86 KB | None | 0 0
  1. import pygame
  2. import random
  3. import time
  4. import math
  5.  
  6. pygame.init()
  7.  
  8. display_width = 950
  9. display_height = 700
  10. screen = pygame.display.set_mode((display_width, display_height))
  11. fps = 60
  12. font_ubuntumono = pygame.font.SysFont('ubuntumono', 50, True)
  13. rock_image = pygame.image.load('cave-painting.png').convert_alpha()
  14. rock_width = rock_image.get_width()
  15. rock_height = rock_image.get_height()
  16. player_image = pygame.image.load('spaceship.png').convert_alpha()
  17. original_player_image = pygame.image.load('spaceship.png').convert_alpha()
  18. gameover_image = pygame.image.load('gameover.png').convert_alpha()
  19. boss_image = pygame.image.load('asteroid.png').convert_alpha()
  20. boss_height = 32
  21. boss_width = boss_image.get_width()
  22. background = pygame.image.load('background.PNG').convert_alpha()
  23. half_life = pygame.image.load('1 life.png').convert_alpha()#pygame.image.load('half life.png').convert_alpha()
  24. one_life = pygame.image.load('1 life.png').convert_alpha()
  25. one_life_half = pygame.image.load('1 and half life.png').convert_alpha()
  26. two_life = pygame.image.load('2 life.png').convert_alpha()
  27. two_life_half = pygame.image.load('2 and half life.png').convert_alpha()
  28. three_life = pygame.image.load('3 life.png').convert_alpha()
  29. laser_beam = pygame.image.load('laser.png').convert_alpha()
  30.  
  31. def initialize():
  32.     global speed_rock, tick, rocks, bullet_change, bullets, score, life, play_again, pause, go_menu, player1, fighting_boss, boss_life, boss, boss_not_present, laser1
  33.     speed_rock = 2
  34.     tick = 0
  35.     bullet_change = 2
  36.     boss_life = 20
  37.     score = 0
  38.     life = 6
  39.     pause = False
  40.     fighting_boss = False
  41.     boss_not_present = False
  42.     go_menu = True
  43.     play_again = False
  44.     bullets = []
  45.     rocks = []
  46.     rocks.append(RockClass())
  47.     player1 = Player(display_width // 2, display_height - 50, player_image)
  48.     laser1 = Laser(player1.x, player1.y)
  49.     boss = Boss(15, 5)
  50.  
  51. def update():
  52.     pygame.display.update()
  53.     pygame.time.Clock().tick(fps)
  54.  
  55. def drawBackground():
  56.     #draw the background
  57.     screen.blit( background, (0,0) )
  58.  
  59. def drawRocks():
  60.     #draw rocks
  61.     for rock2 in rocks:
  62.         rock2.continueDrawRock()
  63.  
  64. def drawBullets():
  65.     for bullet2 in bullets:
  66.         bullet2.rotate()
  67.         bullet2.continueDrawBullet()
  68.  
  69. def drawLaser():
  70.     laser1.continueDrawLaser()
  71.  
  72. def drawBoss():
  73.     global fighting_boss
  74.     if boss.bossDefeat():
  75.         fighting_boss = False
  76.  
  77.     if fighting_boss:
  78.         if tick > 300:
  79.             boss.continueDrawBoss()
  80.  
  81. def drawScore():
  82.     font = pygame.font.SysFont('ubuntumono', 20, True)
  83.     score_surface = font.render("Score : " + str(score), True, (255, 255, 255))
  84.     screen.blit(score_surface, (10,10) )
  85.  
  86. def drawLife():
  87.     if life == 6:
  88.         screen.blit( three_life, (display_width - 130, 10) )
  89.     if life == 5:
  90.         screen.blit( two_life_half, (display_width - 130, 10) )
  91.     if life == 4:
  92.         screen.blit( two_life, (display_width - 130, 10))
  93.     if life == 3:
  94.         screen.blit( one_life_half, (display_width - 130, 10) )
  95.     if life == 2:
  96.         screen.blit( one_life, (display_width - 130, 10) )
  97.     if life == 1:
  98.         screen.blit( half_life, (display_width - 130, 10) )
  99.  
  100.     font = pygame.font.SysFont('ubuntumono', 20, True)
  101.     player_life_surface = font.render("Life : ", True, (255, 255, 255))
  102.     boss_life_surface = font.render("Boss Life : " + str(boss.boss_life), True, (255, 255, 255))
  103.     if fighting_boss:
  104.         if len(str(boss.boss_life)) == 1:
  105.             screen.blit(boss_life_surface, (display_width - 160, 30))
  106.         else:
  107.             screen.blit(boss_life_surface, (display_width-173,30) )
  108.  
  109. def buttonIntro(messaggio, x, y, w, h, colore_chiaro, colore_scuro, action=None):
  110.     mouse = pygame.mouse.get_pos()
  111.     click = pygame.mouse.get_pressed()
  112.     if x < mouse[0] < x + w and y < mouse[1] < y + h:
  113.         pygame.draw.rect(screen, colore_scuro, (x, y, w, h))
  114.         if click[0] == 1 and action != None:
  115.             if action == 'play':
  116.                 global go_menu
  117.                 go_menu = False
  118.             elif action == 'quit':
  119.                 pygame.quit()
  120.                 quit()
  121.     else:
  122.         pygame.draw.rect(screen, colore_chiaro, (x, y, w, h))
  123.  
  124.     testo_green_button = pygame.font.SysFont('ubuntumono', 20, True)
  125.     green_button_surface = testo_green_button.render(messaggio, True, (0, 0, 0))
  126.     green_button_rect = green_button_surface.get_rect()
  127.     green_button_rect.center = (x + (w // 2), (y + (h // 2)))
  128.  
  129.     screen.blit(green_button_surface, green_button_rect)
  130.  
  131. def gameIntro():
  132.     global go_menu
  133.     while go_menu:
  134.         for event in pygame.event.get():
  135.             if event.type == pygame.QUIT:
  136.                 pygame.quit()
  137.                 quit()
  138.  
  139.         screen.fill( (255, 255, 255) )
  140.         testo_menu = pygame.font.SysFont('comicsans', 70, True)
  141.         menu_surface = testo_menu.render( 'MENÙ', True, (0,0,0) )
  142.         menu_rect = menu_surface.get_rect()
  143.         menu_rect.center = ( (display_width//2), (display_height//2)-100 )
  144.         screen.blit(menu_surface, menu_rect)
  145.  
  146.         #creo il bottone verde con scritto start
  147.         buttonIntro( 'start', (display_width // 2) - 125 // 2, 180, 125, 25, (255,0,0), (200,0,0), "play")
  148.         #creo il bottone rosso con scritto quit
  149.         buttonIntro('quit', (display_width // 2) - 125 // 2, 210, 125, 25, (0, 255, 0), (0, 200, 0), "quit")
  150.  
  151.         pygame.display.update()
  152.         pygame.time.Clock().tick(fps//2)
  153.  
  154. def gamePause():
  155.     global pause
  156.     while pause:
  157.         for event in pygame.event.get():
  158.             if event.type == pygame.KEYDOWN and event.key == pygame.K_c:
  159.                 pause = False
  160.  
  161.             if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
  162.                 global go_menu
  163.                 go_menu = True
  164.                 pause = False
  165.                 initialize()
  166.  
  167.             if event.type == pygame.QUIT:
  168.                 pygame.quit()
  169.                 quit()
  170.         screen.fill( (255, 255, 255) )
  171.  
  172.         testo_pausa = pygame.font.SysFont('comicsans', 70, True)
  173.         pausa_surface = testo_pausa.render( 'PAUSA', True, (0,0,0) )
  174.         pausa_rect = pausa_surface.get_rect()
  175.         pausa_rect.center = ( (display_width//2), (display_height//2) )
  176.         screen.blit(pausa_surface, pausa_rect)
  177.  
  178.         pygame.display.update()
  179.         pygame.time.Clock().tick(fps//2)
  180.  
  181. def gameOver():
  182.     global life
  183.     global go_menu
  184.     if life <= 0:
  185.         screen.blit( gameover_image, (0, 0) )
  186.         update()
  187.         play_again = False
  188.         while not play_again:
  189.             for event in pygame.event.get():
  190.  
  191.                 #if key pressed is space than start new game
  192.                 if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
  193.                     initialize()
  194.                     play_again = True
  195.                     go_menu = False
  196.  
  197.                 #if key pressed is esc then go to menu
  198.                 if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
  199.                     go_menu = True
  200.                     play_again = True
  201.                     initialize()
  202.  
  203.                 #quit
  204.                 if event.type == pygame.QUIT:
  205.                     pygame.quit()
  206.                     quit()
  207.  
  208. def checkBoss():
  209.     global fighting_boss, boss_not_present
  210.     if score == 1:
  211.         boss_not_present = True
  212.         fighting_boss = True
  213.  
  214.  
  215.  
  216. class Player:
  217.     #inizialize the player
  218.     def __init__(self, x, y, player):
  219.         self.x = x
  220.         self.y = y
  221.         self.image = player
  222.         self.is_shooting = False
  223.         self.original_player_image = player
  224.         self.angle = 0
  225.  
  226.     #move the player
  227.     def movePlayer(self):
  228.         if key[0]:
  229.             self.x -= 10
  230.         elif key[1]:
  231.             self.x += 10
  232.         if key[2]:
  233.             self.y -= 10
  234.         elif key[3]:
  235.             self.y += 10
  236.  
  237.         #check borders
  238.         if self.x <= 0:
  239.             self.x = 0
  240.         if self.x + rock_image.get_width() >= display_width:
  241.             self.x = display_width - player_image.get_width()
  242.         if self.y <= 0:
  243.             self.y = 0
  244.         if self.y + player_image.get_height() >= display_height:
  245.             self.y = display_height - player_image.get_height()
  246.  
  247.     #rotate the player where the mouse is aiming
  248.     def rotate(self):
  249.         mouse_x, mouse_y = pygame.mouse.get_pos()
  250.         rel_x, rel_y = mouse_x - self.x, mouse_y - self.y
  251.         self.angle = (180 / math.pi) * -math.atan2(rel_y, rel_x) - 90
  252.         self.image = pygame.transform.rotate(self.original_player_image, int(self.angle))
  253.         self.rect = self.image.get_rect()
  254.  
  255.     #draw the player
  256.     def drawPlayer(self):
  257.         screen.blit(self.image, (self.x, self.y))
  258.  
  259. class Bullet:
  260.     #initialize the bullet
  261.     def __init__(self, player_x, player_y):
  262.         self.x = player_x
  263.         self.y = player_y
  264.         self.image = laser_beam
  265.         self.original_image = laser_beam
  266.         self.angle = player1.angle
  267.         self.vel_x = math.cos(self.angle) * 5
  268.         self.vel_y = math.sin(self.angle) * 5
  269.  
  270.     #draw the bullet
  271.     def continueDrawBullet(self):
  272.         self.x += self.vel_x
  273.         self.y += self.vel_y
  274.         if self.x < -laser_beam.get_width() or self.x >= display_width+laser_beam.get_width()\
  275.                 or self.y < -laser_beam.get_height() or self.y >= display_height + laser_beam.get_height():
  276.             bullets.pop(bullets.index(self))
  277.  
  278.         screen.blit(self.image, (self.x, self.y))
  279.  
  280.     #rotate the bullet to the new angle
  281.     def rotate(self):
  282.         self.image = pygame.transform.rotate( self.original_image, ((180 / math.pi) * -math.atan2(self.vel_y, self.vel_x) - 90) )
  283.         self.rect = self.image.get_rect()
  284.  
  285.     #check if the bullet collides with rocks and in that case increment the score
  286.     def collisionBulletRock(self, rock2, rock_image):
  287.         global score
  288.         rock_rect_bullet = pygame.Rect(rock2.x, rock2.y, rock_image.get_width(), rock_image.get_height() )
  289.         rock_bullet_rect = pygame.Rect(self.x, self.y, laser_beam.get_width(), laser_beam.get_height() )
  290.         # check collision
  291.         if rock_rect_bullet.colliderect(rock_bullet_rect):
  292.             bullets.pop(bullets.index(self))
  293.             rock2.x = 5000
  294.             score += 1
  295.  
  296.     #check if the bullet collides with rocks and in that case increment the score
  297.     def collisionBulletBoss(self, boss, boss_image):
  298.         global score
  299.         boss_rect_bullet = pygame.Rect(boss.x, boss.y, boss_image.get_width(), boss_image.get_height() )
  300.         boss_bullet_rect = pygame.Rect(self.x, self.y, laser_beam.get_width(), laser_beam.get_height() )
  301.         # check collision
  302.         if boss_rect_bullet.colliderect(boss_bullet_rect):
  303.             bullets.pop(bullets.index(self))
  304.             boss.boss_life -= 1
  305.  
  306. class RockClass:
  307.     #inizialize the rocks
  308.     def __init__(self):
  309.         self.x = random.uniform(0, display_width)
  310.         self.y = 0
  311.         self.angle_x = random.uniform(-5, 5)
  312.         self.w = rock_width
  313.         self.speed_rock = speed_rock
  314.  
  315.     #draw the rocks
  316.     def continueDrawRock(self):
  317.         self.y += self.speed_rock
  318.         if self.x <= 0 or self.x + self.w >= display_width:
  319.             self.angle_x = self.angle_x * -1
  320.         self.x += self.angle_x
  321.  
  322.         screen.blit(rock_image, (self.x, self.y))
  323.  
  324.     #check if rock collides with player and in that case remove 1 life
  325.     def collisionRock(self, player1, player):
  326.         player_rect = pygame.Rect(player1.x, player1.y, player.get_width(), player.get_height())
  327.         rock_rect = pygame.Rect(self.x, self.y, rock_width, rock_height)
  328.         if player_rect.colliderect(rock_rect):
  329.             rocks.pop(rocks.index(rock2))
  330.             global life
  331.             life -= 1
  332.  
  333. class Boss:
  334.     #inizialize the boss
  335.     def __init__(self, boss_life, speed_boss):
  336.         self.x = random.uniform(10, display_width-100)
  337.         self.y = 0
  338.         self.angle = random.uniform(-5, 5)
  339.         self.speed_boss = speed_boss
  340.         self.boss_life = boss_life
  341.         self.image = boss_image
  342.         self.original_boss_image = boss_image
  343.         self.w = boss_width
  344.         self.h = boss_height
  345.  
  346.     #draw the boss
  347.     def continueDrawBoss(self):
  348.         self.image = pygame.transform.scale(self.original_boss_image, (self.w, self.h))
  349.         self.y += self.speed_boss
  350.         if self.x <= 0 or self.x + self.w >= display_width-10:
  351.             self.angle = self.angle * -1
  352.             self.w += 1
  353.             self.h += 1
  354.         if self.y <= 0 or self.y + self.h >= display_height-10:
  355.             self.speed_boss = -self.speed_boss
  356.             self.w += 1
  357.             self.h += 1
  358.         self.x += self.angle
  359.  
  360.         screen.blit(self.image, (self.x, self.y))
  361.  
  362.     #check if boss collides with player and in that case remove 50 life
  363.     def collisionBoss(self, player1, player):
  364.         player_rect = pygame.Rect(player1.x, player1.y, player.get_width(), player.get_height())
  365.         boss_rect = pygame.Rect(self.x, self.y, self.w, self.h)
  366.         if player_rect.colliderect(boss_rect):
  367.             global life
  368.             life -= 50
  369.  
  370.     def bossDefeat(self):
  371.         global score, fighting_boss, boss_not_present
  372.         if boss_not_present and self.boss_life <= 0:
  373.             score += 50
  374.             boss_not_present = False
  375.             fighting_boss = False
  376.             return True
  377.  
  378. class Laser:
  379.     def __init__(self, player_x, player_y):
  380.         self.x = player_x
  381.         self.y = player_y
  382.         self.original_image = pygame.Surface((5, 150))
  383.         self.original_image.set_colorkey( (0,0,0) )
  384.         self.original_image.fill( (255,0,0) )
  385.         self.copy_image = self.original_image.copy()
  386.         self.copy_image.set_colorkey( (0,0,0) )
  387.         self.rect = self.copy_image.get_rect()
  388.         self.rect.center = self.x + player_image.get_width()//2, self.y - 75
  389.         self.new_image = pygame.Surface((5, 150))
  390.  
  391.     def continueDrawLaser(self):
  392.         if laser_bool:
  393.             screen.blit(self.new_image, self.rect)
  394.  
  395.     def rotate(self):
  396.         mouse_x, mouse_y = pygame.mouse.get_pos()
  397.         rel_x, rel_y = mouse_x - player1.x + player_image.get_width()//2, mouse_y - player1.y - 75
  398.         angle = (180 / math.pi) * -math.atan2(rel_y, rel_x) - 85
  399.         self.new_image = pygame.transform.rotate(self.original_image, angle)
  400.         self.rect = self.new_image.get_rect()
  401.         self.rect.center = player1.x + player_image.get_width()//2,  player1.y - 75
  402.  
  403.  
  404. #inizialize the all game var
  405. initialize()
  406.  
  407.  
  408. #temp var
  409. laser_bool = False
  410. boss_not_present = False
  411. key = key = [False, False, False, False]
  412. mouse = False
  413. temp = False
  414. running = True
  415.  
  416. #main loop
  417. while running:
  418.  
  419.     #event loop
  420.     for event in pygame.event.get():
  421.  
  422.         #move player if keys are pressed
  423.         if event.type == pygame.KEYDOWN:
  424.             if event.key == pygame.K_a:
  425.                 key[0] = True
  426.             if event.key == pygame.K_d:
  427.                 key[1] = True
  428.             if event.key == pygame.K_w:
  429.                 key[2] = True
  430.             if event.key == pygame.K_s:
  431.                 key[3] = True
  432.  
  433.             if event.key == pygame.K_SPACE:
  434.                 laser_bool = True
  435.  
  436.             #if key pressed is esc then go to menu
  437.             if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
  438.                 global go_menu
  439.                 go_menu = True
  440.                 initialize()
  441.  
  442.             #if key pressed is c then pause
  443.             if event.key == pygame.K_c:
  444.                 global pause
  445.                 pause = True
  446.  
  447.         #if mouse is pressed then shoot
  448.         if event.type == pygame.MOUSEBUTTONDOWN:
  449.             mouse = True
  450.             temp = True
  451.  
  452.         # if mouse is not pressed then stop shooting
  453.         if event.type == pygame.MOUSEBUTTONUP:
  454.             mouse = False
  455.  
  456.         #stop player if keys are not pressed
  457.         if event.type == pygame.KEYUP:
  458.             if event.key == pygame.K_a:
  459.                 key[0] = False
  460.             if event.key == pygame.K_d:
  461.                 key[1] = False
  462.             if event.key == pygame.K_w:
  463.                 key[2] = False
  464.             if event.key == pygame.K_s:
  465.                 key[3] = False
  466.  
  467.             if event.key == pygame.K_SPACE:
  468.                 laser_bool = False
  469.  
  470.         #quit
  471.         if event.type == pygame.QUIT:
  472.             running = False
  473.             pygame.quit()
  474.             quit()
  475.  
  476.     if mouse:
  477.         mouse_x, mouse_y = pygame.mouse.get_pos()
  478.         rel_x, rel_y = mouse_x - player1.x, mouse_y - player1.y
  479.         player1.angle = math.atan2(rel_y, rel_x)
  480.         bullets.append( Bullet(player1.x, player1.y) )
  481.  
  482.     if laser_bool:
  483.         laser1.rotate()
  484.  
  485.     checkBoss()
  486.  
  487.     if fighting_boss:
  488.         tick += 1
  489.  
  490.     #move and rotate the player
  491.     player1.movePlayer()
  492.     player1.rotate()
  493.  
  494.     if not fighting_boss:
  495.         # to generate infinite rocks
  496.         if len(rocks) == 0 or rocks[-1].y > 40:
  497.             rocks.append( RockClass() )
  498.  
  499.         # check the player-rock collision
  500.         for rock2 in rocks:
  501.             rock2.collisionRock(player1, player_image)
  502.             if temp:
  503.                 for bullet2 in bullets:
  504.                     bullet2.collisionBulletRock(rock2, rock_image)
  505.  
  506.     if fighting_boss:
  507.         #check if the boss collide with the player
  508.         boss.collisionBoss(player1, player_image)
  509.  
  510.         # check the player-rock collision
  511.         for rock2 in rocks:
  512.             rock2.collisionRock(player1, player_image)
  513.             for bullet2 in bullets:
  514.                 bullet2.collisionBulletRock(rock2, rock_image)
  515.         if tick > 300:
  516.             for bullet2 in bullets:
  517.                 bullet2.collisionBulletBoss(boss, boss_image)
  518.  
  519.     #update screen
  520.     gameIntro()
  521.     gamePause()
  522.     gameOver()
  523.     drawBackground()
  524.     drawScore()
  525.     drawLife()
  526.     drawRocks()
  527.     drawBoss()
  528.     drawBullets()
  529.     player1.drawPlayer()
  530.     drawLaser()
  531.     update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement