精灵在屏幕上没有正确显示 - pygame

时间:2016-01-11 03:05:26

标签: python pygame

我正在游戏编程课程中,我应该制作一个游戏,其中有精灵从你捕获或避开的屏幕顶部掉落。现在我正在努力使物体掉落并且它们首先工作但后来我搞砸了一些东西,因为现在它们在跌落屏幕时显得曲折/随机消失。我已经尝试改变代码中的各种东西来解决问题,但我已经被困了一段时间。

我很确定我搞砸了一些东西要么将煤炭/糖果点到屏幕上,要么一旦其他人消失就添加新的东西掉落,但是我包含了大部分代码以防万一有些东西弄乱它。我将重点介绍下面可能搞砸的部分。

哦,我也非常感谢有关如何使代码更简洁/高效的评论,但老师希望代码与他教我们的方式相似,所以如果你确实有一个特定的修复,如果它可以保持代码类似于现在我真的很感激!

非常感谢你!

#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()

1 个答案:

答案 0 :(得分:0)

blit只有一个糖果。使用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))