game

code
mail@pastecode.io avatar
unknown
python
7 months ago
8.6 kB
4
Indexable
Never
import time
import pygame
import random
import math
import pyautogui

# Inicjalizacja Pygame
pygame.init()

# Ustawienia rozmiarow plansz
screen_width = pygame.display.Info().current_w
screen_height = pygame.display.Info().current_h
board_width = screen_height + (screen_width-screen_height)/3*2  # Szerokość planszy gry
info_board_width = screen_width - board_width  # Szerokość planszy informacyjnej

flags = pygame.NOFRAME | pygame.HWSURFACE | pygame.DOUBLEBUF
gameplay_screen = pygame.display.set_mode(size=(screen_width, screen_height), flags=flags, depth=32, display=0, vsync=1)
pygame.display.set_caption("Simple game in Python")

clock = pygame.time.Clock()
dt = 0

running = True
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)

# Utwórz powierzchnie planszy gry
game_board = pygame.Surface((board_width, screen_height))

# Utwórz powierzchnie planszy informacyjnej
info_board = pygame.Surface((info_board_width, screen_height))
displayed_board_size = game_board.get_size()
class Player(pygame.sprite.Sprite):
    def __init__(self, x=(screen_width-20)/2, y=(screen_height-20)/2):
        super().__init__()
        self.radius = 20  # Promień koła
        self.image = pygame.Surface((self.radius * 2, self.radius * 2), pygame.SRCALPHA)
        pygame.draw.circle(self.image, (255, 255, 255), (self.radius, self.radius), self.radius)
        self.rect = self.image.get_rect()
        self.rect.center = (x, y)
        self.health = 100
        self.damage = 50
        self.speed = 20
        self.range = 300
        self.cost = 100
        self.points = 0
        self.bullets = pygame.sprite.Group()  # Grupa pocisków
        self.reload_speed = 20  # Prędkość ładowania
        self.reloaded = 100  # Inicjalizuj gotowość do strzału na 100%
    def update(self):
        player_colour = math.floor((100-self.health)/100*255)
        pygame.draw.circle(self.image, (player_colour, player_colour, player_colour), (self.radius, self.radius), self.radius)

        if self.reloaded >= 100:
            # Wystrzel pocisk
            bullet = Bullet(self.rect.centerx, self.rect.centery, pyautogui.position(), self.range)
            self.bullets.add(bullet)
            self.reloaded = 0
        # Aktualizuj czas ładowania
        self.reloaded += self.reload_speed
        # Aktualizuj pociski
        self.bullets.update()
#Tworzenie pocisku
class Bullet(pygame.sprite.Sprite):
    def __init__(self, x, y, target, range):
        super().__init__()
        self.x = x
        self.y = y
        self.range = range
        self.image = pygame.Surface((5, 5))
        self.image.fill(black)
        self.rect = self.image.get_rect()
        self.rect.center = (x, y)
        self.speed = 4
        self.dx = target[0] - self.rect.centerx
        self.dy = target[1] - self.rect.centery
    def update(self):
        distance = math.sqrt((self.x - self.rect.centerx) ** 2 + (self.y - self.rect.centery) ** 2)
        if distance > self.range:
            self.kill()
        distance_ = math.sqrt(self.dx ** 2 + self.dy ** 2)
        if distance_ != 0:
            self.dx /= distance_
            self.dy /= distance_
        self.rect.x += self.dx * self.speed
        self.rect.y += self.dy * self.speed

# Tworzenie przeciwników
class Enemy(pygame.sprite.Sprite):
    def __init__(self, x, y):
        super().__init__()
        self.image = pygame.Surface((20, 20))
        self.image.fill(red)
        self.rect = self.image.get_rect()
        self.rect.center = (x, y)
        self.health = 100
        self.speed = 3
        self.damage = 10

# Grupa przeciwników
enemies = pygame.sprite.Group()

