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 * from webaudio import Audio ball_x = 50 ball_y = 50 velocity_x = 2 velocity_y = -2 paddle_x = 200 paddle_y = 380 paddle_move_speed = 40 paddle_length = 100 score = 0 game_start = False brick_length = 60 brick_height = 20 bricks = [] sound = None def setup(): global sound background(0, 0, 0) size(560, 400) sound = Audio("http://bofeng.oyosite.com/hit.wav") # initialize bricks position for i in range(3): for j in range(6): brick = { "x" : 20 + j * 90, "y" : 60 + i * 60 } bricks.append(brick) def draw(): global ball_x, ball_y, velocity_x, velocity_y, score background(0, 0, 0) # draw bricks fill(255, 153, 0) for brick in bricks: rect(brick["x"], brick["y"], brick_length, brick_height) # if no brick, player win if len(bricks) == 0: fill(0, 255, 0) textSize(25) text("You Win !!!", 220, 250) exitp() # draw paddle fill(0, 0, 255) rect(paddle_x, paddle_y, paddle_length, 15) # if not game_start: fill(0, 255, 0) textSize(20) text("Press space to start", 180, 250) # draw ball if not game_start: ball_x = paddle_x + 50 ball_y = paddle_y - 10 else: ball_x += velocity_x ball_y += velocity_y fill(255, 255, 255) ellipse(ball_x, ball_y, 20, 20) if (ball_x + 20 > 560 or ball_x - 10 < 0): velocity_x = -velocity_x if (ball_y - 10 < 0): velocity_y = -velocity_y # collision with paddle if (ball_y + 10 > paddle_y and ball_x > paddle_x and ball_x < paddle_x + paddle_length): sound.play() ball_y = paddle_y - 10 velocity_y = -velocity_y # out of boundary if ball_y > 400: fill(255, 0, 0) textSize(20) text("Game Over", 220, 250) exitp() # collision with bricks collision_index = None for i in range(len(bricks)): brick = bricks[i] x = brick["x"] y = brick["y"] # collision with brick top and bottom if (ball_x > x and ball_x < x + brick_length): if (velocity_y > 0 and ball_y + 10 > y and ball_y < y + brick_height) or \ (velocity_y < 0 and ball_y - 10 < y + brick_height and ball_y > y): velocity_y = -velocity_y collision_index = i break # collision with brick left and right edge if (ball_y > y and ball_y < y + brick_height): if (velocity_x > 0 and ball_x + 10 > x and ball_x < x + brick_length) or \ (velocity_x < 0 and ball_x - 10 < x + brick_length and ball_x > x): velocity_x = -velocity_x collision_index = i break if collision_index is not None: score += 1 sound.play() if score % 5 == 0: velocity_x += velocity_x > 0 and 1 or -1 velocity_y += velocity_y > 0 and 1 or -1 del bricks[collision_index] fill(255, 255, 255) textSize(15) text("Score: " + str(score), 15, 30) def keyPressed(): global paddle_x, paddle_length, game_start # Hit E, to extend paddle (cheat mode) if keyboard.keyCode == 69: paddle_length += 20 # space key if keyboard.keyCode == 32: if not game_start: game_start = True if keyboard.keyCode == 37: # left arrow key paddle_x -= paddle_move_speed elif keyboard.keyCode == 39: # right arrow key paddle_x += paddle_move_speed run()
bricks-v2.py
( around 120 lines python code )
Published By:
Bo Tinker
Published on
2014-12-24T21:53:38Z
Python Mini
- an
OYOclass
application,
own your own class today
.
Run
Result
×
Error message shows here