1

I'm currently making a rhythm game in Pygame and I have been trying to output "notes", but in my sprite class they are called enemies for a separate reason, based on the current time being the same(ish) to floats in a text file. However when I try to use the loop whether it be an infinite for loop or a while loop it cause the window to freeze whilst it's activated. I have tried pygame.event.pump() but that just makes it so that after the loop has ended it is no longer frozen.

Anything commented is what I have tried already.

import pygame
from sys import exit


class Enemies(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.image.load(r'\slime.png').convert_alpha()
        self.image = pygame.transform.rotozoom(self.image,0,4)
        self.rect = self.image.get_rect(center = (1040,-50))

    # def d_notes(self):
    #     keys = pygame.key.get_pressed()
    #     if keys[pygame.K_SPACE]:
    #         maps = beat_read()
    #         temp = list(maps)
    #         note = 0
    #         p_note = 0
    #         for x in iter(int, 1):
    #             pygame.event.pump()
    #             pygame.event.pump()
    #             if p_note == len(maps):
    #                 break
    #             current_time = (pygame.time.get_ticks() / 1000) - start_time
    #             x_lower = maps[note] - 0.02
    #             x_upper = maps[note] + 0.02
    #             if x_lower <= current_time <= x_upper:
    #                 out = temp.pop(0)
    #                 print(out)
    #                 note = note + 1
    #                 p_note = p_note + 1




    def update(self):
        self.rect.y +=5
        #self.d_notes()

def beat_read():
    with open(r'\simple.txt') as f:
        ll = []
        for x in f:
            x = float(x)
            ll.append(x)
        ll = tuple(ll)
        return ll


# def d_notes():
#     maps = beat_read()
#     temp = list(maps)
#     note = 0
#     p_note = 0
#     for x in iter(int,1):
#         pygame.event.pump()
#         pygame.event.pump()
#         if p_note == len(maps):
#             break
#         current_time = (pygame.time.get_ticks() / 1000) - start_time
#         x_lower = maps[note] - 0.02
#         x_upper = maps[note] + 0.02
#         if x_lower <= current_time <= x_upper:
#             out = temp.pop(0)
#             print(out)
#             note = note + 1
#             p_note = p_note + 1
#             print(note)
#             print(maps)
#             print(temp)




pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((1366,850))
pygame.display.set_caption('Rhythmic')

game_active = False
enemies_group = pygame.sprite.Group()
enemies_group.add(Enemies())

background = pygame.image.load(r'\background.png').convert()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()

        if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
            game_active = True
            start_time = pygame.time.get_ticks() / 1000

            # maps = beat_read()
            # temp = list(maps)
            # note = 0
            # p_note = 0
            # for x in iter(int, 1):
            #     pygame.event.pump()
            #     pygame.event.pump()
            #     if p_note == len(maps):
            #         break
            #     current_time = (pygame.time.get_ticks() / 1000) - start_time
            #     x_lower = maps[note] - 0.02
            #     x_upper = maps[note] + 0.02
            #     if x_lower <= current_time <= x_upper:
            #         out = temp.pop(0)
            #         print(out)
            #         note = note + 1
            #         p_note = p_note + 1


    if game_active:
        # d_notes()

    screen.blit(background,(0,0))

    enemies_group.draw(screen)
    enemies_group.update()

    pygame.display.flip()
    clock.tick(60)
Squire
  • 11
  • 1

1 Answers1

2

[Aside: User input handling does not belong in a Sprite class. ]

The for x in iter(int, 1): is an infinite loop, is this your intention?

So the code is:

   If space was pressed:
      Loop forever, doing:
          touch the event handler
          if the timing calculation is correct:
              advance to next beat
              print some stuff
              check if we should exit the loop

I would say your timing-calculation check is never-ok.

Assuming you're writing some kind of follow-the-beat game, don't you need to repeatedly check for the time of the key-press, then check against the beat-time?

Also you need to advance the counters when the key-press has incorrect timing. I think this is the #1 reason it's getting stuck in the loop.

Probably you could (with a pencil and paper), re-work your control loop.

Load some beat-files.

Main Loop:

    For every event:
        Does the event say [space] was pressed?
            Make a note of the press-timing

    Do we have any beats left?:
        Is it time for the next beat:
            Advance to the next beat
    else:
        Show Game Over
        Use the next beat-file, or whatever

    Update any screen elements
    Paint the screen
    
    Was [space] pressed and is the timing accurate:
         Increase player score (or whatever)
Kingsley
  • 14,398
  • 5
  • 31
  • 53