Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Notice: New to this site, not so new to StackOverflow though, assuming this is pretty much the same.

Anyhow, I am working on a game (code at end) and I want to make a counter for the number of balls left in the bottom left. is there an easy way to do so?

i've tried to just print the variable that i assigned the counter's information to, but that printed to console. Also, i tried to make a pattern of rectangles that looked like the numbers, but that was too slow.

.

.

my current game code (without counter or blocks yet)

import pygame

# Constants
WIDTH = 700
HEIGHT = 500
SCREEN_AREA = pygame.Rect(0, 0, WIDTH, HEIGHT)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

# Initialization
pygame.init()
screen = pygame.display.set_mode([WIDTH, HEIGHT])
pygame.mouse.set_visible(0)
pygame.display.set_caption("Breakout Recreation WIP")
clock = pygame.time.Clock()

# Variables
paddle = pygame.Rect(350, 480, 50, 10)
ball = pygame.Rect(10, 250, 15, 15)
paddle_movement_x = 0
ball_direction = (1, 1)
balls = 3
done = False

while not done and balls > 0:

    # Process events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            paddle_movement_x = -2
        elif keys[pygame.K_RIGHT]:
            paddle_movement_x = 2
        else:
            paddle_movement_x = 0

    # Move paddle
    paddle.move_ip(paddle_movement_x, 0)
    paddle.clamp_ip(SCREEN_AREA)

    # Move ball
    ball.move_ip(*ball_direction)
    if ball.right > WIDTH or ball.left < 0:
        ball_direction = -ball_direction[0], ball_direction[1]
    elif ball.top < 0 or paddle.colliderect(ball):
        ball_direction = ball_direction[0], -ball_direction[1]
    elif ball.bottom > HEIGHT:
        balls = balls - 1
        ball_direction = (1, 1)
        ball = pygame.Rect(10, 250, 15, 15)

    ball.clamp_ip(SCREEN_AREA)

    # Redraw screen
    screen.fill(BLACK)
    pygame.draw.rect(screen, WHITE, paddle)
    pygame.draw.rect(screen, WHITE, ball)
    pygame.display.flip()
    clock.tick(100)

pygame.quit()
share|improve this question
1  
You asked a question if something was possible then included code, are we to assume you want it written for you? – Byte56 Apr 27 at 4:54
not necessarily, but if you want to, go ahead. What I was looking for was an example in code format, and an explanation if possible – PythonInProgress Apr 27 at 22:27
Nope, I wasn't asking if I should. I suggest you remove the code, as it makes the question too localized. You should replace it with a description of what you've tried and what about it didn't work. – Byte56 Apr 28 at 0:57

closed as too localized by msell, Anko, Byte56, bummzack, Trevor Powell Apr 30 at 13:00

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

Browse other questions tagged or ask your own question.