d goudeau
d goudeau

Reputation: 31

opygame: trying to bouce a ball, but it gets stuck

import pygame

Red = 255, 0, 0
Black= 0,0,0
rectXpos = 2
rectypos = 2
speed = 2
screenedgex = 500

pygame.init()

window = pygame.display.set_mode(size=(500, 500))

clock = pygame.time.Clock()
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        pygame.display.update()

        window.fill(Black)
        square = pygame.draw.rect(window, Red, [rectXpos, rectypos, 50, 50],2)
        rectXpos += 2

        if rectXpos < 500:
            rectXpos -= 2



        clock.tick(60)
        print(rectXpos)`enter code here`

so what am i doing wrong? i tried making a if statment to stop the ball and reverse it but it keeps the ball at the edge of the window

Upvotes: 3

Views: 255

Answers (3)

Ishwar Ramdasi
Ishwar Ramdasi

Reputation: 195

I assume you want to move rectangle to and fro when mouse is moving.
There are 2 things you are doing wrong here:
1. correct this: if rectXpos > 500: as you have to decrease X when it will reach 500
2. when reach rectXpos 501 it should change its direction till it reach rectXpos 0

but you have decreased position till it is greater than 500 so it will then stuck in between 499 to 501

correct code:

import pygame

Red = 255, 0, 0
Black= 0,0,0
rectXpos = 2
rectypos = 2
speed = 2
screenedgex = 500

pygame.init()

window = pygame.display.set_mode(size=(500, 500))

clock = pygame.time.Clock()
running = True
k=1                                #here is k used to indicate direction
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        pygame.display.update()

        window.fill(Black)
        square = pygame.draw.rect(window, Red, [rectXpos, rectypos, 50, 50],2)
        rectXpos +=  2*k                #here is addition of 2 in given direction

        if (rectXpos > 500) or (rectXpos < 0): #here is condition to change direction
            k=-k



        clock.tick(60)
        print(rectXpos)

Upvotes: 2

furas
furas

Reputation: 142651

You should add speed to position and when you touch border then you should change speed to -speed.

You could also use pygame.Rect() to keep position and size - it has properties .left and .right (and other) which can be very useful. And you can use Rect to draw pygame.draw.rect() (or to check collision with other Rect)

import pygame

# --- constants --- (UPPER_CASE_NAMES)

RED = (255, 0, 0)
BLACK = (0, 0, 0)

WIDTH = 500
HEIGHT = 500

# --- main ---

speed = 10

pygame.init()

window = pygame.display.set_mode((WIDTH, HEIGHT))

item = pygame.Rect(0, 0, 50, 50)

clock = pygame.time.Clock()
running = True
while running:

    # - events -

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # - updates - (without draws)

    item.x += speed

    if item.right >= WIDTH:
        speed = -speed
    if item.left <= 0:
        speed = -speed

    # - draws - (without updates)

    window.fill(BLACK)
    pygame.draw.rect(window, RED, item, 2)
    pygame.display.update()

    clock.tick(60)

# - end -
pygame.quit()    

Upvotes: 0

X3R0
X3R0

Reputation: 6334

This is complete code, I separated the x and y bounces, so you can use either one, also updated the code a bit more, plus some extra formatting.

# Imports
import pygame

# Vars
Red = 255, 0, 0
Black= 0,0,0
rectXpos = 2
rectYpos = 2
rect_width = 50
rect_height = 50
screen_width = 500
screen_height = 500
block_x_direction = 1
block_y_direction = 1

# Setup Code
pygame.init()
window = pygame.display.set_mode(size=(screen_width, screen_height))
clock = pygame.time.Clock()
running = True

# Game Loop
########################################################
while running:
    # Event Loop
    ########################################################
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False


    # Game Code - Update
    ########################################################
    # Game Code - Update - Rect X Bounce
    if rectXpos + (rect_width)>= screen_width:
        block_x_direction = block_x_direction * -1
    rectXpos += 2 * block_x_direction
    # Game Code - Update - Rect Y Bounce
    if rectYpos + (rect_height)>= screen_height:
        block_y_direction = block_y_direction * -1
    rectYpos += 2 * block_y_direction

    # - Tick Game    
    clock.tick(60)

    # Game Code - Render
    ########################################################
    window.fill(Black)
    square = pygame.draw.rect(window, Red, [rectXpos, rectYpos, rect_width, rect_height],2)
    pygame.display.update()

    # Game Code - Debug Code
    ########################################################
    print(clock.tick)

Upvotes: 2

Related Questions