Add Lietotāja definēti notikumi
							parent
							
								
									d53c172e3a
								
							
						
					
					
						commit
						74e1657cc9
					
				| 
						 | 
				
			
			@ -0,0 +1,25 @@
 | 
			
		|||
from settings import SCREEN_HEIGHT, SCREEN_WIDTH
 | 
			
		||||
import random
 | 
			
		||||
import pygame
 | 
			
		||||
 | 
			
		||||
# Define the enemy object by extending pygame.sprite.Sprite
 | 
			
		||||
# The surface you draw on the screen is now an attribute of 'enemy'
 | 
			
		||||
class Enemy(pygame.sprite.Sprite):
 | 
			
		||||
    def __init__(self):
 | 
			
		||||
        super(Enemy, self).__init__()
 | 
			
		||||
        self.surf = pygame.Surface((20, 10))
 | 
			
		||||
        self.surf.fill((255, 0, 0))
 | 
			
		||||
        self.rect = self.surf.get_rect(
 | 
			
		||||
            center=(
 | 
			
		||||
                random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
 | 
			
		||||
                random.randint(0, SCREEN_HEIGHT),
 | 
			
		||||
            )
 | 
			
		||||
        )
 | 
			
		||||
        self.speed = random.randint(5, 20)
 | 
			
		||||
 | 
			
		||||
    # Move the sprite based on speed
 | 
			
		||||
    # Remove the sprite when it passes the left edge of the screen
 | 
			
		||||
    def update(self):
 | 
			
		||||
        self.rect.move_ip(-self.speed, 0)
 | 
			
		||||
        if self.rect.right < 0:
 | 
			
		||||
            self.kill()
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,85 @@
 | 
			
		|||
# Code is taken from https://realpython.com/pygame-a-primer/
 | 
			
		||||
 | 
			
		||||
# Import and initialize the pygame library
 | 
			
		||||
import pygame
 | 
			
		||||
from player import Player
 | 
			
		||||
from enemy import Enemy
 | 
			
		||||
from settings import SCREEN_HEIGHT, SCREEN_WIDTH
 | 
			
		||||
 | 
			
		||||
