【问题标题】:Pygame Sprites DissapearingPygame 精灵消失
【发布时间】:2017-04-17 21:36:30
【问题描述】:

在我之前的question 中,我遇到了精灵问题。所以我决定在绘制它们之前使用清晰的方法。它似乎奏效了,但是当精灵到达屏幕底部时,也就是它们应该回到顶部的时候,它们消失了。只剩下 9 个中的 2 个。

在他们触底之前。



在他们到达底部并重置到顶部之后。


主文件

#!/usr/bin/python
VERSION = "0.1"
import os, sys, raindrop
from os import path

try:
    import pygame
    from pygame.locals import *
except ImportError, err:
    print 'Could not load module %s' % (err)
    sys.exit(2)

# main variables
WIDTH, HEIGHT, FPS = 300, 300, 30


# initialize game
pygame.init()
screen = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Rain and Rain")

# background
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((40,44,52))

# blitting
screen.blit(background,(0,0))
pygame.display.flip()

# clock for FPS settings
clock = pygame.time.Clock()


def main():
    raindrops = pygame.sprite.Group()

    # a function to create new drops
    def newDrop():
        nd = raindrop.Raindrop()
        raindrops.add(nd)

    # creating 10 rain drops
    for x in range(0,9): newDrop()

    # variable for main loop
    running = True

    # event loop
    while running:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False


        screen.blit(background,(100,100))
        raindrops.clear(screen,background)
        raindrops.update()
        raindrops.draw(screen)
        pygame.display.flip()
    pygame.quit()

if __name__ == '__main__': main()

raindrop.py(类)

import pygame
from pygame.locals import *
from os import path
from random import randint
from rain import HEIGHT

img_dir = path.join(path.dirname(__file__), 'img')

class Raindrop(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.width = randint(32, 64)
        self.height = self.width + 33
        self.image = pygame.image.load(path.join(img_dir, "raindrop.png")).convert_alpha()
        self.image = pygame.transform.scale(self.image, (self.width, self.height))
        self.speedy = 5 #randint(1, 8)
        self.rect = self.image.get_rect()
        self.rect.x = randint(0, 290)
        self.rect.y = -self.height

    def update(self):
        self.rect.y += self.speedy
        if self.rect.y == HEIGHT:
            self.rect.y = -self.height
            self.rect.x = randint(0, 290)

【问题讨论】:

  • 不包含所有代码;改为创建minimal reproducible example。这将使问题对其他人更有益,因为它变得更易于阅读、理解、测试/验证,并且也使试图找出问题的人更容易。
  • 下次会考虑这个xD

标签: python python-2.7 pygame sprite


【解决方案1】:
if self.rect.y == HEIGHT:

问题是一些雨滴会超过HEIGHT,因为speedy 是[1,8] 范围内的随机数,所以speedy 的倍数可能不能被2*HEIGHT 整除。例如,speedy = 7rect.y 从 -HEIGHT = -300 变为 -293、-286、...、295,然后变为大于 300 的 302,因此 == 检查永远不会为真,并且雨滴永远落下去。

>= 进行简单更改即可解决问题:

if self.rect.y >= HEIGHT:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 2023-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多