Add Teksts ekranā
parent
5b6e50e489
commit
962df5fea5
|
@ -0,0 +1,26 @@
|
|||
from settings import SCREEN_HEIGHT, SCREEN_WIDTH
|
||||
from pygame.locals import (RLEACCEL,K_UP,K_DOWN,K_LEFT,K_RIGHT,K_ESCAPE,KEYDOWN,QUIT)
|
||||
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().__init__()
|
||||
self.surf = pygame.image.load("sprites/bat.png").convert()
|
||||
self.surf.set_colorkey((0, 0, 0), RLEACCEL)
|
||||
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, 10)
|
||||
|
||||
# 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,117 @@
|
|||
# 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,
|
||||
)
|
||||
|
||||
# Setup for sounds. Defaults are good.
|
||||
pygame.mixer.init()
|
||||
pygame.init()
|
||||
|
||||
pygame.font.init()
|
||||
my_font = pygame.font.SysFont('Comic Sans MS', 30)
|
||||
|
||||
# Load and play background music
|
||||
pygame.mixer.music.load("sounds/cave_theme_1.wav")
|
||||
pygame.mixer.music.play(loops=-1)
|
||||
|
||||
# Load all sound files
|
||||
collision_sound = pygame.mixer.Sound("sounds/boom.wav")
|
||||
|
||||
|
||||
# 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)
|
||||
|
||||
bg = pygame.image.load("sprites/background.png").convert()
|
||||
|
||||
# Setup the clock for a decent framerate
|
||||
clock = pygame.time.Clock()
|
||||
|
||||
# Main loop
|
||||
while running:
|
||||
screen.blit(bg, (0,0))
|
||||
# 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)
|
||||
|
||||
|
||||
# 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)
|
||||
|
||||
|
||||
|
||||
# Check if any enemies have collided with the player
|
||||
if pygame.sprite.spritecollideany(player, enemies):
|
||||
# If so, then remove the player and stop the loop
|
||||
player.kill()
|
||||
collision_sound.play()
|
||||
pygame.time.delay(1000)
|
||||
running = False
|
||||
|
||||
text = my_font.render("Are ya winning, son?", True, (255,255,255))
|
||||
screen.blit(text, (SCREEN_WIDTH - text.get_width(), 0))
|
||||
|
||||
# Flip the display
|
||||
pygame.display.flip()
|
||||
|
||||
# Ensure program maintains a rate of 30 frames per second
|
||||
clock.tick(30)
|
||||
|
||||
# All done! Stop and quit the mixer.
|
||||
pygame.mixer.music.stop()
|
||||
pygame.mixer.quit()
|
|
@ -0,0 +1,36 @@
|
|||
import pygame
|
||||
from settings import SCREEN_HEIGHT, SCREEN_WIDTH
|
||||
from pygame.locals import (RLEACCEL,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(Player, self).__init__()
|
||||
self.surf = pygame.transform.scale(
|
||||
pygame.image.load("sprites/jetfighter.png").convert(),
|
||||
(50, 36)
|
||||
)
|
||||
self.surf.set_colorkey((0, 0, 0), RLEACCEL)
|
||||
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
|
Loading…
Reference in New Issue