为什么当我同时点击两个精灵时会出现这个bug ?

  • 本文关键字:两个 精灵 bug python pygame
  • 更新时间 :
  • 英文 :


我正在使用pygame制作一个简单的游戏,在这个游戏中,你要尽可能快地点击贴图,直到你错过一个贴图。这是我目前取得的进展。有时候,当我点击一个贴图时(通常是当两个贴图相邻,你点击它们之间),其中一个会做它们应该做的事情,而另一个会从屏幕上消失。

import pygame
import random
import sys
#Setting up all possible Tile positions
grid = [[0,0],  [0,150],  [0,300],  [0,450],  [0,600],
[150,0],[150,150],[150,300],[150,450],[150,600],
[300,0],[300,150],[300,300],[300,450],[300,600],
[450,0],[450,150],[450,300],[450,450],[450,600],
[600,0],[600,150],[600,300],[600,450],[600,600]]
taken = []
#Classes
class Cursor(pygame.sprite.Sprite):
def __init__(self, pic):
super().__init__()
self.image = pygame.image.load(pic).convert_alpha()
self.image = pygame.transform.scale(self.image, (50,50))
self.rect = self.image.get_rect()
def destroyTile(self):
pygame.sprite.spritecollide(cursor, tileGroup, True)

def update(self):
self.rect.topleft = pygame.mouse.get_pos()  
class Tiles(pygame.sprite.Sprite):
def __init__(self, tileSize, color, x, y):
super().__init__()
self.image = pygame.Surface(([tileSize, tileSize]))
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.topleft = [x, y]
def drawTiles():
takenLen = len(taken)
while takenLen != 3:
m = random.randint(0,24)
x, y = grid[m]
if grid[m] not in taken:
blackTile = Tiles(150, black, x, y)
blackTile.add(tileGroup)   
taken.append(grid[m])
takenLen += 1

def handleTiles():
mx, my = pygame.mouse.get_pos()
modx = mx % 150
mody = my % 150
x = mx - modx
y = my - mody
taken.remove([x, y])
drawTiles()   
def drawRedTile():
mx, my = pygame.mouse.get_pos()
modx = mx % 150
mody = my % 150
x = mx - modx
y = my - mody
redTile = Tiles(150, red, x, y)
redTile.add(tileGroup)
#Colours
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
blue = (0, 0, 255)
grey = (46, 46, 46)
#Initializing Pygame
pygame.init()
clock = pygame.time.Clock()
#Screen
screenWidth = 750
screenHeight = 900
screen = pygame.display.set_mode((screenWidth, screenHeight))
pygame.display.set_caption("Tiles Game")
whiteSurface = pygame.Surface((750, 750))
whiteSurface.fill(white)
pygame.mouse.set_visible(False)
#Blue line
line = pygame.Surface((750, 10))
line.fill(blue)
#Groups
tileGroup = pygame.sprite.Group()
cursor = Cursor("cursor.png")
cursorGroup = pygame.sprite.Group()
cursorGroup.add(cursor)
score = 0
drawTiles()
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
score += 1
print(score)
print(taken)
print(tileGroup) 
cursor.destroyTile()
handleTiles()

#Background
screen.fill(grey)
screen.blit(whiteSurface, (0,0))
screen.blit(line, (0,750))

tileGroup.draw(screen)
cursorGroup.draw(screen)
cursorGroup.update()
pygame.display.update()

在代码中,我尝试使用print语句来查看似乎已经消失的tile是否仍然存在。当这种情况发生时,我假设这个贴图不再属于它的群组,因为这个贴图群组中的精灵数量从3个变成了2个。但是列表显示了所有的位置仍然显示有3个位置被占据。我仍然可以点击平铺如果我点击应该有平铺的地方然后平铺就回来了。我认为当一个贴图没有被点击时游戏应该退出,但如果有一个"不可见",它就不会退出。

我怎么做,使这个错误不会发生,每一个新的瓷砖是可见的?

问题是光标有一个区域,一次可以击中多个块。因此,在destroyTile中,一次可以删除多个块:

def destroyTile(self):
pygame.sprite.spritecollide(cursor, tileGroup, True)

然而,handleTiles函数不能处理这个,因为它只能从taken列表中删除一个块位置。我建议简化代码,并在删除块时完全从tileGroup重新创建taken列表:

def handleTiles():
taken.clear()
for tile in tileGroup:
x, y = tile.rect.topleft
taken.append([x, y])
drawTiles()  

最新更新