我正在做节奏游戏。 当“拍子”(箭头)击中某个坐标时,它消失。 如果按下了相应的键,则游戏会增加玩家的分数。
但是当我运行我的代码时,它并没有增加得分。
这是我的代码:
import pygame
import time
import itertools
pygame.init()
SCREENWIDTH = 1000
SCREENHEIGHT = 650
screen = pygame.display.set_mode([SCREENWIDTH, SCREENHEIGHT])
screen.fill((255, 123, 67))
pygame.draw.rect(screen, (0, 255, 188), (0, 50, 1000, 650), 0)
myfont = pygame.font.SysFont('Ink Free', 30)
background = screen.copy()
clock = pygame.time.Clock()
stageon = True
sprites = pygame.sprite.Group()
class Player(pygame.sprite.Sprite):
sprite = pygame.image.load("Sprites/lee.png")
def __init__(self, *groups):
super().__init__(*groups)
self.image = Player.sprite
self.rect = self.image.get_rect(topleft=(445, 550))
self.pos = pygame.Vector2(self.rect.topleft)
self.score = 0
def update(self):
key = pygame.key.get_pressed()
dist = 3
if key[pygame.K_DOWN]:
self.rect.y += dist
elif key[pygame.K_UP]:
self.rect.y -= dist
if key[pygame.K_RIGHT]:
self.rect.x += dist
elif key[pygame.K_LEFT]:
self.rect.x -= dist
player = Player(sprites)
beatgroup = pygame.sprite.Group()
class Beat(pygame.sprite.Sprite):
def __init__(self, ticks, image):
super().__init__(beatgroup)
self.image = image
self.rect = self.image.get_rect()
self.pos = pygame.Vector2(730, 100)
self.path = itertools.cycle(((730, 100), (850, 100),))
self.next_point = pygame.Vector2(next(self.path))
self.speed = 2
self.ticks = 200
def update(self):
move = self.next_point - self.pos
move_length = move.length()
if move_length != 0:
move.normalize_ip()
move = move * self.speed
self.pos += move
key = pygame.key.get_pressed()
if self.pos == (850, 100):
self.kill()
#here's the problem area
if self.pos == (850, 100) and key[pygame.K_DOWN] and self.image == pygame.image.load("Sprites/down.png"):
player.score += 10
elif self.pos == (850, 100) and key[pygame.K_UP] and self.image == pygame.image.load("Sprites/up.png"):
player.score += 10
elif self.pos == (850, 100) and key[pygame.K_LEFT] and self.image == pygame.image.load("Sprites/left.png"):
player.score += 10
elif self.pos == (850, 100) and key[pygame.K_RIGHT] and self.image == pygame.image.load("Sprites/right.png"):
player.score += 10
if move.length() == 0 or move_length < self.speed:
self.next_point = pygame.Vector2(next(self.path))
self.rect.topleft = self.pos
class Beat_gen(pygame.sprite.Sprite):
def __init__(self, order):
super().__init__(beatgroup)
self.image = pygame.image.load("Sprites/beat_cropped.png")
self.rect = self.image.get_rect(topleft=(730, 100))
self.start_time = pygame.time.get_ticks()
print(self.start_time)
self.order = []
self.picorder = []
for i in order:
self.order.append(i[0])
self.picorder.append(i[1])
self.currentbeat = 0
self.LastBeat = 0
def update(self):
if self.currentbeat == len(self.order):
stageon = False
else:
time_gone = pygame.time.get_ticks() - self.start_time
if time_gone >= self.order[self.currentbeat] or self.currentbeat == 0:
self.start_time = pygame.time.get_ticks()
Beat(self.order[self.currentbeat], self.picorder[self.currentbeat])
self.currentbeat += 1
self.LastBeat = pygame.time.get_ticks()
class Hit_Line(pygame.sprite.Sprite):
def __init__(self):
super().__init__(beatgroup)
self.image = pygame.image.load("Sprites/hit-line.png")
self.rect = self.image.get_rect(topleft=(873, 60))
def update(self):
self.image.draw()
beatgen = Beat_gen([(820, pygame.image.load("Sprites/left.png")), (410, pygame.image.load("Sprites/right.png")),(410, pygame.image.load("Sprites/left.png")),
(410, pygame.image.load("Sprites/right.png")),(410, pygame.image.load("Sprites/left.png")), (410, pygame.image.load("Sprites/right.png")),
(410, pygame.image.load("Sprites/left.png")), (410, pygame.image.load("Sprites/right.png")),(410, pygame.image.load("Sprites/left.png")),
(410, pygame.image.load("Sprites/right.png")),(410, pygame.image.load("Sprites/left.png")), (410, pygame.image.load("Sprites/right.png")),
(410, pygame.image.load("Sprites/left.png")), (410, pygame.image.load("Sprites/right.png")),(410, pygame.image.load("Sprites/left.png")),
(410, pygame.image.load("Sprites/right.png"))])
def main():
while stageon:
for events in pygame.event.get():
if events.type == pygame.QUIT:
pygame.quit()
return
sprites.update()
beatgroup.update()
screen.blit(background, (0, 0))
sprites.draw(screen)
beatgroup.draw(screen)
pygame.display.update()
clock.tick(100)
if __name__ == '__main__':
main()
另外,我测试分数是否上升的一个注释是,在我的原始代码中,分数显示在屏幕上。我也将其打印到控制台。 为了证明这不是我不良的节奏游戏技能,我按住了一段时间(现在只是左右左右)。
我已经多次遍历我的代码以尝试发现问题,但找不到。
任何解释将不胜感激。
谢谢:)
答案 0 :(得分:0)
这是我的未来!我如何解决的是在Beat
类中添加了另一个名为direction
的属性,该属性是一个包含“ left”,“ right”,“ up”或“ down”的字符串。>
要分配此值,我去了Beat_gen
初始化的部分,并在每个元组中添加了另一个值,如下所示:
(820, pygame.image.load("Sprites/left.png"), "left")
。然后,我进入Beat_gen
类,添加了一个名为dirorder
的列表(方向顺序),并循环遍历每个元组,获取第三个值并将其附加到列表中,就像对图片和时间。
然后将其添加到Beat
初始化中作为分配它的参数; self.dirorder[self.currentbeat]