Получение двух движущихся объектов из одного класса для взаимодействия (pygame) - PullRequest
0 голосов
/ 23 апреля 2020

Это мой класс Игрока, в настоящее время у меня есть оба игрока, взаимодействующие со стенами (это отдельный класс в коде, но не вставленный в этот вопрос), но мне нужен способ, чтобы игроки не могли проходить друг через друга так же, как игроки не могу пройти сквозь стены. Проблема в том, что я могу использовать self.rect.right = block.rect.left, чтобы не дать игроку пройти через стену, когда его правая сторона касается левой стороны стены, но я не могу использовать self.rect.right = self.rect.left, чтобы не дать игроку 1 пройти через игрока 2, потому что это не так. укажите, что один относится к игроку один, а другой относится к игроку 2. Будем благодарны за любые предложения.

class Player(pygame.sprite.Sprite):

    # Constructor function
    def __init__(self, x, y, colour):
        # Call the parent's constructor
        super().__init__()

        # Set height, width
        self.image = pygame.Surface([50, 50])
        self.image.fill(colour)

        # Make our top-left corner the passed-in location.
        self.rect = self.image.get_rect()
        self.rect.y = y
        self.rect.x = x

        # Set speed vector
        self.change_x = 0
        self.change_y = 0
        self.walls = None

    def changespeed(self, x, y):
        self.change_x += x
        self.change_y += y

    def update(self):
        # Move left/right
        self.rect.x += self.change_x

        # Did this update cause us to hit a wall?
        block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)
        for block in block_hit_list:
            # If we are moving right, set our right side to the left side of
            # the item we hit
            if self.change_x > 0:
                self.rect.right = block.rect.left
            else:
                # Otherwise if we are moving left, do the opposite.
                self.rect.left = block.rect.right

        # Move up/down
        self.rect.y += self.change_y

        # Check and see if we hit anything
        block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)
        for block in block_hit_list:

            # Reset our position based on the top/bottom of the object.
            if self.change_y > 0:
                self.rect.bottom = block.rect.top
            else:
                self.rect.top = block.rect.bottom

Здесь я создаю игроков как объекты

P1 = Player(200, 200, BLUE)
P2 = Player(300, 300, WHITE)
P1.walls = wall_list
P2.walls = wall_list
all_sprite_list.add(P1)
all_sprite_list.add(P2)

Полный код :

import pygame
import ctypes
user32 = ctypes.windll.user32

# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (50, 50, 255)

# Screen dimensions
SCREEN_WIDTH = user32.GetSystemMetrics(78)#1600
SCREEN_HEIGHT = user32.GetSystemMetrics(79)#900
WALL_THICKNESS = 10
MAP_WIDTH = SCREEN_WIDTH - 300 #1300
MAP_HEIGHT = SCREEN_HEIGHT - 160 #700
HEIGHT = SCREEN_HEIGHT - MAP_HEIGHT
#START CORDS = 150, 150


class Player(pygame.sprite.Sprite):

    # Constructor function
    def __init__(self, x, y, colour):
        # Call the parent's constructor
        super().__init__()

        # Set height, width
        self.image = pygame.Surface([50, 50])
        self.image.fill(colour)

        # Make our top-left corner the passed-in location.
        self.rect = self.image.get_rect()
        self.rect.y = y
        self.rect.x = x

        # Set speed vector
        self.change_x = 0
        self.change_y = 0
        self.walls = None

    def changespeed(self, x, y):
        self.change_x += x
        self.change_y += y

    def update(self):
        # Move left/right
        self.rect.x += self.change_x

        # Did this update cause us to hit a wall?
        block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)
        for block in block_hit_list:
            # If we are moving right, set our right side to the left side of
            # the item we hit
            if self.change_x > 0:
                self.rect.right = block.rect.left
            else:
                # Otherwise if we are moving left, do the opposite.
                self.rect.left = block.rect.right

        # Move up/down
        self.rect.y += self.change_y

        # Check and see if we hit anything
        block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)
        for block in block_hit_list:

            # Reset our position based on the top/bottom of the object.
            if self.change_y > 0:
                self.rect.bottom = block.rect.top
            else:
                self.rect.top = block.rect.bottom

