Python Pygame问题:碰撞很奇怪
在我的pygame上,我遇到了一个问题:碰撞很奇怪,它们工作了一半:在边界上,它们工作,但在地图的中间不(对不起,颜色) 以下是更换的代码部分:Python Pygame问题:碰撞很奇怪,python,pygame,Python,Pygame,在我的pygame上,我遇到了一个问题:碰撞很奇怪,它们工作了一半:在边界上,它们工作,但在地图的中间不(对不起,颜色) 以下是更换的代码部分: if game.pressed.get(pygame.K_d): if lvl.structure[game.player.rect.y // 64 + camY // 64][game.player.rect.x // 64 + 1 + camX // 64] is not 'T': game.pla
if game.pressed.get(pygame.K_d):
if lvl.structure[game.player.rect.y // 64 + camY // 64][game.player.rect.x // 64 + 1 + camX // 64] is not 'T':
game.player.move_right()
#print(game.player.rect.x)
if not screen_rect.contains(game.player.rect):
game.player.rect.x -= HEIGHT -60
camX += -HEIGHT
elif game.pressed.get(pygame.K_q):
if lvl.structure[game.player.rect.y // 64 + camY // 64][game.player.rect.x // 64 + camX // 64] is not 'T':
game.player.move_left()
#print(game.player.rect.x)
if not screen_rect.contains(game.player.rect):
game.player.rect.x += HEIGHT - 60
camX += HEIGHT
elif game.pressed.get(pygame.K_s):
print(game.player.rect.y)
if lvl.structure[game.player.rect.y // 64 + 1 + camY // 64][game.player.rect.x // 64 + camX // 64] is not 'T':
game.player.move_down()
if not screen_rect.contains(game.player.rect):
game.player.rect.y -= WIDHT - 80
camY -= WIDHT
elif game.pressed.get(pygame.K_z):
if lvl.structure[game.player.rect.y // 64 + camY // 64][game.player.rect.x // 64 + camX // 64] is not 'T':
game.player.move_up()
#print(game.player.rect.y)
if not screen_rect.contains(game.player.rect):
game.player.rect.y += WIDHT - 80
camY += WIDHT
lvl.afficher(screen, 0, 0, camX, camY, game.player.rect.x, game.player.rect.y)
screen.blit(game.player.image, game.player.rect)
pygame.display.flip()
在这一级别:
class Level(pygame.sprite.Sprite):
def __init__(self):
self.structure = 0
self.map = []
def generer(self):
with open("niveau.txt", "r") as fichier:
structure_niveau = []
for ligne in fichier:
ligne_niveau = []
for sprite in ligne:
if sprite != '\n':
ligne_niveau.append(sprite)
structure_niveau.append(ligne_niveau)
self.structure = structure_niveau
def afficher(self, fenetre, x, y, camX, camY, playerX, playerY):
tailleSprite = 64
self.collide = False
#Camera.__init__(self, x, y)
grass = pygame.image.load("assets/bloc/grass.png").convert_alpha()
tree = pygame.image.load("assets/bloc/tree_grass.png").convert_alpha()
no_texture = pygame.image.load("assets/bloc/no_texture.png").convert_alpha()
num_ligne = 0
for ligne in self.structure:
num_case = 0
for sprite in ligne:
x = num_case * tailleSprite + camX
y = num_ligne * tailleSprite + camY
sprite_rect = pygame.Rect(x, y, 64, 64)
screenRect = pygame.Rect(x, y, 1088, 836)
aroundPlayer = pygame.Rect(playerX, playerY, 46, 64)
if sprite == 'G':
if screenRect.contains(sprite_rect):
fenetre.blit(grass, (x, y))
elif sprite == 'T':
if screenRect.contains(sprite_rect):
fenetre.blit(tree, (x, y))
else:
if screenRect.contains(sprite_rect):
fenetre.blit(no_texture, (x, y))
#print(x, y)
num_case += 1
num_ligne += 1
提前谢谢你 你真的是什么意思?你的意思是?对不起,我不明白,试着用
.colliderect()
代替.contains()
。Everywhere.On lvl.structure[game.player.rect.y//64+1+camY//64][game.player.rect.x//64+camX//64]不是“T”?不是。你应该使用方法collide rect
,而不是方法contains
。(我数了7次)你真的是什么意思?你的意思是?对不起,我不明白,试着用.colliderect()
代替.contains()
。Everywhere.On lvl.structure[game.player.rect.y//64+1+camY//64][game.player.rect.x//64+camX//64]不是“T”?不是。你应该使用方法collide rect
,而不是方法contains
。(I计数包含7次)