-1

Am trying to get neural network (NEAT) to learn and control a game,so far the sprite is controlled using keyboard.

I tried pyautogui ,it did not work.

pygame.event.post on the other hand can click letters if passed from within Game class, for instance, I set m is for music within the Game class, which it dealt with successfully but not LEFT or RIGHT, because they are from the Player class. Lastly,I tried some inheritances but that gave me a never ending iteration/loading.

I understand the pygame class is quite complicated, I would like help of any sort on how I can solve the problem.

Here is a sample of my atempt to solve the problem.

class Game(pygame.sprite.Sprite):
def __init__(self, scrn_width,scrn_height,score,..):
    super().__init__()
    'reponsible for game enviroment'
    'inits screen,inits group sprite, keeps increments score'
    self.player = Player()
    self.enemy = Enemy()
    self.player_group.add(self.player)
    self.enemy_group.add(self.enemy)
    self.score...
    
def run(self):
    pass
    
class Player(pygame.sprite.Sprite):
    def __init__(self, path = r'..\player.png'):
        super().__init__(self)
        ..
        self.image = pygame.image.load(path)
        self.rect.center = [0,0]
        
    def move(self):
        'allows'


class Controller(Player,Game):
def __init__(self):
    super().__init__()
    
def begin(self):
    Start_Game._run(self)        

    if self.score == 1:
        pyautogui.press['Left','Left','Left'] #try 1

        self.rect.top = 0 #try 2

        newevent = pygame.event.Event(pygame.locals.KEYDOWN, key=pygame.locals.K_LEFT)  #try 3
        pygame.event.post(newevent)

""" 
I would like this class to pass coordinates to update player sprite's position
based on information from Game such as score,collision,enemy.
"""

    
Quad
  • 11
  • 3
  • 1
    why do You need to press keys in the first place? can't You just define functions that will move the player and make the AI call them? like: `move_left(): x += 5` and then there is no need to simulate a keypress – Matiiss Apr 27 '21 at 13:52
  • I want to leave that as the last option because adding a sprite in a group allows simplicity in functionality such as collision detection, but am beginning to think that is the way to go – Quad Apr 27 '21 at 13:59
  • 1
    all You need to do is instead of simulating a keypress call a function that will do the exact same thing, except You don't need to simulate anything, it literally does not change anything except makes it a bit simpler – Matiiss Apr 27 '21 at 14:01
  • Just so you know, changing the sprites movement within the game class makes the enemy (which is added a group)disappear, (I think )it means I no longer have privaleges that come in groups such as collision detection because I would have to blit the image on screen directly. – Quad Apr 27 '21 at 15:49
  • I will just do everything manually as you have suggested. – Quad Apr 27 '21 at 15:50

0 Answers0