class Wall(pygame.sprite.Sprite):
    def __init__(self, x, y, width, height):
        # Call the parent's constructor
        super().__init__()

        # Make a blue wall, of the size specified in the parameters
        self.image = pygame.Surface([width, height])
        self.image.fill(BLUE)

        # Make our top-left corner the passed-in location.
        self.rect = self.image.get_rect()
        self.rect.y = y
        self.rect.x = x


# Call this function so the Pygame library can initialize itself
pygame.init()

# Create an 800x600 sized screen
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT], pygame.FULLSCREEN)

# Set the title of the window
pygame.display.set_caption('Haste')

# List to hold all the sprites
all_sprite_list = pygame.sprite.Group()

# Make the walls. (x_pos, y_pos, width, height)
wall_list = pygame.sprite.Group()
#--------------------------------------------------------------------WALLS

#OUTER WALL 700 1500 - 1490. 1300

wall = Wall(150, 150, WALL_THICKNESS, MAP_HEIGHT - 150)#map height (left verticle)
wall_list.add(wall)
all_sprite_list.add(wall)

wall = Wall(160, 150, MAP_WIDTH, WALL_THICKNESS)#(top across)
wall_list.add(wall)
all_sprite_list.add(wall)

wall = Wall(160, MAP_HEIGHT - 10, MAP_WIDTH, WALL_THICKNESS)#(bottom across)
wall_list.add(wall)
all_sprite_list.add(wall)

wall = Wall(MAP_WIDTH + 150, 150, WALL_THICKNESS, MAP_HEIGHT - 150)#(right verticle)
wall_list.add(wall)
all_sprite_list.add(wall)

# Create the player paddle object
P1 = Player(200, 200, BLUE)
P2 = Player(300, 300, WHITE)
P1.walls = wall_list
P2.walls = wall_list
all_sprite_list.add(P1)
all_sprite_list.add(P2)

clock = pygame.time.Clock()

done = False
sped = 9
speed = -9
while not done:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                P1.changespeed(speed, 0)
            if event.key == pygame.K_RIGHT:
                P1.changespeed(sped, 0)
            if event.key == pygame.K_UP:
                P1.changespeed(0, speed)
            if event.key == pygame.K_DOWN:
                P1.changespeed(0, sped)
            if event.key == pygame.K_a:
                P2.changespeed(speed, 0)
            if event.key == pygame.K_d:
                P2.changespeed(sped, 0)
            if event.key == pygame.K_w:
                P2.changespeed(0, speed)
            if event.key == pygame.K_s:
                P2.changespeed(0, sped)

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                P1.changespeed(sped, 0)
            if event.key == pygame.K_RIGHT:
                P1.changespeed(speed, 0)
            if event.key == pygame.K_UP:
                P1.changespeed(0, sped)
            if event.key == pygame.K_DOWN:
                P1.changespeed(0, speed)
            if event.key == pygame.K_a:
                P2.changespeed(sped, 0)
            if event.key == pygame.K_d:
                P2.changespeed(speed, 0)
            if event.key == pygame.K_w:
                P2.changespeed(0, sped)
            if event.key == pygame.K_s:
                P2.changespeed(0, speed)

    all_sprite_list.update()

    screen.fill(BLACK)

    all_sprite_list.draw(screen)

    pygame.display.flip()

    clock.tick(60)

pygame.quit()

1 Ответ

2 голосов
/ 23 апреля 2020

Я не могу использовать self.rect.right = self.rect.left ...

Правильно: вы должны написать свой код для ссылки на каждый Player в зависимости от ситуации. Вы не можете ссылаться на оба из них как self.

Очень просто, вы должны использовать соответствующие переменные, такие как:

P1.rect.right == P2.rect.left

Независимо от того, проектируете ли вы Все это зависит от дизайна вашей системы, когда все oop проходят через пары игроков или по очереди сравнивают каждого игрока с другими. Например, имея только двух игроков, вы просто пишете al oop, который переворачивается между двумя игроками:

active, passive = P1, P2   # P1 is the currently active player

while run_game:
    ...
    if active.rect.right == passive.rect.left:
        ... #avoid collision
    ...
    # Switch active player (take turns) at end of loop
    active, passive = passive, active
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...