# Tworzenie przeciwników
def create_enemies():
    for _ in range(5):
        x = random.randint(0, displayed_board_size[0])
        y = random.randint(0, displayed_board_size[1])

        # Krawędzie obszaru
        left_edge = (0, y)
        right_edge = (displayed_board_size[0], y)
        top_edge = (x, 0)
        bottom_edge = (x, displayed_board_size[1])
        # Oblicz odległości od punktu (x, y) do każdej z krawędzi
        distances = {
            'left_edge': math.dist((x, y), left_edge),
            'right_edge': math.dist((x, y), right_edge),
            'top_edge': math.dist((x, y), top_edge),
            'bottom_edge': math.dist((x, y), bottom_edge)
        }
        # Znajdź krawędź o najmniejszej odległości
        closest_edge = min(distances, key=distances.get)
        if closest_edge == 'left_edge':
            x_closest = 0
            y_closest = y
        elif closest_edge == 'right_edge':
            x_closest = board_width
            y_closest = y
        elif closest_edge == 'top_edge':
            x_closest = x
            y_closest = 0
        else:  # Jeśli najbliższą krawędzią jest 'bottom_edge'
            x_closest = x
            y_closest = screen_height
        enemy = Enemy(x_closest, y_closest)
        enemies.add(enemy)
def draw_text(surface, text, x, y, font_size, font_color):
    font = pygame.font.Font(None, font_size)  # Tworzenie obiektu czcionki
    text_obj = font.render(text, True, font_color)  # Tworzenie obiektu tekstu
    text_rect = text_obj.get_rect()
    text_rect.topleft = (x, y)  # Ustawienie pozycji tekstu
    surface.blit(text_obj, text_rect)  # Narysuj tekst na powierzchni
player = Player()
# Główna pętla gry
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Pobierz stan klawiszy
    keys = pygame.key.get_pressed()

    if keys[pygame.K_ESCAPE]:
        running = False

    if keys[pygame.K_w]:
        player.rect.y -= player.speed * dt * 10
        if player.rect.y < 0:
            player.rect.y = 0
    if keys[pygame.K_s]:
        player.rect.y += player.speed * dt * 10
        if player.rect.y > displayed_board_size[1] - 2 * player.radius:
            player.rect.y = displayed_board_size[1] - 2 * player.radius
    if keys[pygame.K_a]:
        player.rect.x -= player.speed * dt * 10
        if player.rect.x < 0:
            player.rect.x = 0
    if keys[pygame.K_d]:
        player.rect.x += player.speed * dt * 10
        if player.rect.x > displayed_board_size[0] - 2 * player.radius:
            player.rect.x = displayed_board_size[0] - 2 * player.radius
    # fill the screen with a color to wipe away anything from last frame
    gameplay_screen.fill(white)
    # Rysuj planszę gry na lewej stronie
    gameplay_screen.blit(game_board, (0, 0))
    # Rysuj planszę informacyjną na prawej stronie
    gameplay_screen.blit(info_board, (board_width, 0))
    game_board.fill(white)
    info_board.fill(white)
    # Narysuj linię oddzielającą planszę gry od planszy informacyjnej
    pygame.draw.line(info_board, black, (0, 0), (0, screen_height), 2)

    # Ruch przeciwników
    for enemy in enemies:
        dx = player.rect.x - enemy.rect.x
        dy = player.rect.y - enemy.rect.y
        distance = math.sqrt(dx ** 2 + dy ** 2)
        if distance != 0:
            dx /= distance
            dy /= distance
        enemy.rect.y += enemy.speed * dy
        enemy.rect.x += enemy.speed * dx

    player.bullets.update()
    player.bullets.draw(game_board)

    hits = pygame.sprite.groupcollide(player.bullets, enemies, True, False)
    for bullet, enemy_list in hits.items():
        for enemy in enemy_list:
            enemy.health -= player.damage
            if enemy.health <= 0:
                player.points +=10
                enemies.remove(enemy)
    hits = pygame.sprite.spritecollide(player, enemies, True)
    for enemy in hits:
        player.health -= enemy.damage
        enemies.remove(enemy)
        player.points -= 50
        if player.health <= 0:
            running = False
    # Tworzenie nowych przeciwników
    if random.randint(1, 100) < 2:
        create_enemies()
    # Aktualizacja
    player.update()
    game_board.blit(player.image, player.rect.topleft)
    
    player_position_text = f'x = {player.rect.x}, y = {player.rect.y}\npoints: {player.points}'
    draw_text(info_board, player_position_text, 10, 10, 24, black)
    
    # Aktualizacja i rysowanie przeciwników
    enemies.update()
    enemies.draw(game_board)
    pygame.display.update()
    dt = clock.tick(60) / 1000

draw_text(gameplay_screen, 'u have lost', 100, 100, 48, black)
pygame.display.update()
time.sleep(5)
pygame.quit()