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 * wt = 500 ht = 400 originalXpos = 250 originalYpos = 367.5 xv = 5.1 yv = -4.5 global score, lives, isGameBegun score = 0 lives = 5 isGameBegun = False paddle = { "xp" : 200, "yp" : 375, "wt" : 80, "ht" : 10, "vv" : 15, "mx" : 0 } b = { "xp" : originalXpos, "yp" : originalYpos, "xv" : xv, "yv" : yv, "rad" : 7.5 } # ::::::::::::::::::: BRICKS ::::::::::::::::::::::::::::::: def drawBrick(brick): fill(255, 200, 0) rect(brick["xp"], brick["yp"], brick["wt"], brick["ht"]) def generateBricks(): global allBricks size(500, 400) background(0, 0, 0) allBricks = [] rValue = 0 gValue = 0 currentYpos = 40 for columnHt in range(4): currentXpos = 20 for rowLt in range(6): brick = { "xp" : currentXpos, "yp" : currentYpos, "wt" : 50, "ht" : 15 } allBricks.append(brick) currentXpos += 80 currentYpos += 55 def drawAllBricks(): for brick in allBricks: drawBrick(brick) # ::::::::::::::**** BRICK BREAKING ****:::::::::::::::::::::: def brickCollisions(): global allBricks, score currentBrickIndex = 0 currentBrickList = allBricks ballLeftEdge = b["xp"] - b["rad"] ballRightEdge = b["xp"] + b["rad"] ballTopEdge = b["yp"] - b["rad"] ballBottomEdge = b["yp"] + b["rad"] for brick in currentBrickList: brickLeftEdge = brick["xp"] brickRightEdge = brick["xp"] + brick["wt"] brickTopEdge = brick["yp"] brickBottomEdge = brick["yp"] + brick["ht"] # Is the ball LEFT or RIGHT of the given brick ? ballXpos = "left" ballYpos = "top" if ballRightEdge <= brickLeftEdge: ballXpos = "left" elif ballLeftEdge >= brickRightEdge: ballXpos = "right" else: ballXpos = "middle" # Is the ball ABOVE or BELOW the given brick ? if ballBottomEdge <= brickTopEdge: ballYpos = "above" elif ballTopEdge >= brickBottomEdge: ballYpos = "below" else: ballYpos = "middle" # [[[[ BRICK BOTTOM EDGE ]]]] if ballXpos == "middle" and ballYpos == "below": if b["yv"] < 0 and ballTopEdge + b["yv"] <= brickBottomEdge: distToBrickBottomEdge = ballTopEdge - brickBottomEdge b["yp"] -= distToBrickBottomEdge b["yv"] = b["yv"] *-1 currentBrickList.pop(currentBrickIndex) score += 1 # [[[[ BRICK TOP EDGE ]]]] if ballXpos == "middle" and ballYpos == "above": if b["yv"] > 0 and ballBottomEdge + b["yv"] >= brickTopEdge: distToBrickTopEdge = brickTopEdge - ballBottomEdge b["yp"] += distToBrickTopEdge b["yv"] = b["yv"] *-1 currentBrickList.pop(currentBrickIndex) score += 1 # [[[[ BRICK LEFT EDGE ]]]] if ballYpos == "middle" and ballXpos == "left": if b["xv"] > 0 and ballRightEdge + b["xv"] >= brickLeftEdge: distToBrickLeftEdge = brickLeftEdge - ballRightEdge b["xp"] += distToBrickLeftEdge b["xv"] = b["xv"] *-1 currentBrickList.pop(currentBrickIndex) score += 1 # [[[[ BRICK RIGHT EDGE ]]]] if ballYpos == "middle" and ballXpos == "right": if b["xv"] < 0 and ballLeftEdge + b["xv"] <= brickRightEdge: distToBrickRightEdge = ballLeftEdge - brickRightEdge b["xp"] -= distToBrickRightEdge b["xv"] = b["xv"] *-1 currentBrickList.pop(currentBrickIndex) score += 1 # [[[[ CORNER HITS ]]]] if ballXpos == "middle" and ballYpos == "middle": if b["xv"] > 0 and b["yv"] > 0 and b["xp"] < brickLeftEdge: if ballRightEdge - brickLeftEdge > ballBottomEdge - brickTopEdge: b["yv"] = b["yv"]*-1 currentBrickList.pop(currentBrickIndex) score += 1 elif ballRightEdge - brickLeftEdge < ballBottomEdge - brickTopEdge: b["xv"] = b["xv"]*-1 currentBrickList.pop(currentBrickIndex) score += 1 else: b["xv"] = b["xv"]*-1 b["yv"] = b["yv"]*-1 currentBrickList.pop(currentBrickIndex) score += 1 elif b["xv"] > 0 and b["yv"] < 0 and b["xp"] < brickLeftEdge: if ballRightEdge - brickLeftEdge > brickBottomEdge - ballTopEdge: b["yv"] = b["yv"]*-1 currentBrickList.pop(currentBrickIndex) score += 1 elif ballRightEdge - brickLeftEdge < brickBottomEdge - ballTopEdge: b["xv"] = b["xv"]*-1 currentBrickList.pop(currentBrickIndex) score += 1 else: b["xv"] = b["xv"]*-1 b["yv"] = b["yv"]*-1 currentBrickList.pop(currentBrickIndex) score += 1 elif b["xv"] < 0 and b["yv"] < 0 and b["xp"] > brickRightEdge: if brickRightEdge - ballLeftEdge > brickBottomEdge - ballTopEdge: b["yv"] = b["yv"]*-1 currentBrickList.pop(currentBrickIndex) score += 1 elif brickRightEdge - ballLeftEdge < brickBottomEdge - ballTopEdge: b["xv"] = b["xv"]*-1 currentBrickList.pop(currentBrickIndex) score += 1 else: b["xv"] = b["xv"]*-1 b["yv"] = b["yv"]*-1 currentBrickList.pop(currentBrickIndex) score += 1 elif b["xv"] < 0 and b["yv"] > 0 and b["xp"] > brickRightEdge: if brickRightEdge - ballLeftEdge > ballBottomEdge - brickTopEdge: b["yv"] = b["yv"]*-1 currentBrickList.pop(currentBrickIndex) score += 1 elif brickRightEdge - ballLeftEdge < ballBottomEdge - brickTopEdge: b["xv"] = b["xv"]*-1 currentBrickList.pop(currentBrickIndex) score += 1 else: b["xv"] = b["xv"]*-1 b["yv"] = b["yv"]*-1 currentBrickList.pop(currentBrickIndex) score += 1 allBricks = currentBrickList # :::::::::::::: ENDGAME ::::::::::::::::::::::: currentBrickIndex += 1 if len(allBricks) == 0: textSize(100) fill(0, 255, 0) text("VICTORY!", 20, 240) exitp() # :::::::::::::::: PADDLE ::::::::::::::::::::::::::::::: def drawPaddle(): fill(0, 255, 255) rect(paddle["xp"], paddle["yp"], paddle["wt"], paddle["ht"]) def keyPressed(): global isGameBegun if keyboard.keyCode == 32: if not isGameBegun: isGameBegun = True if keyboard.keyCode == LEFT: paddle["mx"] = -1 elif keyboard.keyCode == RIGHT: paddle["mx"] = 1 def movePaddle(): if not isGameBegun: paddle["xp"] = 200 paddle["mv"] = 0 else: paddle["xp"] = (paddle["xp"] + paddle["vv"] * paddle["mx"]) if paddle["xp"] <= 0: paddle["xp"] = 0 if paddle["xp"] + paddle["wt"] >= wt: paddle["xp"] = wt - paddle["wt"] def keyReleased(): if keyboard.keyCode in [LEFT, RIGHT]: paddle["mx"] = 0 # ::::::::::::::::: BALL ::::::::::::::::::::::::::::::::::: def drawB(): fill(255, 255, 255) ellipse(b["xp"], b["yp"], b["rad"]*2, b["rad"]*2) def moveB(): global score, lvlup, lives, isGameBegun if not isGameBegun: b["xp"] = paddle["xp"] + paddle["wt"]*0.5 b["yp"] = paddle["yp"] - b["rad"] else: b["xp"] = b["xp"] + b["xv"] b["yp"] = b["yp"] + b["yv"] if b["xp"] - b["rad"] <= 0 or b["xp"] + b["rad"] >= wt: b["xv"] = b["xv"]*-1 # PADDLE COLLISION BEHAVIOR ::::::::::::::::::::::::::::: if b["yp"] + b["rad"] >= paddle["yp"] and b["xp"] > paddle["xp"] and b["xp"] < paddle["xp"] + paddle["wt"]: distToPaddleTop = paddle["yp"] - (b["yp"] + b["rad"]) b["yp"] = b["yp"] + distToPaddleTop b["yv"] = b["yv"]*-1 # :::::: BRICK COLLISION ::::::::::::::::::::::::::::::: brickCollisions() # :::::: LOSING LIVES ::::::::::::::::::::::::::::::::::::::::: if b["yp"] - b["rad"] <= 0: b["yv"] = b["yv"]*-1 if b["yp"] + b["rad"] >= ht and lives > 0: isGameBegun = False paddle["xp"] = 200 b["xv"] = xv b["yv"] = yv lives -= 1 # :::::::::::::::::: SCORE AND LIVES ::::::::::::::::::::::::::::::::: def display(): textSize(16) fill(255, 255, 255) text("Score: %d"%score, 15, 25) textSize(16) fill(255, 255, 255) text("Lives: %d"%lives, 425, 25) if b["yp"] + b["rad"] >= ht and lives <= 0: textSize(60) fill(255, 0, 0) text("Game Over", 100, 150) textSize(16) exitp() def setup(): size(wt, ht) keyPressed() keyReleased() generateBricks() def draw(): background(0, 0, 0) if not isGameBegun: textSize(20) fill(0, 255, 0) text("Press SPACE to begin", 150, 300) drawPaddle() movePaddle() drawB() moveB() drawAllBricks() display() run()
ch_BrickBreaker_AddLives.py
( around 269 lines python code )
Published By:
EM5700
Published on
2020-03-28T20:12:47Z
Python Mini
- an
OYOclass
application,
own your own class today
.
Run
Result
×
Error message shows here