Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- import time
- import math
- from pygame.math import Vector2
- pygame.init()
- display_width = 800
- display_height = 650
- screen = pygame.display.set_mode((display_width, display_height))
- fps = 60
- shooting_enemy_image = pygame.image.load('cave-painting.png').convert_alpha()
- first_player_spaceship_image = pygame.image.load('spaceship.png').convert_alpha()
- laser_bullet = pygame.image.load('laser1.png').convert_alpha()
- def initialize():
- global shooting_enemies_list, enemy_shooting_bullets_list, player_one_istance
- shooting_enemies_list, enemy_shooting_bullets_list = [], []
- shooting_enemies_list.append(ShootingEnemy())
- player_one_istance = Player(display_width // 2, display_height // 2, first_player_spaceship_image)
- def update():
- pygame.display.update()
- pygame.time.Clock().tick(fps)
- def drawAll():
- drawBackground()
- drawShootingEnemy()
- drawShootingEnemyBullets()
- player_one_istance.drawPlayer()
- def drawBackground():
- screen.fill((0, 0, 0))
- def drawShootingEnemy():
- # draw shooting enemy
- for shooting_enemy in shooting_enemies_list:
- shooting_enemy.continueMoveShootingEnemy()
- shooting_enemy.rotate()
- shooting_enemy.drawShootingEnemies()
- def drawShootingEnemyBullets():
- for bullets_shooting_enemy in enemy_shooting_bullets_list:
- bullets_shooting_enemy.MoveBulletEnemyShooting()
- bullets_shooting_enemy.rotate()
- bullets_shooting_enemy.drawBulletEnemyShooting()
- class Player:
- # inizialize the player
- def __init__(self, x, y, player):
- self.x = x
- self.y = y
- self.image = first_player_spaceship_image
- self.life = 6
- self.original_player_image = first_player_spaceship_image
- self.angle = 0
- self.rect = self.image.get_rect()
- # 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 + shooting_enemy_image.get_width() >= display_width:
- self.x = display_width - first_player_spaceship_image.get_width()
- if self.y <= 0:
- self.y = 0
- if self.y + first_player_spaceship_image.get_height() >= display_height:
- self.y = display_height - first_player_spaceship_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))
- orig_center = self.original_player_image.get_rect(topleft=(self.x, self.y)).center
- self.rect = self.image.get_rect(center=orig_center)
- # draw the player
- def drawPlayer(self):
- screen.blit(self.image, self.rect.topleft)
- class ShootingEnemy:
- # inizialize the enemy
- def __init__(self):
- self.image = shooting_enemy_image
- self.new_shooting_enemy_image = shooting_enemy_image
- self.x = display_width // 2
- self.y = 0
- self.begin_down = True
- self.choices = 0
- self.timer = 51
- self.left_again = False
- self.right_again = False
- self.up_again = False
- self.down_again = False
- self.rect = shooting_enemy_image.get_rect()
- self.angle = 0
- # draw the shooting enemy
- def continueMoveShootingEnemy(self):
- if self.y < 30:
- self.y += 1
- if self.y == 30:
- self.begin_down = False
- self.y += 1
- else:
- if not self.begin_down and self.timer > 20:
- self.choices = random.choice([1, 2, 3, 4])
- self.timer = 0
- self.timer += 1
- if self.left_again:
- self.x += 1
- self.left_again = False
- if self.right_again:
- self.x -= 1
- self.right_again = False
- if self.up_again:
- self.y += 0
- self.up_again = False
- if self.down_again:
- self.y -= 1
- self.down_again = False
- else:
- if self.choices == 1:
- if self.x >= display_width:
- self.timer = 21
- self.right_again = True
- else:
- if self.y < 140 and self.y > 10:
- self.y += 1
- self.x += 4
- else:
- self.x += 4
- if self.choices == 2:
- if self.x <= 0:
- self.timer = 21
- self.left_again = True
- else:
- if self.y < 140 and self.y > 10:
- self.y += 1
- self.x -= 4
- else:
- self.x -= 4
- if self.choices == 3:
- if self.y >= 150:
- self.timer = 21
- self.down_again = True
- else:
- self.y += 2
- if self.choices == 4:
- if self.y <= 0:
- self.timer = 21
- self.up_again = True
- else:
- self.y -= 2
- # rotate the shooting enemy where the player is
- def rotate(self):
- temp_x, temp_y = player_one_istance.x - self.x, player_one_istance.y - self.y
- self.angle = (180 / math.pi) * -math.atan2(temp_y, temp_x) - 90
- self.image = pygame.transform.rotate(self.new_shooting_enemy_image, int(self.angle))
- orig_center = self.new_shooting_enemy_image.get_rect(topleft=(self.x, self.y)).center
- self.rect = self.image.get_rect(center=orig_center)
- def drawShootingEnemies(self):
- screen.blit(self.image, (self.x, self.y))
- class BulletEnemyShooting:
- # initialize the bullet for the enemy shooting
- def __init__(self, shooting_enemy):
- self.x = shooting_enemy.x
- self.y = shooting_enemy.y
- self.image = laser_bullet
- self.original_image = laser_bullet
- self.angle = shooting_enemy.angle
- self.vel_x = math.cos(self.angle) * 45
- self.vel_y = math.sin(self.angle) * 45
- self.rect = self.image.get_rect()
- # draw the bullet
- def MoveBulletEnemyShooting(self):
- self.x += self.vel_x
- self.y += self.vel_y
- if self.x < -laser_bullet.get_width() or self.x >= display_width + laser_bullet.get_width() \
- or self.y < -laser_bullet.get_height() or self.y >= display_height + laser_bullet.get_height():
- enemy_shooting_bullets_list.pop(enemy_shooting_bullets_list.index(self))
- def drawBulletEnemyShooting(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()
- # inizialize the all game var
- initialize()
- # temp var
- key = [False, False, False, False]
- mouse = False
- temp = False
- running = True
- enemy_shooting_bool = False
- # 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_LSHIFT:
- enemy_shooting_bool = 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 enemy_shooting_bool:
- enemy_shooting_bullets_list.append(BulletEnemyShooting(shooting_enemies_list[0]))
- # move and rotate the player
- player_one_istance.movePlayer()
- player_one_istance.rotate()
- # draw all things
- drawAll()
- # update all things
- update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement