Back

Throwable Bouncy Ball

Script description

This script uses the pygame library to create a square that you can drag and throw. When you drag the ball, its path is revealed. The square should bounce off of the walls and floor. The Key "R" should make the game restart.

Result

Example

Source Code

import math
import pygame
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Bouncy Ball")
pygame.font.init()

FPS = 60
run = True

M1 = 10
x1, y1 = WIDTH/2, HEIGHT/2

t0 = 0
t = 0
u0x = 0
u0y = 0
uy = 0
ux = 0

ux0 = 0
uy0 = 0

startposx = x1
startposy = y1
ACCELERATION = 1
tstarted = 0
framessincetstarted = 0

totalFrames = 0
rectangle = pygame.rect.Rect(x1, y1, 30, 30)

def calculatethrow(x0, y0, u0x, u0y,a):
    #print(x0, y0, u0x, u0y,a)
    y = y0 +15
    x = x0 +15
    uy = u0y
    while y= HEIGHT-15 and uy>0:
        uy = -uy/1.3
        y1 = HEIGHT - 15
    if x1 > WIDTH-15 or x1 < 0:
        ux = -ux
    x1 += ux/1.2
    y1 += uy

    renderRectangle(x1, y1)
    pygame.display.update()
    pygame.time.delay(16)



pygame.quit()