import pygame
class Player(pygame.sprite.Sprite):
def __init__(self, Team, Position):
super().__init__()
self.window = pygame.display.set_mode((1280, 800))
self.red = (255, 0, 0)
self.blue = (0, 0, 255)
self.PlayerHolder = []
self.Team = Team
self.Position = Position
self.CurrentPosition = ""
# Player
self.image = pygame.Surface([20, 20])
self.rect = self.image.get_rect()
def DrawPlayerShape(self, co_ordinate, colouropt):
self.CurrentPosition = co_ordinate
# Player's shape as a sprite
self.image.fill(colour)
self.rect.center = co_ordinate
def MovePlayerForward(self, x, y):
speed = self.Pace
curr_x, curr_y = self.CurrentPosition
curr_x = x
curr_y = y
st1.DrawPlayerShape((curr_x, curr_y), 'R')
class Button:
def __init__(self, Title, x, y):
self.window = pygame.display.set_mode((1280, 800))
self.Title = Title
self.Colour = (255, 255, 255)
self.x = x
self.y = y
self.Shape = pygame.Rect((self.x, self.y), (200, 100))
def Draw(self):
pygame.draw.rect(self.window, self.Colour, self.Shape)
text_type = pygame.font.SysFont('arialunicode', 18).render(self.Title, True, (
0, 0, 0))
self.window.blit(text_type, self.Shape)
# Checks if the buttons have been pressed to begin their function
self.ClickCheck()
def ClickCheck(self):
mouse_pos = pygame.mouse.get_pos()
if self.Shape.collidepoint(mouse_pos):
if pygame.mouse.get_pressed()[0]:
self.Colour = (80, 80, 80)
if self.Title == 'Attacking':
self.Attacking()
elif self.Title == 'Defending':
self.Defending()
elif self.Title == 'Balanced':
self.Balanced()
else:
self.Colour = (255, 255, 255)
def Attacking(self):
pass
def Defending(self):
plyer.st1.MovePlayerForward(0, 10)
def Balanced(self):
pass
class Game:
def __init__(self):
self.width = 1280
self.height = 800
self.window = pygame.display.set_mode((1280, 800))
pygame.display.set_caption("Football Simulator")
self.clock = pygame.time.Clock()
def BuildWindow(self):
# Builds the GUI
self.window.fill((50, 50, 50))
attacking_button.Draw()
defensive_button.Draw()
balance_button.Draw()
self.BuildTeams()
def BuildTeams(self):
st1.DrawPlayerShape((630, 375), self.colour1)
def Running(self):
# Allows the window to stay open
running = True
while running:
for events in pygame.event.get():
if events.type == pygame.QUIT:
running = False
pygame.quit()
self.BuildWindow()
plyer.Players.draw(self.window)
# Updating the window continuously
pygame.display.flip()
st = Player('Arsenal', 'ST')
attacking_button = Button_tactics.Button('Attacking', 0, 100)
defensive_button = Button_tactics.Button('Defending', 0, 500)
balance_button = Button_tactics.Button('Balanced', 0, 300)
Players = pygame.sprite.Group()
Players.add(st1)
if __name__ == '__main__':
game = Game()
game.Running()
else:
pass
When I press the defensive button, the player is meant to move. However, when I press the button, it remains frozen and is unable to move in any direction. Is this due to the while loop in the Game Class. I have tried to put the BuildTeam function outside of the loop, however this does not work either. Why is the sprite frozen?
CodePudding user response:
The problem is that you call self.BuildTeams()
in BuildWindow
. This method resets the player to its initial position. Call self.BuildTeams()
before the application loop, but not in BuildWindow
:
class Game:
# [...]
def BuildWindow(self):
# Builds the GUI
self.window.fill((50, 50, 50))
attacking_button.Draw()
defensive_button.Draw()
balance_button.Draw()
#self.BuildTeams() <--- DELETE
# [...]
def Running(self):
self.BuildTeams() # <--- INSERT
# Allows the window to stay open
running = True
while running:
for events in pygame.event.get():
if events.type == pygame.QUIT:
running = False
pygame.quit()
self.BuildWindow()
plyer.Players.draw(self.window)
# Updating the window continuously
pygame.display.flip()