我最近学过python并且有一个涉及pygame的学校项目,但是我们没有被教过多少,反正我一直试图做一个迷宫,其中玩家(白色方块)与红色块碰撞(某些事情发生了(一个任务发生在碰撞的结果),但我尝试了各种各样的东西,但似乎没有任何工作(我仍然是pygame的新手)。好的,感谢您的帮助,但请记住,我是初学者,我的代码可能有很多错误
import pygame
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (50, 50, 255)
GREEN = (33,206,14)
RED = (255,0,0)
GREY = (43,44,61)
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
# Call the parent's constructor
super().__init__()
#self.image = pygame.image.load("basketballsprite.png").convert()
#self.image.set_colorkey(WHITE)
self.image = pygame.Surface([15, 15])
self.image.fill(WHITE)
# Make our top-left corner the passed-in location.
self.rect = self.image.get_rect()
self.rect.y = y
self.rect.x = x
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):
self.rect.x += self.change_x
block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)
for block in block_hit_list:
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
self.rect.y += self.change_y
block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)
for block in block_hit_list:
if self.change_y > 0:
self.rect.bottom = block.rect.top
else:
self.rect.top = block.rect.bottom
#if pygame.sprite.spritecollide(Endpoint, self.image,True):
class Wall(pygame.sprite.Sprite):
def __init__(self, x, y, width, height):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(GREY)
self.rect = self.image.get_rect()
self.rect.y = y
self.rect.x = x
class Endpoint(pygame.sprite.Sprite):
def __init__(self, x, y, width, height):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.y = y
self.rect.x = x
pygame.init()
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
pygame.display.set_caption('A "Little" Maze Game...')
all_sprite_list = pygame.sprite.Group()
wall_list = pygame.sprite.Group()
Endpoint_list = pygame.sprite.Group()
wall = Wall(0, 0, 10, 600)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(0, 590, 400, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(450, 590, 390, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(10, 0, 790, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(30, 200, 85, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(790, 0, 10, 800)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(70, 0, 10, 80)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(390, 500, 10, 150)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(450, 500, 10, 150)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(790, 0, 10, 800)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(400, 600, 2000, 15)
wall_list.add(wall)
all_sprite_list.add(wall)
Endpoint = Endpoint(400, 590, 50, 15)
wall_list.add(wall)
all_sprite_list.add(Endpoint)
wall = Wall(35, 100, 50, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(100, 95, 10, 80)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(100, 10, 10, 65)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(130, 200, 10, 90)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(130, 225, 90, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(50, 100, 10, 45)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(10, 160, 40, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(300, 200, 10, 250)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(240, 260, 60, 10)
wall_list.add(wall)
all_sprite_list.add(wall)
'''
wall = Wall(x, y, width, height)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(x, y, width, height)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(x, y, width, height)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(x, y, width, height)
wall_list.add(wall)
all_sprite_list.add(wall)
wall = Wall(x, y, width, height)
wall_list.add(wall)
all_sprite_list.add(wall)
'''
player = Player(34, 34)
player.walls = wall_list
all_sprite_list.add(player)
clock = pygame.time.Clock()
done = False
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:
player.changespeed(-2, 0)
elif event.key == pygame.K_RIGHT:
player.changespeed(2, 0)
elif event.key == pygame.K_UP:
player.changespeed(0, -2)
elif event.key == pygame.K_DOWN:
player.changespeed(0, 2)
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.changespeed(2, 0)
elif event.key == pygame.K_RIGHT:
player.changespeed(-2, 0)
elif event.key == pygame.K_UP:
player.changespeed(0, 2)
elif event.key == pygame.K_DOWN:
player.changespeed(0, -2)
screen.fill(BLACK)
if pygame.sprite.spritecollideany(player,Endpoint_list):
exit()
all_sprite_list.update()
pygame.draw.circle(screen, BLUE, [40, 40], 30)
all_sprite_list.draw(screen)
pygame.display.flip()
clock.tick(60)
pygame.quit()
答案 0 :(得分:0)
您只需将Endpoint
实例添加到用于碰撞检测的Endpoint_list
即可。此外,不要对实例和类使用相同的名称,否则您将覆盖该类,并且不能再创建新实例。
endpoint = Endpoint(400, 590, 50, 15) # Just use a lowercase name for the instance.
all_sprite_list.add(endpoint)
Endpoint_list.add(endpoint)
或者,您可以使用pygame.sprite.collide_rect()
功能。
if pygame.sprite.collide_rect(player, endpoint):
print('exit reached')
或者只是rects的colliderect
方法:
if player.rect.colliderect(endpoint):