from pygame.locals import (
 | 
			
		||||
    K_UP,
 | 
			
		||||
    K_DOWN,
 | 
			
		||||
    K_LEFT,
 | 
			
		||||
    K_RIGHT,
 | 
			
		||||
    K_ESCAPE,
 | 
			
		||||
    KEYDOWN,
 | 
			
		||||
    QUIT,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
pygame.init()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# Create the screen object
 | 
			
		||||
# The size is determined by the constant SCREEN_WIDTH and SCREEN_HEIGHT
 | 
			
		||||
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
 | 
			
		||||
 | 
			
		||||
# Create a custom event for adding a new enemy
 | 
			
		||||
ADDENEMY = pygame.USEREVENT + 1
 | 
			
		||||
pygame.time.set_timer(ADDENEMY, 250)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# Variable to keep the main loop running
 | 
			
		||||
running = True
 | 
			
		||||
 | 
			
		||||
player = Player()
 | 
			
		||||
 | 
			
		||||
# Create groups to hold enemy sprites and all sprites
 | 
			
		||||
# - enemies is used for collision detection and position updates
 | 
			
		||||
# - all_sprites is used for rendering
 | 
			
		||||
enemies = pygame.sprite.Group()
 | 
			
		||||
all_sprites = pygame.sprite.Group()
 | 
			
		||||
all_sprites.add(player)
 | 
			
		||||
 | 
			
		||||
# Main loop
 | 
			
		||||
while running:
 | 
			
		||||
    # Look at every event in the queue
 | 
			
		||||
    for event in pygame.event.get():
 | 
			
		||||
        # Did the user hit a key?
 | 
			
		||||
        if event.type == KEYDOWN:
 | 
			
		||||
            # Was it the Escape key? If so, stop the loop.
 | 
			
		||||
            if event.key == K_ESCAPE:
 | 
			
		||||
                running = False
 | 
			
		||||
 | 
			
		||||
        # Did the user click the window close button? If so, stop the loop.
 | 
			
		||||
        elif event.type == QUIT:
 | 
			
		||||
            running = False
 | 
			
		||||
            
 | 
			
		||||
        # Add a new enemy?
 | 
			
		||||
        elif event.type == ADDENEMY:
 | 
			
		||||
            # Create the new enemy and add it to sprite groups
 | 
			
		||||
            new_enemy = Enemy()
 | 
			
		||||
            enemies.add(new_enemy)
 | 
			
		||||
            all_sprites.add(new_enemy)
 | 
			
		||||
 | 
			
		||||
    # Fill the background with white
 | 
			
		||||
    screen.fill((255, 255, 255))
 | 
			
		||||
    
 | 
			
		||||
    # Get all the keys currently pressed
 | 
			
		||||
    pressed_keys = pygame.key.get_pressed()
 | 
			
		||||
 | 
			
		||||
    # Update the player sprite based on user keypresses
 | 
			
		||||
    player.update(pressed_keys)
 | 
			
		||||
    
 | 
			
		||||
    # Update enemy position
 | 
			
		||||
    enemies.update()
 | 
			
		||||
 | 
			
		||||
    # Draw all sprites
 | 
			
		||||
    for entity in all_sprites:
 | 
			
		||||
        screen.blit(entity.surf, entity.rect)
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
    # Flip the display
 | 
			
		||||
    pygame.display.flip()
 | 
			
		||||
 | 
			
		||||
# Done! Time to quit.
 | 
			
		||||
pygame.quit()
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,41 @@
 | 
			
		|||
import pygame
 | 
			
		||||
from settings import SCREEN_HEIGHT, SCREEN_WIDTH
 | 
			
		||||
from pygame.locals import (
 | 
			
		||||
    K_UP,
 | 
			
		||||
    K_DOWN,
 | 
			
		||||
    K_LEFT,
 | 
			
		||||
    K_RIGHT,
 | 
			
		||||
    K_ESCAPE,
 | 
			
		||||
    KEYDOWN,
 | 
			
		||||
    QUIT,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# Define a Player object by extending pygame.sprite.Sprite
 | 
			
		||||
# The surface drawn on the screen is now an attribute of 'player'
 | 
			
		||||
class Player(pygame.sprite.Sprite):
 | 
			
		||||
    def __init__(self):
 | 
			
		||||
        super().__init__()
 | 
			
		||||
        self.surf = pygame.Surface((75, 25))
 | 
			
		||||
        self.surf.fill((0, 0, 0))
 | 
			
		||||
        self.rect = self.surf.get_rect()
 | 
			
		||||
        
 | 
			
		||||
    def update(self, pressed_keys):
 | 
			
		||||
        if pressed_keys[K_UP]:
 | 
			
		||||
            self.rect.move_ip(0, -5)
 | 
			
		||||
        if pressed_keys[K_DOWN]:
 | 
			
		||||
            self.rect.move_ip(0, 5)
 | 
			
		||||
        if pressed_keys[K_LEFT]:
 | 
			
		||||
            self.rect.move_ip(-5, 0)
 | 
			
		||||
        if pressed_keys[K_RIGHT]:
 | 
			
		||||
            self.rect.move_ip(5, 0)
 | 
			
		||||
 | 
			
		||||
        # Keep player on the screen
 | 
			
		||||
        if self.rect.left < 0:
 | 
			
		||||
            self.rect.left = 0
 | 
			
		||||
        if self.rect.right > SCREEN_WIDTH:
 | 
			
		||||
            self.rect.right = SCREEN_WIDTH
 | 
			
		||||
        if self.rect.top <= 0:
 | 
			
		||||
            self.rect.top = 0
 | 
			
		||||
        if self.rect.bottom >= SCREEN_HEIGHT:
 | 
			
		||||
            self.rect.bottom = SCREEN_HEIGHT
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,3 @@
 | 
			
		|||
# Define constants for the screen width and height
 | 
			
		||||
SCREEN_WIDTH = 800
 | 
			
		||||
SCREEN_HEIGHT = 600
 | 
			
		||||
| 
						 | 
				
			
			@ -8,7 +8,7 @@ class Enemy(pygame.sprite.Sprite):
 | 
			
		|||
    def __init__(self):
 | 
			
		||||
        super(Enemy, self).__init__()
 | 
			
		||||
        self.surf = pygame.Surface((20, 10))
 | 
			
		||||
        self.surf.fill((255, 255, 255))
 | 
			
		||||
        self.surf.fill((255, 0, 0))
 | 
			
		||||
        self.rect = self.surf.get_rect(
 | 
			
		||||
            center=(
 | 
			
		||||
                random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue