test
commit
85b1a5868f
|
@ -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, 50))
|
||||
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(1, 3)
|
||||
|
||||
# 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,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
|
|
@ -0,0 +1,46 @@
|
|||
import pygame
|
||||
# Vieglākai piekļuvei notikumiem
|
||||
from pygame.locals import (K_UP,K_DOWN,K_LEFT,K_RIGHT,K_ESCAPE,KEYDOWN,QUIT)
|
||||
|
||||
|
||||
# Izveidosim ekrāna izmēra konstantes
|
||||
SCREEN_WIDTH = 800
|
||||
SCREEN_HEIGHT = 600
|
||||
# Izveidosim screen objektu
|
||||
# Padodam izmērus kā kortežu (vai sarakstu)!
|
||||
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
||||
|
||||
# Mainīgais, lai kontrolētu cilpu
|
||||
running = True
|
||||
# Galvenā cilpa
|
||||
while running:
|
||||
# Apskatām visus notikumus
|
||||
for event in pygame.event.get():
|
||||
# Vai lietotājs ir nospiedis tastatūras pogu?
|
||||
if event.type == KEYDOWN:
|
||||
pass
|
||||
# Vai šī ir Escape poga?
|
||||
if event.key == K_ESCAPE:
|
||||
running = False
|
||||
# Vai lietotājs ir nospiedis loga aizvēršanas pogu?
|
||||
elif event.type == QUIT:
|
||||
running = False
|
||||
|
||||
# Izveidojam virsmu un padodam kā argumentu garumu un platumu
|
||||
surf = pygame.Surface((50, 50))
|
||||
# Iestatām virsmas fona krasu
|
||||
surf.fill((255, 25, 0))
|
||||
# Var piekļūt arī virsmas Rect, izmantojot .get_rect()
|
||||
rect = surf.get_rect()
|
||||
surf_center = (
|
||||
(SCREEN_WIDTH - surf.get_width()) / 2,
|
||||
(SCREEN_HEIGHT - surf.get_height()) / 2
|
||||
)
|
||||
screen.blit(surf, surf_center)
|
||||
pygame.display.flip()
|
||||
|
||||
# Apstrādāsim notikumus…
|
||||
# Kaut kādā brīdī iziesim no cilpas (running = False)
|
||||
pygame.quit()
|
||||
|
||||
pygame.init()
|
Loading…
Reference in New Issue