Issue
I added classes 'Button' to they do action in my game, but I don't khow how to realize them. What should I do to my program will work correctly without errors?
class Button2(pygame.sprite.Sprite):
def __init__(self, width, height):
#[...]
def draw(self, x, y, message, action = None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x < mouse[0] < x + self.width and y < mouse[1] < y + self.height:
pygame.draw.rect(screen, self.active_color, (x, y, self.width, self.height))
if click[0] == 1:
if action is not None:
action()
else:
pygame.draw.rect(screen, self.inactive_color, (x, y, self.width, self.height))
print_text_5(message, x + 10, y + 10)
def game():
Game = True
while Game:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_SPACE:
player.jump()
elif event.key == K_p:
pause()
elif event_key == K_k:
Game = False
Begining = True
while Begining:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
Begining = False
mini_button = Button2(180, 120)
screen.blit(important_phone_jpg, important_phone_rect)
print_text_5('Welcome to my game!', 300, 50)
mini_button.draw(570, 200, 'PLAY')
mini_button.draw(570, 420, 'QUIT')
pygame.display.update()
Solution
You should make spaces after clock.tick(FPS)
Game = True
while Game:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_SPACE:
player.jump()
elif event.key == K_p:
pause()
elif event_key == K_k:
Game = False
Answered By - Вильдан Латыпов
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.