Why does move() run moves() although I still haven't clicked anything?

23 views Asked by At

I'm trying to programm the board game quoridor, but in my "move()" function, which is supposed to move the player, it runs another function moves() that should only run after the mouse position is within the player's hitbox, but it runs moves() with the mouse still not getting an input, so at (0,0). Why? To add on, the moves() function is supposed to draw smaller circles, representing the available moves, but they appear without any mouseclicks AND the players aren't drawn anymore. I'm definitely not finished yet with the whole project, but I first want to solve this problem. I'd appreciate any help The initialisation:

import pygame

pygame.display.set_caption("Quorridor")
x = 440
y = 440
background = pygame.display.set_mode((x, y))
pygame.init()

def board():
    background.fill((13,152,186))
    for x in range(0,450,50):
      pygame.draw.rect(background,(0, 71, 171),(x,0,40,40))
      pygame.draw.rect(background,(20, 52, 164),(x,50,40,40))
      pygame.draw.rect(background,(0, 71, 171),(x,100,40,40))
      pygame.draw.rect(background,(20, 52, 164),(x,150,40,40))
      pygame.draw.rect(background,(0, 71, 171),(x,200,40,40))
      pygame.draw.rect(background,(20, 52, 164),(x,250,40,40))
      pygame.draw.rect(background,(0, 71, 171),(x,300,40,40))
      pygame.draw.rect(background,(20, 52, 164),(x,350,40,40))
      pygame.draw.rect(background,(0, 71, 171),(x,400,40,40))
      

class player(object):
  def __init__(self,x,y,turn):
    self.walls = 10
    self.x=x
    self.y=y
    self.Pos=(x,y)
    self.moveUp = False
    self.moveRight = True
    self.moveLeft = True
    self.moveDown = False
    self.turn=turn


  def moves(self): # shows possible moves
    if self.moveUp:
      pygame.draw.circle(background,(170, 74, 68),(self.x,self.y-50),5)
    if self.moveDown:
      pygame.draw.circle(background,(170, 74, 68),(self.x,self.y+50),5)
    if self.moveLeft:
      pygame.draw.circle(background,(170, 74, 68),(self.x-50,self.y),5)
    if self.moveRight:
      pygame.draw.circle(background,(170, 74, 68),(self.x+50,self.y),5)
    pygame.display.flip()


  def move(self): # moves the player
    for event in pygame.event.get():
      if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
        x, y = pygame.mouse.get_pos()
        print(x,y)
        if self.x-x<= 20 and self.x-x >= -20:
          if self.y-y<= 20 and self.y-y >= -20:
            print("True")
            self.moves()
            if self.x-x<= 20 and self.x-x >= -20: # move down
              if y-self.y <=30 and y-self.y <=70:
                print("DOWN")
                board() 
                self.draw_p(self.x,self.y+50)
                self.turn = False
            elif self.x-x<= 20 and self.x-x >= -20: # move up
              if self.y-y <=30 and self.y-y <=70:
                print("UP")
                board()
                self.draw_p(self.x,self.y-50)
                self.turn = False
            elif self.y-y <= 20 and self.y-y >= -20: # move left
              if self.x-x >= 30 and self.x-x <= 70:
                print("LEFT")
                board()
                self.draw_p(self.x,self.y-50)
                self.turn = False
            elif self.y-y <= 20 and self.y-y >= -20: # move right
              if x-self.x >= 30 and x-self.x <= 70:
                print("RIGHT")
                board()
                self.draw_p(self.x,self.y-50)
                self.turn = False
  
  def draw_p(self,x,y): # draws player
    pygame.draw.circle(background,(220,0,0),(x,y),10)

My next file, supposed to run the game

player1=cor.player(220,20,True)
player2=cor.player(220,420,False)
cor.board()
player1.moves()

for i in range(100):
  while player1.turn:
    cor.board()
    player1.draw_p(player1.x,player1.y)
    player2.draw_p(player2.x,player2.y)
    player1.move()
    player2.turn=True

  while player2.turn:
    cor.board()
    player1.draw_p(player1.x,player1.y)
    player2.draw_p(player2.x,player2.y)
    player2.move()
    player1.turn=True

I tried deleting the 2nd while loop, only focusing on the problem in the first while loop for player 1, but somehow it kept on going. Tried changing some display flips around, but I just can't catch the problem. If it helps, I could post my replit link on here as well.

0

There are 0 answers