Skip to content

Instantly share code, notes, and snippets.

@jamesxu123
Created March 7, 2019 22:00
Show Gist options
  • Save jamesxu123/c91e7564e4839169c66c7fe8d200be23 to your computer and use it in GitHub Desktop.
Save jamesxu123/c91e7564e4839169c66c7fe8d200be23 to your computer and use it in GitHub Desktop.
from pygame import *
from random import randint
screen = display.set_mode((800, 600))
running = True
ballPos = [400, 300]
randomBallPos = []
randomBallVel = []
speed = 5
myClock = time.Clock()
grav = 2
while running:
screen.fill(0)
for e in event.get():
if e.type == QUIT:
running = False
keys = key.get_pressed()
if keys[K_w] and 0 < ballPos[1] - speed:
ballPos[1] -= speed
if keys[K_s] and 600 > ballPos[1] + speed:
ballPos[1] += speed
if keys[K_a] and 0 < ballPos[0] - speed:
ballPos[0] -= speed
if keys[K_d] and 800 > ballPos[0] + speed:
ballPos[0] += speed
if keys[K_SPACE]:
randomBallPos.append([randint(0, 800), randint(0, 600)])
randomBallVel.append([speed, speed])
for i in range(len(randomBallPos)):
if not 0 < randomBallPos[i][0] < 800:
randomBallVel[i][0] *= -1
if not 0 < randomBallPos[i][1] < 600:
randomBallVel[i][1] *= -1
randomBallVel[i][1] += grav
for v in range(2):
randomBallPos[i][v] += randomBallVel[i][v]
draw.circle(screen, (randint(0,255), randint(0,255), randint(0,255)), randomBallPos[i], 5)
draw.circle(screen, (255, 140, 0), ballPos, 5)
myClock.tick(60)
display.flip()
quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment