Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- PlayerColor = (255, 128, 0)
- WIDTH, HEIGHT = 800, 600
- BackgroundColor = (0,0,0)
- PLAYERSPEED = 10
- CloudColor = (200,200,200)
- EnemyColor = (255, 0, 0)
- PlayerBulletColor = (0, 0, 255)
- EnemyBulletColor = (0, 250, 255)
- BULLETSPEED = 10
- ENEMYSPEED = 7
- pygame.init()
- window = pygame.display.set_mode((WIDTH, HEIGHT))
- class Player(pygame.sprite.Sprite):
- def __init__(self):
- super().__init__()
- self.image = pygame.Surface((50,35))
- self.image.fill(PlayerColor)
- self.rect = self.image.get_rect()
- self.rect.center = (200, 200)
- def update(self):
- keys = pygame.key.get_pressed()
- dx, dy = 0, 0 # change in x, change in y
- # dx -> speed of player going right
- # dy -> speed of player going down
- if keys[pygame.K_w] and self.rect.top > 0:
- dy = -PLAYERSPEED
- if keys[pygame.K_a] and self.rect.left > 0:
- dx = -PLAYERSPEED
- if keys[pygame.K_s] and self.rect.bottom < HEIGHT:
- dy = PLAYERSPEED
- if keys[pygame.K_d] and self.rect.right < WIDTH:
- dx = PLAYERSPEED
- self.rect.move_ip(dx, dy)
- # pos = pygame.mouse.get_pos()
- # self.rect.center = pos
- class Cloud(pygame.sprite.Sprite):
- def __init__(self):
- super().__init__()
- self.size = random.randrange(50,100)
- self.image = pygame.Surface((self.size*2,self.size))
- self.image.fill(CloudColor)
- self.rect = self.image.get_rect()
- self.rect.topleft = (WIDTH, random.randrange(0, HEIGHT-50))
- self.speed = random.randrange(2,7)
- def update(self):
- self.rect.move_ip(-self.speed, 0)
- if self.rect.right < 0: # if cloud goes outside the screen
- self.kill()
- class Enemy(pygame.sprite.Sprite):
- def __init__(self):
- super().__init__()
- self.image = pygame.Surface((50,35))
- self.image.fill(EnemyColor)
- self.rect = self.image.get_rect()
- self.rect.topleft = (WIDTH, random.randrange(0, HEIGHT))
- self.speed = random.randrange(ENEMYSPEED-5, ENEMYSPEED)
- def update(self):
- self.rect.move_ip(-self.speed, 0)
- if self.rect.right < 0:
- self.kill()
- class Bullet(pygame.sprite.Sprite):
- def __init__(self, player_position):
- super().__init__()
- self.image = pygame.Surface((15,10))
- self.image.fill((PlayerBulletColor))
- self.speed = BULLETSPEED
- self.rect = self.image.get_rect()
- self.rect.center = player_position
- def update(self):
- self.rect.move_ip(self.speed, 0)
- if self.rect.left > WIDTH:
- self.kill()
- class Bullet(pygame.sprite.Sprite):
- def __init__(self, player_position):
- super().__init__()
- self.image = pygame.Surface((15,10))
- self.image.fill((PlayerBulletColor))
- self.speed = BULLETSPEED
- self.rect = self.image.get_rect()
- self.rect.center = player_position
- def update(self):
- self.rect.move_ip(self.speed, 0)
- if self.rect.left > WIDTH:
- self.kill()
- class Star(pygame.sprite.Sprite):
- def __init__(self):
- super().__init__()
- self.image = pygame.Surface((50,35))
- self.image.fill((255,255,0))
- self.rect = self.image.get_rect()
- self.rect.center = (random.randrange(50, WIDTH-50), -50)
- self.speed = random.randrange(ENEMYSPEED-5, ENEMYSPEED)
- def update(self):
- self.rect.move_ip(0, self.speed)
- if self.rect.top > HEIGHT:
- self.kill()
- pygame.display.set_caption("Sky Shooter")
- run = True
- clock = pygame.time.Clock()
- elements = pygame.sprite.Group()
- clouds = pygame.sprite.Group()
- enemies = pygame.sprite.Group()
- stars = pygame.sprite.Group()
- playerbullets = pygame.sprite.Group()
- enemybullets = pygame.sprite.Group()
- event_cloud = pygame.USEREVENT + 1
- event_enemy = pygame.USEREVENT + 2
- event_enemy_shoots = pygame.USEREVENT + 3
- event_star = pygame.USEREVENT + 3
- pygame.time.set_timer(event_cloud, 800)
- pygame.time.set_timer(event_enemy, 300)
- pygame.time.set_timer(event_enemy_shoots, 200)
- pygame.time.set_timer(event_star, 2000)
- player = Player()
- elements.add(player)
- pygame.mouse.set_visible(False)
- while run:
- clock.tick(60)
- window.fill(BackgroundColor)
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- run = False
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_SPACE:
- b = Bullet(player.rect.center)
- playerbullets.add(b)
- if event.type == event_enemy_shoots and enemies:
- e = random.choice(list(enemies))
- b = Bullet(e.rect.center, 2)
- enemybullets.add(b)
- if event.type == event_cloud:
- c = Cloud()
- clouds.add(c)
- if event.type == event_enemy:
- e = Enemy()
- enemies.add(e)
- if event.type == event_star:
- s = Star()
- stars.add(s)
- hit = pygame.sprite.spritecollideany(player, enemies)
- if hit:
- run = False
- pygame.sprite.groupcollide(playerbullets, enemies, True, True)
- playerbullets.update()
- playerbullets.draw(window)
- enemybullets.update()
- enemybullets.draw(window)
- elements.update()
- elements.draw(window)
- stars.update()
- stars.draw(window)
- enemies.update()
- enemies.draw(window)
- clouds.update()
- clouds.draw(window)
- pygame.display.update()
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement