[英]Why is my sprite stuck in the same position and will not move?
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
當我按下防守按鈕時,球員就會移動。 但是,當我按下按鈕時,它仍然凍結並且無法向任何方向移動。 這是由於游戲 Class 中的 while 循環造成的嗎? 我試圖將 BuildTeam function 放在循環之外,但這也不起作用。 為什么精靈被凍結了?
問題是您在BuildWindow
中調用self.BuildTeams()
。 此方法將播放器重置為其初始 position。 在應用程序循環之前調用self.BuildTeams()
,但不在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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.