我不知道如何创建新的精灵鼠标点击和添加一个移动到所有新的精灵。你知道吗
我创建了一个简单的游戏:
K_LEFT
,K_RIGHT
向左/向右移动如果你点击一个鼠标,你会附加一个心形,并根据鼠标点击的次数绘制一个精灵心形0,心形1,等等。你知道吗
如果你点击一个鼠标,你会画一颗精灵的心,它会落在钻石所在的地方,心会向下移动。。。你知道吗
问题:心脏没有向下移动。我画不出多颗心。 目前,我只能画一个心脏在一个地方的钻石和附加的心。你知道吗
你知道我的代码有什么问题,所以我不能得到所需的功能请?你知道吗
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()
问题是这个循环:
停下来想一想这个循环到底做了什么。每次循环运行时,它都会为hearts中的每个元素创建一个带有
diamondx
和diamondy
的HeartSprite
的新实例。你知道吗您真正想做的是在单击屏幕后创建
HeartSprite
实例,并将这些实例保留在列表中(这样每个心脏都会保持其状态):这段代码还有一些问题,但这超出了这个答案的范围。你知道吗
相关问题 更多 >
编程相关推荐