Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import time
- import random
- pygame.font.init()
- pygame.mixer.init()
- pygame.mixer.music.set_volume(1)
- #* The variables of the game, change what you want, just dont change WIN or BG.
- WIDTH, HEIGHT = 700, 600
- WIN = pygame.display.set_mode((WIDTH, HEIGHT))
- WINDOW_TITLE = "something"
- #* If you want to change the background, replace bg.jpg with what you want.
- BG = pygame.transform.scale(pygame.image.load("bg.jpg"), (WIDTH, HEIGHT))
- PLAYER_HEIGHT = 60
- PLAYER_WIDTH = 40
- PLAYER_VEL = 10
- PLAYER_COLOR = "red"
- ENEMY_WIDTH = 10
- ENEMY_HEIGHT = 20
- enemy_vel = 8
- player_lives = 5
- ENEMIES_PER_SPAWN = 3
- FONT = pygame.font.SysFont("segoeui", 35)
- BULLET_IMG = pygame.image.load("bullet.png").convert()
- def draw(player, elapsed_time, stars, lives):
- WIN.blit(BG, (0, 0))
- time_text = FONT.render(f"{round(elapsed_time)}", 1, "white")
- WIN.blit(time_text, (15, 5))
- lives_text = FONT.render(f"Lives: {lives}", 0, "white")
- for star in stars:
- pygame.draw.rect(WIN, "red", star)
- if lives >= 10:
- WIN.blit(lives_text, (WIDTH - 130, 5))
- elif lives <= 10:
- WIN.blit(lives_text, (WIDTH - 120, 5))
- elif lives >= 100:
- WIN.blit(lives_text, (WIDTH - 110, 5))
- elif lives >= 1000:
- lives = 100
- pygame.draw.rect(WIN, PLAYER_COLOR, player)
- pygame.display.update()
- pygame.display.set_caption(WINDOW_TITLE)
- def main():
- pygame.mixer.music.load("bgSong.wav")
- pygame.mixer.Channel(0).play(pygame.mixer.Sound("bgSong.wav"), loops=-1)
- #* Main loop
- run = True
- player = pygame.Rect(200, HEIGHT - PLAYER_HEIGHT, PLAYER_WIDTH, PLAYER_HEIGHT)
- clock = pygame.time.Clock()
- start_time = time.time()
- elapsed_time = 0
- star_add_increment = 2000
- star_count = 0
- stars = []
- hit = False
- lives = player_lives
- global enemy_vel
- while run:
- star_count += clock.tick(60)
- elapsed_time = time.time() - start_time
- if star_count > star_add_increment:
- for _ in range(ENEMIES_PER_SPAWN):
- star_x = random.randint(0, WIDTH - ENEMY_WIDTH)
- star = pygame.Rect(star_x, - ENEMY_HEIGHT, ENEMY_WIDTH, ENEMY_HEIGHT)
- stars.append(star)
- star_add_increment = max(200, star_add_increment - 50)
- star_count = 0
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- #* Check for X button pressed.
- run = False
- pygame.quit()
- break
- keys = pygame.key.get_pressed()
- if keys[pygame.K_LEFT] and player.x - PLAYER_VEL >= 0:
- player.x -= PLAYER_VEL
- if keys[pygame.K_RIGHT] and player.x + PLAYER_VEL + PLAYER_WIDTH <= WIDTH:
- player.x += PLAYER_VEL
- if keys[pygame.K_a] and player.x - PLAYER_VEL >= 0:
- player.x -= PLAYER_VEL
- if keys[pygame.K_d] and player.x + PLAYER_VEL + PLAYER_WIDTH <= WIDTH:
- player.x += PLAYER_VEL
- if keys[pygame.K_k]:
- lives = 0
- hit = True
- for star in stars[:]:
- star.y += enemy_vel
- if star.y > HEIGHT:
- stars.remove(star)
- elif star.y + star.height >= player.y and star.colliderect(player):
- stars.remove(star)
- hit = True
- break
- if hit:
- if lives > 0:
- hit = False
- lives -= 1
- enemy_vel += 1
- pygame.mixer.music.load("hitHurt.wav")
- pygame.mixer.Channel(1).play(pygame.mixer.Sound("hitHurt.wav"))
- if lives == 0:
- pygame.mixer.Channel(2).stop()
- pygame.mixer.Channel(1).stop()
- pygame.mixer.Channel(0).pause()
- pygame.mixer.music.load("soundLose.wav")
- pygame.mixer.Channel(3).play(pygame.mixer.Sound("soundLose.wav"))
- stopped_time = elapsed_time
- lost_text = FONT.render(f"You lost and survived {round(stopped_time)} seconds!", 1, "white")
- WIN.blit(lost_text, (WIDTH/2 - lost_text.get_width()/2, HEIGHT/2 - lost_text.get_height()/2))
- pygame.display.update()
- time.sleep(2.25)
- pygame.mixer.Channel(0).unpause()
- pygame.time.delay(4000)
- pygame.quit()
- draw(player, elapsed_time, stars, lives)
- pygame.quit()
- #* Checks if program is not a library
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement