#python #python-3.x #pygame
#python #python-3.x #pygame
Вопрос:
Код для запуска простой игры. Все, что он делает, это перемещает круг, просто чтобы проверить fps, но когда вы запускаете его, для его запуска требуется некоторое время, а затем заикается
import pygame
pygame.init()
screen = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
playerX = 250 # Player X
playerY = 250 # Player Y
playerColor = (10, 10, 100)
class Player:
def __init__(self, x, y, color):
self.x = x
self.y = y
self.color = color
def show(self):
pygame.draw.circle(screen, self.color, (self.x, self.y), 10)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
playerX = 5
player = Player(playerX, playerY, playerColor)
screen.fill((60, 30, 10))
player.show()
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
Ответ №1:
Это вопрос Отступа. Вам нужно обновить позицию проигрывателя и нарисовать сцену в цикле приложения, а не в цикле событий.
Создайте экземпляр Player
перед циклом приложения:
import pygame
pygame.init()
screen = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
class Player:
def __init__(self, x, y, color):
self.x = x
self.y = y
self.color = color
def show(self):
pygame.draw.circle(screen, self.color, (self.x, self.y), 10)
playerX = 250 # Player X
playerY = 250 # Player Y
playerColor = (10, 10, 100)
player = Player(playerX, playerY, playerColor)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#<--| INDENTATION
player.x = 5
screen.fill((60, 30, 10))
player.show()
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
Комментарии:
1. Спасибо, я уже некоторое время борюсь с этим.