Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- import time
- import math
- pygame.init()
- display_width = 950
- display_height = 700
- screen = pygame.display.set_mode((display_width, display_height))
- fps = 60
- font_ubuntumono = pygame.font.SysFont('ubuntumono', 50, True)
- rock_image = pygame.image.load('cave-painting.png').convert_alpha()
- rock_width = rock_image.get_width()
- rock_height = rock_image.get_height()
- player_image = pygame.image.load('spaceship.png').convert_alpha()
- original_player_image = pygame.image.load('spaceship.png').convert_alpha()
- gameover_image = pygame.image.load('gameover.png').convert_alpha()
- boss_image = pygame.image.load('asteroid.png').convert_alpha()
- boss_height = 32
- boss_width = boss_image.get_width()
- background = pygame.image.load('background.PNG').convert_alpha()
- half_life = pygame.image.load('1 life.png').convert_alpha()#pygame.image.load('half life.png').convert_alpha()
- one_life = pygame.image.load('1 life.png').convert_alpha()
- one_life_half = pygame.image.load('1 and half life.png').convert_alpha()
- two_life = pygame.image.load('2 life.png').convert_alpha()
- two_life_half = pygame.image.load('2 and half life.png').convert_alpha()
- three_life = pygame.image.load('3 life.png').convert_alpha()
- laser_beam = pygame.image.load('laser.png').convert_alpha()
- def initialize():
- 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
- speed_rock = 2
- tick = 0
- bullet_change = 2
- boss_life = 20
- score = 0
- life = 6
- pause = False
- fighting_boss = False
- boss_not_present = False
- go_menu = True
- play_again = False
- bullets = []
- rocks = []
- rocks.append(RockClass())
- player1 = Player(display_width // 2, display_height - 50, player_image)
- laser1 = Laser(player1.x, player1.y)
- boss = Boss(15, 5)
- def update():
- pygame.display.update()
- pygame.time.Clock().tick(fps)
- def drawBackground():
- #draw the background
- screen.blit( background, (0,0) )
- def drawRocks():
- #draw rocks
- for rock2 in rocks:
- rock2.continueDrawRock()
- def drawBullets():
- for bullet2 in bullets:
- bullet2.rotate()
- bullet2.continueDrawBullet()
- def drawLaser():
- laser1.continueDrawLaser()
- def drawBoss():
- global fighting_boss
- if boss.bossDefeat():
- fighting_boss = False
- if fighting_boss:
- if tick > 300:
- boss.continueDrawBoss()
- def drawScore():
- font = pygame.font.SysFont('ubuntumono', 20, True)
- score_surface = font.render("Score : " + str(score), True, (255, 255, 255))
- screen.blit(score_surface, (10,10) )
- def drawLife():
- if life == 6:
- screen.blit( three_life, (display_width - 130, 10) )
- if life == 5:
- screen.blit( two_life_half, (display_width - 130, 10) )
- if life == 4:
- screen.blit( two_life, (display_width - 130, 10))
- if life == 3:
- screen.blit( one_life_half, (display_width - 130, 10) )
- if life == 2:
- screen.blit( one_life, (display_width - 130, 10) )
- if life == 1:
- screen.blit( half_life, (display_width - 130, 10) )
- font = pygame.font.SysFont('ubuntumono', 20, True)
- player_life_surface = font.render("Life : ", True, (255, 255, 255))
- boss_life_surface = font.render("Boss Life : " + str(boss.boss_life), True, (255, 255, 255))
- if fighting_boss:
- if len(str(boss.boss_life)) == 1:
- screen.blit(boss_life_surface, (display_width - 160, 30))
- else:
- screen.blit(boss_life_surface, (display_width-173,30) )
- def buttonIntro(messaggio, x, y, w, h, colore_chiaro, colore_scuro, action=None):
- mouse = pygame.mouse.get_pos()
- click = pygame.mouse.get_pressed()
- if x < mouse[0] < x + w and y < mouse[1] < y + h:
- pygame.draw.rect(screen, colore_scuro, (x, y, w, h))
- if click[0] == 1 and action != None:
- if action == 'play':
- global go_menu
- go_menu = False
- elif action == 'quit':
- pygame.quit()
- quit()
- else:
- pygame.draw.rect(screen, colore_chiaro, (x, y, w, h))
- testo_green_button = pygame.font.SysFont('ubuntumono', 20, True)
- green_button_surface = testo_green_button.render(messaggio, True, (0, 0, 0))
- green_button_rect = green_button_surface.get_rect()
- green_button_rect.center = (x + (w // 2), (y + (h // 2)))
- screen.blit(green_button_surface, green_button_rect)
- def gameIntro():
- global go_menu
- while go_menu:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- quit()
- screen.fill( (255, 255, 255) )
- testo_menu = pygame.font.SysFont('comicsans', 70, True)
- menu_surface = testo_menu.render( 'MENÙ', True, (0,0,0) )
- menu_rect = menu_surface.get_rect()
- menu_rect.center = ( (display_width//2), (display_height//2)-100 )
- screen.blit(menu_surface, menu_rect)
- #creo il bottone verde con scritto start
- buttonIntro( 'start', (display_width // 2) - 125 // 2, 180, 125, 25, (255,0,0), (200,0,0), "play")
- #creo il bottone rosso con scritto quit
- buttonIntro('quit', (display_width // 2) - 125 // 2, 210, 125, 25, (0, 255, 0), (0, 200, 0), "quit")
- pygame.display.update()
- pygame.time.Clock().tick(fps//2)
- def gamePause():
- global pause
- while pause:
- for event in pygame.event.get():
- if event.type == pygame.KEYDOWN and event.key == pygame.K_c:
- pause = False
- if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
- global go_menu
- go_menu = True
- pause = False
- initialize()
- if event.type == pygame.QUIT:
- pygame.quit()
- quit()
- screen.fill( (255, 255, 255) )
- testo_pausa = pygame.font.SysFont('comicsans', 70, True)
- pausa_surface = testo_pausa.render( 'PAUSA', True, (0,0,0) )
- pausa_rect = pausa_surface.get_rect()
- pausa_rect.center = ( (display_width//2), (display_height//2) )
- screen.blit(pausa_surface, pausa_rect)
- pygame.display.update()
- pygame.time.Clock().tick(fps//2)
- def gameOver():
- global life
- global go_menu
- if life <= 0:
- screen.blit( gameover_image, (0, 0) )
- update()
- play_again = False
- while not play_again:
- for event in pygame.event.get():
- #if key pressed is space than start new game
- if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
- initialize()
- play_again = True
- go_menu = False
- #if key pressed is esc then go to menu
- if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
- go_menu = True
- play_again = True
- initialize()
- #quit
- if event.type == pygame.QUIT:
- pygame.quit()
- quit()
- def checkBoss():
- global fighting_boss, boss_not_present
- if score == 1:
- boss_not_present = True
- fighting_boss = True
- class Player:
- #inizialize the player
- def __init__(self, x, y, player):
- self.x = x
- self.y = y
- self.image = player
- self.is_shooting = False
- self.original_player_image = player
- self.angle = 0
- #move the player
- def movePlayer(self):
- if key[0]:
- self.x -= 10
- elif key[1]:
- self.x += 10
- if key[2]:
- self.y -= 10
- elif key[3]:
- self.y += 10
- #check borders
- if self.x <= 0:
- self.x = 0
- if self.x + rock_image.get_width() >= display_width:
- self.x = display_width - player_image.get_width()
- if self.y <= 0:
- self.y = 0
- if self.y + player_image.get_height() >= display_height:
- self.y = display_height - player_image.get_height()
- #rotate the player where the mouse is aiming
- def rotate(self):
- mouse_x, mouse_y = pygame.mouse.get_pos()
- rel_x, rel_y = mouse_x - self.x, mouse_y - self.y
- self.angle = (180 / math.pi) * -math.atan2(rel_y, rel_x) - 90
- self.image = pygame.transform.rotate(self.original_player_image, int(self.angle))
- self.rect = self.image.get_rect()
- #draw the player
- def drawPlayer(self):
- screen.blit(self.image, (self.x, self.y))
- class Bullet:
- #initialize the bullet
- def __init__(self, player_x, player_y):
- self.x = player_x
- self.y = player_y
- self.image = laser_beam
- self.original_image = laser_beam
- self.angle = player1.angle
- self.vel_x = math.cos(self.angle) * 5
- self.vel_y = math.sin(self.angle) * 5
- #draw the bullet
- def continueDrawBullet(self):
- self.x += self.vel_x
- self.y += self.vel_y
- if self.x < -laser_beam.get_width() or self.x >= display_width+laser_beam.get_width()\
- or self.y < -laser_beam.get_height() or self.y >= display_height + laser_beam.get_height():
- bullets.pop(bullets.index(self))
- screen.blit(self.image, (self.x, self.y))
- #rotate the bullet to the new angle
- def rotate(self):
- self.image = pygame.transform.rotate( self.original_image, ((180 / math.pi) * -math.atan2(self.vel_y, self.vel_x) - 90) )
- self.rect = self.image.get_rect()
- #check if the bullet collides with rocks and in that case increment the score
- def collisionBulletRock(self, rock2, rock_image):
- global score
- rock_rect_bullet = pygame.Rect(rock2.x, rock2.y, rock_image.get_width(), rock_image.get_height() )
- rock_bullet_rect = pygame.Rect(self.x, self.y, laser_beam.get_width(), laser_beam.get_height() )
- # check collision
- if rock_rect_bullet.colliderect(rock_bullet_rect):
- bullets.pop(bullets.index(self))
- rock2.x = 5000
- score += 1
- #check if the bullet collides with rocks and in that case increment the score
- def collisionBulletBoss(self, boss, boss_image):
- global score
- boss_rect_bullet = pygame.Rect(boss.x, boss.y, boss_image.get_width(), boss_image.get_height() )
- boss_bullet_rect = pygame.Rect(self.x, self.y, laser_beam.get_width(), laser_beam.get_height() )
- # check collision
- if boss_rect_bullet.colliderect(boss_bullet_rect):
- bullets.pop(bullets.index(self))
- boss.boss_life -= 1
- class RockClass:
- #inizialize the rocks
- def __init__(self):
- self.x = random.uniform(0, display_width)
- self.y = 0
- self.angle_x = random.uniform(-5, 5)
- self.w = rock_width
- self.speed_rock = speed_rock
- #draw the rocks
- def continueDrawRock(self):
- self.y += self.speed_rock
- if self.x <= 0 or self.x + self.w >= display_width:
- self.angle_x = self.angle_x * -1
- self.x += self.angle_x
- screen.blit(rock_image, (self.x, self.y))
- #check if rock collides with player and in that case remove 1 life
- def collisionRock(self, player1, player):
- player_rect = pygame.Rect(player1.x, player1.y, player.get_width(), player.get_height())
- rock_rect = pygame.Rect(self.x, self.y, rock_width, rock_height)
- if player_rect.colliderect(rock_rect):
- rocks.pop(rocks.index(rock2))
- global life
- life -= 1
- class Boss:
- #inizialize the boss
- def __init__(self, boss_life, speed_boss):
- self.x = random.uniform(10, display_width-100)
- self.y = 0
- self.angle = random.uniform(-5, 5)
- self.speed_boss = speed_boss
- self.boss_life = boss_life
- self.image = boss_image
- self.original_boss_image = boss_image
- self.w = boss_width
- self.h = boss_height
- #draw the boss
- def continueDrawBoss(self):
- self.image = pygame.transform.scale(self.original_boss_image, (self.w, self.h))
- self.y += self.speed_boss
- if self.x <= 0 or self.x + self.w >= display_width-10:
- self.angle = self.angle * -1
- self.w += 1
- self.h += 1
- if self.y <= 0 or self.y + self.h >= display_height-10:
- self.speed_boss = -self.speed_boss
- self.w += 1
- self.h += 1
- self.x += self.angle
- screen.blit(self.image, (self.x, self.y))
- #check if boss collides with player and in that case remove 50 life
- def collisionBoss(self, player1, player):
- player_rect = pygame.Rect(player1.x, player1.y, player.get_width(), player.get_height())
- boss_rect = pygame.Rect(self.x, self.y, self.w, self.h)
- if player_rect.colliderect(boss_rect):
- global life
- life -= 50
- def bossDefeat(self):
- global score, fighting_boss, boss_not_present
- if boss_not_present and self.boss_life <= 0:
- score += 50
- boss_not_present = False
- fighting_boss = False
- return True
- class Laser:
- def __init__(self, player_x, player_y):
- self.x = player_x
- self.y = player_y
- self.original_image = pygame.Surface((5, 150))
- self.original_image.set_colorkey( (0,0,0) )
- self.original_image.fill( (255,0,0) )
- self.copy_image = self.original_image.copy()
- self.copy_image.set_colorkey( (0,0,0) )
- self.rect = self.copy_image.get_rect()
- self.rect.center = self.x + player_image.get_width()//2, self.y - 75
- self.new_image = pygame.Surface((5, 150))
- def continueDrawLaser(self):
- if laser_bool:
- screen.blit(self.new_image, self.rect)
- def rotate(self):
- mouse_x, mouse_y = pygame.mouse.get_pos()
- rel_x, rel_y = mouse_x - player1.x + player_image.get_width()//2, mouse_y - player1.y - 75
- angle = (180 / math.pi) * -math.atan2(rel_y, rel_x) - 85
- self.new_image = pygame.transform.rotate(self.original_image, angle)
- self.rect = self.new_image.get_rect()
- self.rect.center = player1.x + player_image.get_width()//2, player1.y - 75
- #inizialize the all game var
- initialize()
- #temp var
- laser_bool = False
- boss_not_present = False
- key = key = [False, False, False, False]
- mouse = False
- temp = False
- running = True
- #main loop
- while running:
- #event loop
- for event in pygame.event.get():
- #move player if keys are pressed
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_a:
- key[0] = True
- if event.key == pygame.K_d:
- key[1] = True
- if event.key == pygame.K_w:
- key[2] = True
- if event.key == pygame.K_s:
- key[3] = True
- if event.key == pygame.K_SPACE:
- laser_bool = True
- #if key pressed is esc then go to menu
- if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
- global go_menu
- go_menu = True
- initialize()
- #if key pressed is c then pause
- if event.key == pygame.K_c:
- global pause
- pause = True
- #if mouse is pressed then shoot
- if event.type == pygame.MOUSEBUTTONDOWN:
- mouse = True
- temp = True
- # if mouse is not pressed then stop shooting
- if event.type == pygame.MOUSEBUTTONUP:
- mouse = False
- #stop player if keys are not pressed
- if event.type == pygame.KEYUP:
- if event.key == pygame.K_a:
- key[0] = False
- if event.key == pygame.K_d:
- key[1] = False
- if event.key == pygame.K_w:
- key[2] = False
- if event.key == pygame.K_s:
- key[3] = False
- if event.key == pygame.K_SPACE:
- laser_bool = False
- #quit
- if event.type == pygame.QUIT:
- running = False
- pygame.quit()
- quit()
- if mouse:
- mouse_x, mouse_y = pygame.mouse.get_pos()
- rel_x, rel_y = mouse_x - player1.x, mouse_y - player1.y
- player1.angle = math.atan2(rel_y, rel_x)
- bullets.append( Bullet(player1.x, player1.y) )
- if laser_bool:
- laser1.rotate()
- checkBoss()
- if fighting_boss:
- tick += 1
- #move and rotate the player
- player1.movePlayer()
- player1.rotate()
- if not fighting_boss:
- # to generate infinite rocks
- if len(rocks) == 0 or rocks[-1].y > 40:
- rocks.append( RockClass() )
- # check the player-rock collision
- for rock2 in rocks:
- rock2.collisionRock(player1, player_image)
- if temp:
- for bullet2 in bullets:
- bullet2.collisionBulletRock(rock2, rock_image)
- if fighting_boss:
- #check if the boss collide with the player
- boss.collisionBoss(player1, player_image)
- # check the player-rock collision
- for rock2 in rocks:
- rock2.collisionRock(player1, player_image)
- for bullet2 in bullets:
- bullet2.collisionBulletRock(rock2, rock_image)
- if tick > 300:
- for bullet2 in bullets:
- bullet2.collisionBulletBoss(boss, boss_image)
- #update screen
- gameIntro()
- gamePause()
- gameOver()
- drawBackground()
- drawScore()
- drawLife()
- drawRocks()
- drawBoss()
- drawBullets()
- player1.drawPlayer()
- drawLaser()
- update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement