Advertisement
MrPinzon

pygame_unstoppable_highway_part_1.py

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