精灵不能正确出现在屏幕上

2024-05-14 19:12:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个游戏编程类,我应该做一个游戏,有精灵从屏幕顶部下降,你抓住或避免。现在我正在努力让物体落下,一开始它们还在工作,但后来我搞砸了一些东西,因为现在它们在落下屏幕时显得零零碎碎/随机消失。我试着改变代码中的各种东西来解决这个问题,但我已经被困了一段时间。你知道吗

我很肯定我把一些东西弄糟了,要么把煤/糖果放到屏幕上,要么在其他东西消失后再加上新的东西,但我包含了很大一部分代码,以防万一有什么东西把它弄糟了。我将强调下面可能出现混乱的部分。你知道吗

哦,我也一直很欣赏关于如何使代码更简洁/高效的评论,但是老师希望代码与他教我们的类似,所以如果你有一个特定的修复,如果它能保持代码与现在的相似,我会非常感激!你知道吗

非常感谢!你知道吗

#Setting up time/timer
time = 6000
TICKTOCK = 0 
pygame.time.set_timer (TICKTOCK+1, 10)

#Class for candy
class Candy: 
    def __init__(self):
        self.x = random.randint(0, SCREEN_WIDTH - 150)
        self.y = random.randint(-1000, 0)
        self.image_Candy = pygame.image.load('konpeito.png')
        self.height = self.image_Candy.get_height()
        self.width = self.image_Candy.get_width()

    def collide (self, sprite):
        selfRect = pygame.Rect(self.x, self.y, self.width, self.height)
        spriteRect = pygame.Rect(sprite.x, sprite.y, sprite.width, sprite.height)
        if selfRect.colliderect(spriteRect):
            return True
        else: 
            return False

#class for coal
class Coal: 
    def __init__(self):
        self.x = random.randint(0, SCREEN_WIDTH - 150)
        self.y = random.randint(-1000, SCREEN_HEIGHT)
        self.image_Coal = pygame.image.load('coal.png') 
        self.width = self.image_Coal.get_width()
        self.height = self.image_Coal.get_height()

    def collide (self, sprite):
        selfRect = pygame.Rect(self.x, self.y, self.width, self.height)
        spriteRect = pygame.Rect(sprite.x, sprite.y, sprite.width, sprite.height)
        if selfRect.colliderect(spriteRect):
            return True
        else: 
            return False

#class for sootsprite (collects candy and coal)
class Sootsprite:
    def __init__(self):
        self.x = random.randint(0, SCREEN_WIDTH)
        self.y = random.randint(0, SCREEN_HEIGHT)
        self.image_bowl = pygame.image.load('sootsprite.png')
        self.height = self.image_bowl.get_height()
        self.width = self.image_bowl.get_width()

clock = pygame.time.Clock() 
fps = 10

#Creating candies and rocks
bowl = []
for i in range (15):
    candyInstance = Candy()
    bowl.append(candyInstance)

rocks = []
for i in range (8):
    coalInstance = Coal()
    rocks.append(coalInstance)

catch = Sootsprite()   

playground.fill(cyan)

Game_Over = False

while not Game_Over: 
    font = pygame.font.SysFont(None, 30)
    endfont = pygame.font.SysFont(None, 100)
    text = font.render('Points: ' + str(total) + '/15', True, black)
    playground.blit(text, (0,0))
    timer = font.render('Time remaining: ' + str(time), True, black)
    playground.blit(timer, (0, 40))
    end = endfont.render("GAME OVER." + str(total) + " POINTS EARNED", True, black)

这是我在屏幕上闪现的东西,可能搞砸了:

    playground.blit(catch.image_bowl, (catch.x, catch.y))
    playground.blit(candyInstance.image_Candy, (candyInstance.x, candyInstance.y))
    playground.blit(coalInstance.image_Coal, (coalInstance.x, coalInstance.y))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            Game_Over = True
        if event.type == TICKTOCK+1:
            time -=1
        #ends game when time is over
        if time < 0:
            playground.blit(end, (300, 400))

        #moving sootsprite with mouse
        if event.type == MOUSEMOTION:
            (catch.x, catch.y) = pygame.mouse.get_pos() 

    #making candy fall    
    for candyInstance in bowl:
        if candyInstance.x <= 0: 
            candyInstance.x += 0 
        elif candyInstance.x >= 0 :
            candyInstance.x -=0 

        if candyInstance.y <= (SCREEN_HEIGHT+150): 
            candyInstance.y += 20  
        elif candyInstance.y > (SCREEN_HEIGHT + 150) :
            candyInstance.y = -100 

这就是我删除东西并添加新东西的地方,可能会把事情搞砸:

    #removing candy when collected        
    for candyInstance in bowl:
        if candyInstance.collide(catch):
            bowl.remove(candyInstance)
            total += 1
            candyInstance = Candy()
            bowl.append(candyInstance)

    #making coal fall
    for coalInstance in rocks:
        if coalInstance.x <= 0: 
            coalInstance.x += 0 
        elif coalInstance.x >= 0 :
            coalInstance.x -=0 

        if coalInstance.y <= (SCREEN_HEIGHT + 200): 
            coalInstance.y += 20  
        elif coalInstance.y > (SCREEN_HEIGHT + 200) :
            coalInstance.y = -100        

这也是我删除对象并添加新对象的地方:

    #removing coal when collected
    for coalInstance in rocks:
        if coalInstance.collide(catch):
            rocks.remove(coalInstance)
            total -= 1
            coalInstance = Coal()
            rocks.append(coalInstance)

    pygame.display.flip()
    playground.fill(cyan)
    clock.tick (fps) 

pygame.quit()

Tags: imageselfforgetiftimewidthscreen
1条回答
网友
1楼 · 发布于 2024-05-14 19:12:59

你只有一块糖果。使用for循环blit所有糖果。你知道吗

for candyInstance in bowl:
    playground.blit(candyInstance.image_Candy, (candyInstance.x, candyInstance.y))

你也有同样的问题rocks

for coalInstance in rocks:
    playground.blit(coalInstance.image_Coal, (coalInstance.x, coalInstance.y))

相关问题 更多 >

    热门问题