Advertisement
eeweww

ee

Jun 18th, 2023
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.60 KB | None | 0 0
  1. import pygame
  2. import time
  3. import random
  4. pygame.font.init()
  5. pygame.mixer.init()
  6. pygame.mixer.music.set_volume(1)
  7.  
  8. #* The variables of the game, change what you want, just dont change WIN or BG.
  9. WIDTH, HEIGHT = 700, 600
  10. WIN = pygame.display.set_mode((WIDTH, HEIGHT))
  11. WINDOW_TITLE = "something"
  12. #* If you want to change the background, replace bg.jpg with what you want.
  13. BG = pygame.transform.scale(pygame.image.load("bg.jpg"), (WIDTH, HEIGHT))
  14. PLAYER_HEIGHT = 60
  15. PLAYER_WIDTH = 40
  16. PLAYER_VEL = 10
  17. PLAYER_COLOR = "red"
  18. ENEMY_WIDTH = 10
  19. ENEMY_HEIGHT = 20
  20. enemy_vel = 8
  21. player_lives = 5
  22. ENEMIES_PER_SPAWN = 3
  23. FONT = pygame.font.SysFont("segoeui", 35)
  24.  
  25. BULLET_IMG = pygame.image.load("bullet.png").convert()
  26.  
  27. def draw(player, elapsed_time, stars, lives):
  28.     WIN.blit(BG, (0, 0))
  29.     time_text = FONT.render(f"{round(elapsed_time)}", 1, "white")
  30.     WIN.blit(time_text, (15, 5))
  31.     lives_text = FONT.render(f"Lives: {lives}", 0, "white")
  32.     for star in stars:
  33.         pygame.draw.rect(WIN, "red", star)
  34.     if lives >= 10:
  35.         WIN.blit(lives_text, (WIDTH - 130, 5))
  36.     elif lives <= 10:
  37.         WIN.blit(lives_text, (WIDTH - 120, 5))
  38.     elif lives >= 100:
  39.         WIN.blit(lives_text, (WIDTH - 110, 5))
  40.     elif lives >= 1000:
  41.         lives = 100
  42.     pygame.draw.rect(WIN, PLAYER_COLOR, player)
  43.     pygame.display.update()
  44.  
  45. pygame.display.set_caption(WINDOW_TITLE)
  46. def main():
  47.     pygame.mixer.music.load("bgSong.wav")
  48.     pygame.mixer.Channel(0).play(pygame.mixer.Sound("bgSong.wav"), loops=-1)
  49.     #* Main loop
  50.     run = True
  51.     player = pygame.Rect(200, HEIGHT - PLAYER_HEIGHT, PLAYER_WIDTH, PLAYER_HEIGHT)
  52.     clock = pygame.time.Clock()
  53.     start_time = time.time()
  54.     elapsed_time = 0
  55.     star_add_increment = 2000
  56.     star_count = 0
  57.     stars = []
  58.     hit = False
  59.     lives = player_lives
  60.     global enemy_vel
  61.  
  62.     while run:
  63.         star_count += clock.tick(60)
  64.         elapsed_time = time.time() - start_time
  65.         if star_count > star_add_increment:
  66.             for _ in range(ENEMIES_PER_SPAWN):
  67.                 star_x = random.randint(0, WIDTH - ENEMY_WIDTH)
  68.                 star = pygame.Rect(star_x, - ENEMY_HEIGHT, ENEMY_WIDTH, ENEMY_HEIGHT)
  69.                 stars.append(star)
  70.             star_add_increment = max(200, star_add_increment - 50)
  71.             star_count = 0
  72.         for event in pygame.event.get():
  73.             if event.type == pygame.QUIT:
  74.                 #* Check for X button pressed.
  75.                 run = False
  76.                 pygame.quit()
  77.                 break
  78.         keys = pygame.key.get_pressed()
  79.         if keys[pygame.K_LEFT] and player.x - PLAYER_VEL >= 0:
  80.             player.x -= PLAYER_VEL
  81.         if keys[pygame.K_RIGHT] and player.x + PLAYER_VEL + PLAYER_WIDTH <= WIDTH:
  82.             player.x += PLAYER_VEL
  83.         if keys[pygame.K_a] and player.x - PLAYER_VEL >= 0:
  84.             player.x -= PLAYER_VEL
  85.         if keys[pygame.K_d] and player.x + PLAYER_VEL + PLAYER_WIDTH <= WIDTH:
  86.             player.x += PLAYER_VEL
  87.         if keys[pygame.K_k]:
  88.             lives = 0
  89.             hit = True
  90.         for star in stars[:]:
  91.             star.y += enemy_vel
  92.             if star.y > HEIGHT:
  93.                 stars.remove(star)
  94.             elif star.y + star.height >= player.y and star.colliderect(player):
  95.                 stars.remove(star)
  96.                 hit = True
  97.                 break
  98.             if hit:
  99.                 if lives > 0:
  100.                     hit = False
  101.                     lives -= 1
  102.                     enemy_vel += 1
  103.                     pygame.mixer.music.load("hitHurt.wav")
  104.                     pygame.mixer.Channel(1).play(pygame.mixer.Sound("hitHurt.wav"))
  105.                 if lives == 0:
  106.                     pygame.mixer.Channel(2).stop()
  107.                     pygame.mixer.Channel(1).stop()
  108.                     pygame.mixer.Channel(0).pause()
  109.                     pygame.mixer.music.load("soundLose.wav")
  110.                     pygame.mixer.Channel(3).play(pygame.mixer.Sound("soundLose.wav"))
  111.                     stopped_time = elapsed_time
  112.                     lost_text = FONT.render(f"You lost and survived {round(stopped_time)} seconds!", 1, "white")
  113.                     WIN.blit(lost_text, (WIDTH/2 - lost_text.get_width()/2, HEIGHT/2 - lost_text.get_height()/2))
  114.                     pygame.display.update()
  115.                     time.sleep(2.25)
  116.                     pygame.mixer.Channel(0).unpause()
  117.                     pygame.time.delay(4000)
  118.                     pygame.quit()
  119.         draw(player, elapsed_time, stars, lives)
  120.     pygame.quit()
  121. #* Checks if program is not a library
  122. if __name__ == '__main__':
  123.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement