M
M
Misha Shandra2017-11-20 22:48:11
Python
Misha Shandra, 2017-11-20 22:48:11

How to stop a loop in pygame, continue running when SPACE is pressed, and exit it when it fails?

I don't know how to make the game stop and start when SPACE is pressed.

import pygame

size = (700, 500)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
RED = (255, 0, 0)

pygame.init()

screen = pygame.display.set_mode(size)
pygame.display.set_caption("Probnichok")

game_over = False
clock = pygame.time.Clock()

x_pos = 350
y_pos = 250
x_p = 5
y_p = 5
igr_1x = 0
igr_1y = 50
igr_2x = 695
igr_2y = 400
move_up = False
move_down = False
move_w = False
move_s = False
font = pygame.font.Font(None, 25)
x = 0
y = 0

while not game_over:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                move_up = True
            if event.key == pygame.K_DOWN:
                move_down = True
            if event.key == pygame.K_w:
                move_w = True
            if event.key == pygame.K_s:
                move_s = True

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                move_up = False
            if event.key == pygame.K_DOWN:
                move_down = False
            if event.key == pygame.K_w:
                move_w = False
            if event.key == pygame.K_s:
                move_s = False

    screen.fill(WHITE)

    pygame.draw.line(screen, BLACK, (350, 0), (350, 500), 1)
    sharik = pygame.draw.circle(screen, RED, (x_pos, y_pos), 7)
    igrok_1 = pygame.draw.rect(screen, BLACK, (igr_1x, igr_1y, 5, 50))
    igrok_2 = pygame.draw.rect(screen, BLACK, (igr_2x, igr_2y, 5, 50))

    x_pos += x_p
    y_pos += y_p

    if y_pos > 500 or y_pos < 0:
        y_p *= -1
    if x_pos == igr_1x + 5 and y_pos > igr_1y and y_pos < igr_1y + 50:
        x_p *= -1
    if x_pos == igr_2x and y_pos > igr_2y and y_pos < igr_2y + 50:
        x_p *= -1

    if move_down == True:
        igr_2y += 8
    if move_up == True:
        igr_2y -= 8

    if move_w == True:
        igr_1y -= 8
    if move_s == True:
        igr_1y += 8

    if igr_1y < 0:
        igr_1y = 0
    if igr_1y > 450:
        igr_1y = 450

    if igr_2y < 0:
        igr_2y = 0
    if igr_2y > 450:
        igr_2y = 450

    text_x = font.render(str(x), True, BLACK)
    text_y = font.render(str(y), True, BLACK)
    text = font.render("Game Over!", True, BLACK)

    screen.blit(text_x, (300, 0))
    screen.blit(text_y, (400, 0))

    if x_pos < 0:
        x += 1
        screen.blit(text_x, (300, 0))
        x_pos = 450
        y_pos = 250
    if x_pos > 700:
        y += 1
        screen.blit(text_y, (400, 0))
        x_pos = 450
        y_pos = 250

    if x > 9 or y > 3:
        screen.blit(text, (300, 200))

    pygame.display.flip()

    clock.tick(60)

pygame.quit()

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question