1

I'm relatively new to Python, but something that came to my mind the other day was that I wanted to implement a version of The Brain Puzzle by Magnif No. 2225, which is demonstrated here:

https://www.youtube.com/watch?v=o6V1UlEzdD8&t=160s

Here's the thing, is I used to own one of these, and it broke. It was something I really enjoyed, and decided I wanted to implement it in software. I read that pygame could help draw things, so I started with that library and a small tutorial... Long story short, I'm able to get the "starting" board state, but it seems really slow to update.

Without further ramble, here's what I have -- I haven't written the elif block yet, but it's essentially the same, just in the "other" direction:

import pygame
import pygame.freetype
import numpy as np
import time

class Particle:
    def __init__(self,position,size,thickness,color):
        self.x=position[0]
        self.y=position[1]
        self.size = size
        self.rred = color[0]
        self.bblue = color[1]
        self.ggreen = color[2]
        self.thickness = thickness
    def display(self):
        pygame.draw.circle(screen, (self.rred,self.bblue,self.ggreen), (self.x, self.y), self.size, self.thickness)


# sets a window size
(width, height) = (800, 600)
screen = pygame.display.set_mode((width, height))
# Sets a color for background
background_colour = (255,255,255)
# Sets window title
pygame.display.set_caption('The Brain Puzzler')
# Makes background white
screen.fill(background_colour)
#draws a black circle on the screen at the specified location
myParticles = []
mainBoard = Particle((400,300),225,5,(0,0,0))
myParticles.append(mainBoard)

#185 for tab out
for circ in range(0,8):
    gameTab = Particle((400+70*np.cos(circ*np.pi/4),300+70*np.sin(circ*np.pi/4)),10,3,(0,0,0))  
    myParticles.append(gameTab)
    for part in myParticles:
        part.display()
    #displays screen
pygame.display.flip()


running = True
#while running:
  #for event in pygame.event.get():
    #if event.type == pygame.QUIT:
      #running = False


#initialize binary variables
#each position represents the state of each slider
binaryy = '11111111'

#initialize solution
sol = '12131214121312151213121412131216121312141213121512131214121312171213121412131215121312141213121612131214121312151213121412131218121312141213121512131214121312161213121412'
#initialize user path
x=''
#initialize iter variable for solution path
y=0
while  x != sol:
    inputf = int(input('Input the slider that you\'d like to actuate?: '))
    if str(inputf) == sol[y]:
        y=y+1
        x+=str(inputf)
        print("Current Path:", x)
        if binaryy[inputf-1]=='1':
            binaryy = binaryy[:inputf-1]+'2'+binaryy[inputf+0:]
            print("Staus:", binaryy)
            #update game status
            gameTab = Particle((400+70*np.cos((inputf-1)*np.pi/4),300+70*np.sin((inputf-1)*np.pi/4)),10,3,(0,0,0))  
            myParticles.append(gameTab)
            gameTab = Particle((400+185*np.cos((inputf-1)*np.pi/4),300+185*np.sin((inputf-1)*np.pi/4)),10,3,(255,255,255)) 
            myParticles.append(gameTab)
            for part in myParticles:
                part.display()
            pygame.display.update()
            
        else:
            binaryy = binaryy[:inputf-1]+'1'+binaryy[inputf+0:]
            print("Status:", binaryy)
            gameTab = Particle((400+70*np.cos((inputf-1)*np.pi/4),300+70*np.sin((inputf-1)*np.pi/4)),10,3,(255,255,255))  
            myParticles.append(gameTab)
            gameTab = Particle((400+185*np.cos((inputf-1)*np.pi/4),300+185*np.sin((inputf-1)*np.pi/4)),10,3,(0,0,0)) 
            myParticles.append(gameTab)
            for part in myParticles:
                part.display()
            pygame.display.update()
    elif inputf == sol[y-1]:
        y=y-1
        x= x[:-1]
        print(x)
     
        
    else:
        print("try again")
        
        


Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Andy Chao
  • 31
  • 5
  • 1
    I can't watch the video demo right now, but you code is very hard to read. Try to separate update() and draw() in two different functions. From what I see you are creating two new instances of your Particule object every frame. Since you didn't cap fps you might be creating up to 2000 objects every seconds so no doubt it is laggy. – Anto Sep 02 '22 at 07:36
  • Hey @Anto heard about the code being hard to read. In my while loop, I added `myParticles = []` to clear that list every loop. When I initialize the drawing, I removed: `for part in myParticles: part.display()` from the previous for loop, so it only draws one of each Particle. I also added `clock.tick(30)`. Still having the same issues! – Andy Chao Sep 02 '22 at 13:51
  • Removed it from the for loop, pasted it outside the loop – Andy Chao Sep 02 '22 at 14:01
  • 1
    I don't quite get why you would create objects, delete them and create new ones every frame. Seems like you are creating new object rather than updating the ones you already have. A real world comparison would be : Consider you have an decoration object on a shelf. Let's say you want to move this object a bit. Rather than just pushing it, you would buy a new one, place it at the new position and throw your old one in the trash. – Anto Sep 05 '22 at 07:50
  • That's a good call. I'm still not certain if the creation/destruction/creation cycle is what causes the game to freeze after the first input... – Andy Chao Sep 06 '22 at 04:47
  • I'm unsure tbh but what is certain is that you should work towards simplifying the code. If I find some time I may watch the video you linked and try to create it myself to see if I can make it without it being laggy. Is the principle so complexe that you cannot explain what you are trying to achive with your own words ? Can you describe what you want to end result to look like exactly or is it too hard to explain with words ? – Anto Sep 06 '22 at 07:04
  • Sure, I can explain it. – Andy Chao Sep 06 '22 at 16:47
  • The game in real life is a cylinder starting with #ed pegs 1-8 at the inner circle of the board. The aim is to move the pegs in order to get them all on an outer circle. There is a specific sequence that must be followed. The correct peg slides freely, and there is only one solution. You can return to the start by going the opposite direction in the sequence. What I want in Python: the player inputs the peg # they wish to move. If the peg can be moved, then the drawing should update accordingly (that is, the peg moves to the outer circle or inner circle as determined by the sequence). – Andy Chao Sep 06 '22 at 16:59

0 Answers0