Обнаружение столкновений Pygame с группамиPython

Программы на Python
Anonymous
Обнаружение столкновений Pygame с группами

Сообщение Anonymous »

Код работает, также не беспокойтесь о классе блока для будущих битов. Я новичок в pygame, и это моя первая игра, так что могу ли я получить некоторую помощь, пожалуйста?
Я пробовал настроить коллизии несколькими способами, чтобы печатать коллизии, обнаруженные в консоли, но это не так. вот такой код:
import pygame
import sys
``
# Initialize Pygame
pygame.init()
# Set up the display
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption("The Square")

# Load the background image
background = pygame.image.load("back.jpeg")
Cave = pygame.image.load("cave.png")

# Set up scrolling variables
scroll_x = 0
scroll_y = 0
scroll_speed = 2

# Define the Player class
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((10, 10))
self.image.fill((255, 0, 0)) # Red square
self.rect = self.image.get_rect()
self.rect.center = (250, 250) # Start in the middle of the screen
self.speed = 2

def update(self, keys):
global scroll_x, scroll_y
if keys[pygame.K_LEFT]:
scroll_x += self.speed
if keys[pygame.K_RIGHT]:
scroll_x -= self.speed
if keys[pygame.K_UP]:
scroll_y += self.speed
if keys[pygame.K_DOWN]:
scroll_y -= self.speed
class Box(pygame.sprite.Sprite):
def __init__(self, color, size, x, y, speed):
super().__init__()
self.image = pygame.Surface([size, size])
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.speed = speed

def update(self, keys):
if keys[pygame.K_LEFT]:
self.rect.x += self.speed
if keys[pygame.K_RIGHT]:
self.rect.x -= self.speed
if keys[pygame.K_UP]:
self.rect.y += self.speed
if keys[pygame.K_DOWN]:
self.rect.y -= self.speed
class StationaryCube(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface((50, 50))
self.image.fill((0, 0, 255)) # Blue cube
self.rect = self.image.get_rect()
self.rect.topleft = (x, y)

def draw(self, surface, scroll_x, scroll_y):
surface.blit(self.image, (self.rect.x + scroll_x, self.rect.y + scroll_y))

# Initialize the player
monsters = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
teleport = pygame.sprite.Group()
player = Player()
cube = StationaryCube(480, 400)
all_sprites.add(cube)
teleport.add(cube)

all_sprites.add(player)

# Main game loop
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
all_sprites.update(keys)

# Ensure the player doesn't move beyond the background boundaries
if scroll_x > 0:
scroll_x = 0
if scroll_x < -background.get_width() +500:
scroll_x = -background.get_width() + 500

if scroll_y > 0:
scroll_y = 0
if scroll_y < -background.get_height() + 500:
scroll_y = -background.get_height() + 500
x, y = player.rect.topleft

# Update position based on key presses
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
x += 5
if keys[pygame.K_RIGHT]:
x -= 5
if keys[pygame.K_UP]:
y += 5
if keys[pygame.K_DOWN]:
y -= 5
# Draw the background
screen.fill((0, 0, 0)) # Clear the screen with black
screen.blit(background, (scroll_x, scroll_y))
for sprite in all_sprites:
if isinstance(sprite, Player):
screen.blit(sprite.image, sprite.rect.topleft)
if isinstance(sprite, StationaryCube):
sprite.draw(screen, scroll_x, scroll_y)
if pygame.sprite.spritecollideany(player, teleport):
print("Collision detected!")

pygame.display.flip()
clock.tick(60)`

``


Подробнее здесь: https://stackoverflow.com/questions/789 ... ith-groups

Вернуться в «Python»