Text Size
Text Size
Exit Full Screen
Python Mini
Show
Source Code
View Source Code in Full Screen
Open in New Tab
from processing import * width = 550 height = 400 score = 0 ball = { 'x': 50, # x coor of the center of ball 'y': 50, # y coor of center of the ball "velocity_x": 2, # speed in x direction "velocity_y": 3, # speed in y direction "radius": 10 # radius of ball } balls = [ball] paddle = { 'x': 200, 'y': height - 20, 'width': 100, 'height': 15, 'velocity': 5, 'move': 0 } def drawBall(ball): fill(255, 255, 255) # stroke(0, 0, 0) ellipse(ball['x'], ball['y'], 2 * ball["radius"], 2 * ball["radius"]) def drawPaddle(): fill(0, 0, 255) rect(paddle['x'], paddle['y'], paddle['width'], paddle['height']) def moveBall(ball): ball['x'] = ball['x'] + ball["velocity_x"] ball['y'] = ball['y'] + ball["velocity_y"] if ball['y'] + ball["radius"] >= height and len(balls) <= 1: textSize(30) fill(255, 0, 0) text("Game Over", 200, 150) exitp() if ball['y'] + ball["radius"] >= height and len(balls) >= 1: balls.remove(ball) if ball['y'] - ball["radius"] <= 0: ball["velocity_y"] = -ball["velocity_y"] if ball['x'] + ball["radius"] >= width or ball['x'] - ball["radius"] <= 0: ball["velocity_x"] = -ball["velocity_x"] def movePaddle(): paddle['x'] += paddle['velocity'] * paddle['move'] if paddle['x'] <= 0: # check if the paddle is hitting the left wall paddle['x'] = 0 elif paddle['x'] + paddle['width'] > width: # check if the paddle is hitting the right wall paddle['x'] = width - paddle['width'] def setup(): # set the size of the canvas size(width, height) background(0, 53, 140) def checkCollisions(ball): if ball['x'] >= paddle['x'] and ball['x'] <= paddle['x'] + paddle['width']: if ball['y'] + ball['radius'] >= paddle['y']: ball['velocity_y'] = -ball['velocity_y'] ball['y'] = paddle['y'] - ball['radius'] global score score = score + 1 if score % 7 == 0: paddle['width'] = paddle['width'] + 25 if score % 11 == 0: balls.append({ 'x': 50, # x coor of the center of ball 'y': 50, # y coor of center of the ball "velocity_x": 2, # speed in x direction "velocity_y": 3, # speed in y direction "radius": 10 # radius of ball }) def draw(): setup() for ball in balls: drawBall(ball) moveBall(ball) checkCollisions(ball) drawPaddle() movePaddle() fill(255, 255, 255) textSize(15) text("Score: " + str(score), 20, 20) def keyPressed(): if keyboard.keyCode == LEFT: # left arrow key pressed paddle['move'] = -1 elif keyboard.keyCode == RIGHT: # right arrow key pressed paddle['move'] = 1 def keyReleased(): if keyboard.keyCode in [LEFT, RIGHT]: paddle['move'] = 0 run()
PythonAddABall.py
( around 92 lines python code )
Published By:
EM3944
Published on
2019-02-12T13:59:55Z
Python Mini
- an
OYOclass
application,
own your own class today
.
Run
Result
×
Error message shows here