我不知道如何通过鼠标点击创建新的精灵,并为所有新精灵添加动作。
我创造了一个简单的游戏:
K_LEFT
,K_RIGHT
如果你单击一个鼠标,你会根据鼠标点击次数追加心脏并绘制精灵heart0,heart1等。
如果你点击鼠标,你会画出一颗精灵心脏,它会落在钻石所在的位置,心脏会向下移动......
问题:心脏没有向下移动。我无法画出多颗心。 此刻,我只能在一个钻石的地方画一颗心,并附加心灵。您知道我的代码有什么问题吗,所以我无法获得所需的功能吗?
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()
答案 0 :(得分:1)
问题在于这个循环:
for myfigure in hearts:
myfigure = HeartSprite(diamondx , diamondy)
myfigure.update(time, 480)
myfigure.draw(screen)
停下片刻,想一想这个循环到底是做什么的。它为心中的每个元素创建HeartSprite
的新实例,其中diamondx
和diamondy
;每次循环运行。
您真正想要做的是在点击屏幕后创建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)
...
此代码还存在一些问题,但这超出了此答案的范围。