MrPinzon

pygame_unstoppable_highway_part_2.py

May 4th, 2022
1,047
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.59 KB | None | 0 0
  1. #adapted from this example
  2. #https://coderslegacy.com/python/python-pygame-tutorial/
  3.  
  4. import pygame
  5. from pygame.locals import (K_UP, K_DOWN, K_LEFT, K_RIGHT, K_ESCAPE, QUIT, KEYDOWN)
  6. import random, time, sys
  7.  
  8. #initialize the game!
  9. pygame.init()
  10.  
  11. #FPS stands for frames per second
  12. FPS = 60
  13. FramePerSec = pygame.time.Clock()
  14.  
  15. #define (R, G, B) colors
  16. BLUE = (0, 0, 255)
  17. RED = (255, 0, 0)
  18. GREEN = (0, 255, 0)
  19. BLACK = (0, 0, 0)
  20. WHITE = (255, 255, 255)
  21. GRAY = (190, 190, 190)
  22.  
  23. # Screen information
  24. SCREEN_WIDTH = 1600
  25. SCREEN_HEIGHT = 900
  26.  
  27. #
  28. SPEED = 5
  29.  
  30. #set "screen" as the game's main display
  31. SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  32. SCREEN.fill(GRAY)
  33.  
  34. #window title
  35. pygame.display.set_caption("Unstoppable highway")
  36.  
  37. #Enemy is going to be a type of sprite of PyGame
  38. class Enemy(pygame.sprite.Sprite):
  39.     #Defines what to do when you create a new Enemy
  40.     def __init__(self):
  41.         super().__init__()
  42.         #load the image and convert to a format that facilitates efficiency of execution
  43.         self.image = pygame.image.load("Enemy.png").convert_alpha()
  44.         #resize the image
  45.         self.image = pygame.transform.scale(self.image, (300, 149))
  46.         #flip the image horizontally
  47.         self.image = pygame.transform.flip(self.image, True, False)
  48.         #get a bounding rectangle for that image
  49.         self.rect = self.image.get_rect()
  50.         #set the center of the image as a coordinate
  51.         self.rect.center = (SCREEN_WIDTH, random.randint(40, SCREEN_HEIGHT - 40))
  52.  
  53.     #Every Enemy object can move
  54.     def move(self):
  55.         #Move 10 pixels to the left
  56.         self.rect.move_ip(-SPEED, 0)
  57.         #If the sprite moves beyond the screen limits then move it back to the start
  58.         if (self.rect.left < -300):
  59.             self.rect.right = SCREEN_WIDTH+300
  60.             self.rect.center = (SCREEN_WIDTH+300, random.randint(80, SCREEN_HEIGHT - 80))
  61.  
  62.     #draw is necessary to show it on the screen
  63.     def draw(self, surface):
  64.         #blit is used to draw a surface on top of another
  65.         surface.blit(self.image, self.rect) #draw the image of the object onto the screen
  66.  
  67. #Even though our game is single player, it has a sprite
  68. class Player(pygame.sprite.Sprite):
  69.     def __init__(self):
  70.         super().__init__()
  71.         self.image = pygame.image.load("Player.png")
  72.         self.image = pygame.transform.scale(self.image, (300, 149))
  73.         self.rect = self.image.get_rect()
  74.         self.rect.center = (160, 520)
  75.  
  76.     #This method is used to read if one of the keys is pressed and move the sprite
  77.     def move(self):
  78.         #This will return a list
  79.         pressed_keys = pygame.key.get_pressed()
  80.         #move the car up but not above pixel 0
  81.         if self.rect.top > 0:
  82.             if pressed_keys[K_UP]:
  83.                 self.rect.move_ip(0, -5)
  84.         #move the car down but not below the bottom of the screen
  85.         if self.rect.bottom < SCREEN_HEIGHT:
  86.             if pressed_keys[K_DOWN]:
  87.                 self.rect.move_ip(0,5)
  88.  
  89.     def draw(self, surface):
  90.         surface.blit(self.image, self.rect)
  91.  
  92. #Create both Player and Enemy
  93. P1 = Player()
  94. E1 = Enemy()
  95.  
  96. #Creating Sprites Group for enemies
  97. enemies = pygame.sprite.Group()
  98. enemies.add(E1)
  99.  
  100. #Creating all Sprites Group
  101. all_sprites = pygame.sprite.Group()
  102. all_sprites.add(P1)
  103. all_sprites.add(E1)
  104.  
  105. #Adding a new User event
  106. INC_SPEED = pygame.USEREVENT + 1 #The +1 is to give it a new ID in the events list
  107. #set_timer calls every 1000 milliseconds the INC_SPEED event
  108. pygame.time.set_timer(INC_SPEED, 1000)
  109.  
  110. #An infinite loop to run the game endlessly
  111. while True:
  112.     #pygame is continually reading events
  113.     for event in pygame.event.get():
  114.         #if the user clicks the x on the top bar, quit the game safely
  115.         if event.type == QUIT:
  116.             pygame.quit()
  117.             sys.exit()
  118.         if event.type == INC_SPEED:
  119.             SPEED += 1
  120.     #otherwise, update the player's and enemy's position
  121.     P1.update()
  122.     E1.move()
  123.  
  124.     # The screen is filled again to reset the previous positions visually
  125.     SCREEN.fill(GRAY)
  126.  
  127.     # Moves and Re-draws all Sprites
  128.     for entity in all_sprites:
  129.         SCREEN.blit(entity.image, entity.rect)
  130.         entity.move()
  131.  
  132.     # To be run if collision occurs between Player and Enemy
  133.     if pygame.sprite.spritecollideany(P1, enemies):
  134.         SCREEN.fill(RED)
  135.         pygame.display.update()
  136.         for entity in all_sprites:
  137.             entity.kill()
  138.         time.sleep(2)
  139.         pygame.quit()
  140.         sys.exit()
  141.  
  142.         #commit changes
  143.     pygame.display.update()
  144.     FramePerSec.tick(FPS)
Advertisement
Add Comment
Please, Sign In to add comment