python - Sprite 没有向下 move

标签 python pygame sprite move

我不知道如何通过鼠标单击创建新的 Sprite 并向所有新 Sprite 添加运动。

我创建了一个简单的游戏:

  • 钻石:用 K_LEFTK_RIGHT 向左/右 move
  • Pad:用鼠标左/右 move

如果您单击鼠标,则会根据鼠标单击次数添加心形并绘制 Sprite heart0、heart1 等。

如果你点击鼠标,你会画一个 Sprite 心,它会落在钻石所在的地方,心应该向下 move ......

问题:心脏没有向下 move 。我无法画出多颗心。 目前我只能在钻石所在的地方画一个心并附加心。

您知道我的代码有什么问题,所以我无法获得所需的功能吗?

import pygame, sys
from pygame.locals import *

pygame.init()
pygame.mouse.set_visible(1)
clock = pygame.time.Clock()

screen_width = 640
screen_height = 480
screen = pygame.display.set_mode((screen_width, screen_height))

myfont = pygame.font.SysFont("Arial", 15)

hearts = [] 
clicks = []

class HeartSprite(object):

    def __init__(self, pozx, pozy):

        self.image = pygame.image.load("heart.png")
        self.posx = pozx
        self.posy = pozy   
        self.rect = self.image.get_rect()
        self.going_down = True
        self.next_update_time = 0

    def update(self, current_time, bottom):
        if self.next_update_time < current_time:
            ### bounce back
            #if self.rect.bottom >= bottom - 1:
            #    self.going_down = False
            ### kill it
            if self.posy > screen_height:
                del hearts[0]
            elif self.posy <= 85:
                self.going_down = True       
            if self.going_down:
                self.posy += 2
            else:
                self.posy -= 2
            self.next_update_time = current_time + 10

    def draw(self, surface):
        surface.blit(self.image, (self.posx, self.posy))

class Diamond(object):
    def __init__(self):
        self.image = pygame.image.load("diamond.png").convert()
        self.rect = self.image.get_rect()

    def handle_keys(self):
        key = pygame.key.get_pressed()
        dist = 3
        if key[pygame.K_LEFT]:
            self.rect[0] -= dist
        elif key[pygame.K_RIGHT]:
            self.rect[0] += dist

    def draw(self, surface):
        surface.blit(self.image, (self.rect[0], self.rect[1]+18))

class Pad(object):
    def __init__(self):
        self.image = pygame.image.load("pad.png")
        self.rect = self.image.get_rect()
        self.top = screen.get_height() - self.rect[1]
        self.shoot_y = 0
        self.shoot_x = 0
    def draw(self, surface, posx, posy):
        surface.blit(self.image, (posx, posy))

class DisplayText(object):
    def __init__(self):
        self.myfont = pygame.font.SysFont("Tahoma", 15)
        self.heartsleft = 10 - len(hearts)
        self.label = self.myfont.render("hearts left : %d" %     self.heartsleft, 1, (0,0,0))

    def draw(self, surface):
        surface.blit(self.label, (3, 1))

def main():

    diamond = Diamond()   
    pad = Pad()

    while True:

        white = (255, 255, 255)
        screen.fill(white)
        time = pygame.time.get_ticks()

        x, y = pygame.mouse.get_pos()
        pad.draw(screen, x - pad.rect[2]/2, screen_height - pad.rect[2]/2 )

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == MOUSEBUTTONDOWN:
                diamondy = diamond.rect[1]+18
                diamondx = diamond.rect[0]
                count = "clicks"
                clicks.append(count)
                myvar = len(clicks)
                for i in range(myvar):
                    ii = "heart" + str(i)
                hearts.append(ii)

        for myfigure in hearts:
            myfigure = HeartSprite(diamondx , diamondy)
            myfigure.update(time, 480)
            myfigure.draw(screen)

        diamond.draw(screen)   
        diamond.handle_keys()

        displaytext = DisplayText()
        displaytext.draw(screen)

        pygame.display.update()              
        clock.tick(100)
    pygame.quit()
main()

最佳答案

问题是这个循环:

for myfigure in hearts:
    myfigure = HeartSprite(diamondx , diamondy)
    myfigure.update(time, 480)
    myfigure.draw(screen)

停下来想想这个循环到底做了什么。它为心中的每个元素创建一个带有 diamondxdiamondyHeartSprite 实例;每次运行循环时。

您真正想做的是在单击屏幕后创建 HeartSprite 实例,并将这些实例保留在列表中(以便每颗心保持其状态):

...
        elif event.type == MOUSEBUTTONDOWN:
            diamondy = diamond.rect[1]+18
            diamondx = diamond.rect[0]
            hearts.append(diamondx, diamondy)
...
    for myfigure in hearts:
        myfigure.update(time, 480)
        myfigure.draw(screen)
...
<小时/>

此代码还有一些问题,但这超出了本答案的范围。

关于python - Sprite 没有向下 move ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28202860/

相关文章:

python - 将 distinct on (Postgres) 与 SqlAlchemy 一起使用的正确方法是什么?

python - 即使 Pygame 窗口未处于事件状态,也可以继续使用 Pygame 跟踪鼠标

python - 游戏 : a fast way to find what is on screen before displaying it with insane amount of objects

html - IE8 显示错误的 CSS Sprite

python - 如何在pygame中围绕 Sprite 或图像绘制边框?

python - 根据包含 N (pandas) 的另一列获取一列月份的前 N ​​个月

python - 如何找到玩具哈希函数的冲突?

ios - Swift - SpriteKit - 下一级别进度条

Python 装饰器参数

python-3.x - python3 在 PyCObject_AsVoidPtr 中导入 pygame 结果