Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- import math
- from pygame import mixer
- # intialize pygame
- pygame.init()
- #Background music
- mixer.music.load('background.wav')
- mixer.music.play(-1)
- # create the screen
- height = 600
- width = 800
- screen = pygame.display.set_mode((width, height)) # there must be 2 parenthesis
- # Title and Icon
- pygame.display.set_caption("Space Invader")
- icon = pygame.image.load('ufo.png')
- pygame.display.set_icon(icon)
- #score
- score_value = 0
- font = pygame.font.Font('freesansbold.ttf',32)
- textx = 10
- texty = 10
- # Player
- player_image = pygame.image.load('arcade.png')
- player_x = width / 2 - 36
- player_y = height * 4 / 5
- player_x_change = 0
- # enemy
- enemy_image = []
- enemy_x = []
- enemy_y = []
- enemy_x_change = []
- enemy_y_change = []
- num_of_enemies = 6
- for i in range(num_of_enemies):
- enemy_image.append(pygame.image.load('alien.png'))
- enemy_x.append(random.randint(0, width - 65))
- enemy_y.append(random.randint(int(height / 12), int(height / 4)))
- enemy_x_change.append(0.3)
- enemy_y_change.append(40)
- # Bullet
- # ready = you can't see the bullet on screen
- # Fire = Bullet is moving
- bullet_image = pygame.image.load('bullet.png')
- bullet_x = 0
- bullet_y = height * 4 / 5
- bullet_x_change = 0
- bullet_y_change = 0.5
- bullet_state = 'ready'
- #GameOver
- over_font = pygame.font.Font('freesansbold.ttf',64)
- def gameovertext():
- game_over = over_font.render('* GAME OVER *', True, (255, 255, 255))
- screen.blit(game_over, (200, 250))
- def show_score (x,y):
- score = font.render('score:' +str(score_value),True,(255,255,255))
- screen.blit(score, (x, y))
- def player(x, y):
- screen.blit(player_image, (x, y))
- def enemy(x, y, i):
- screen.blit(enemy_image[i], (x, y))
- def fire_bullet(x, y):
- global bullet_state
- bullet_state = 'fire'
- screen.blit(bullet_image, (x + 16, y + 10))
- def iscollision(enemy_x, enemy_y, bullet_x, bullet_y):
- distance = math.sqrt(pow(enemy_x - bullet_x, 2) + pow(enemy_y - bullet_y, 2))
- if distance <= 27:
- return True
- else:
- return False
- running = True
- while running:
- # RGB Red Green Blue
- screen.fill((0, 0, 0))
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_LEFT:
- player_x_change = -0.3
- if event.key == pygame.K_RIGHT:
- player_x_change = 0.3
- if bullet_state == 'ready':
- if event.key == pygame.K_SPACE:
- bullet_sound = mixer.Sound('laser.wav')
- bullet_sound.play()
- # Get the current x co-ordinate of spaceship
- bullet_x = player_x
- fire_bullet(bullet_x, bullet_y)
- if event.type == pygame.KEYUP:
- if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
- player_x_change = 0
- # Checking for boundaries so that spaceship doesn't go out of boundary.
- player_x += player_x_change
- if player_x <= 0:
- player_x = 0
- elif player_x > width - 64:
- player_x = width - 64
- # Enemy movement
- for i in range(num_of_enemies):
- if enemy_y[i] > 200:
- for j in range (num_of_enemies):
- enemy_y[j] = 2000
- gameovertext()
- break
- enemy_x[i] += enemy_x_change[i]
- if enemy_x[i] <= 0:
- enemy_x_change[i] = 0.3
- enemy_y[i] += enemy_y_change[i]
- elif enemy_x[i] >= width - 64:
- enemy_x_change[i] = -0.3
- enemy_y[i] += enemy_y_change[i]
- # Checking for collission
- collision = iscollision(enemy_x[i], enemy_y[i], bullet_x, bullet_y)
- if collision:
- explosion_sound = mixer.Sound('explosion.wav')
- explosion_sound.play()
- bullet_y = height * 4 / 5
- bullet_state = 'ready'
- score_value += 1
- enemy_x[i] = random.randint(0, width - 65)
- enemy_y[i] = random.randint(int(height / 12), int(height / 4))
- enemy(enemy_x[i], enemy_y[i], i)
- player(player_x, player_y)
- show_score(textx,texty)
- # Bullet movement
- if bullet_y <= 0:
- bullet_y = height * 4 / 5
- bullet_state = 'ready'
- if bullet_state == 'fire':
- fire_bullet(bullet_x, bullet_y)
- bullet_y -= bullet_y_change
- pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement