Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Built by jgrumbre1 using Christian Thompson's pong and missile command games
- # Updated by Christian Thompson (@TokyoEdTech) to simplify building levels and update scoring
- import turtle
- import os
- import math
- import random
- wn = turtle.Screen()
- wn.title("Brick Break")
- wn.bgcolor("black")
- wn.setup(width=800, height=700)
- wn.tracer(0)
- # Set score to zero
- score = 0
- #Draw score
- score_pen = turtle.Turtle()
- score_pen.speed(0)
- score_pen.color("white")
- score_pen.penup()
- score_pen.goto(-380, 330)
- scorestring = "Score: %s" %score
- score_pen.write(scorestring, False, align="left", font=("Arial", 14,"normal"))
- score_pen.hideturtle()
- # paddle
- paddle= turtle.Turtle()
- paddle.speed(0)
- paddle.shape("square")
- paddle.color("red")
- paddle.shapesize(stretch_wid=1, stretch_len=5)
- paddle.penup()
- paddle.goto(0, -320)
- # ball
- ball= turtle.Turtle()
- ball.speed(0)
- ball.shape("circle")
- ball.color("blue")
- ball.shapesize(1, 1)
- ball.penup()
- ball.goto(0, -300)
- ball.dx = .75
- ball.dy = 2
- ball.speed = 15
- # Levels
- # b = brown, B = blue, Y = yellow, R = red, G = green, g = grey
- level_one = ["bbbbbbbbbb",
- "BBBBBBBBBB",
- "YYYRYYRYYY",
- "GGGGGGGGGG",
- "gggggggggg"]
- # Start at top left
- brick_x = -340
- brick_y = 220
- bricks = []
- for row in range(len(level_one)):
- print(len(level_one))
- for column in range(len(level_one[row])):
- print(row, column)
- # Select Color
- # Set default color
- color = "orange"
- # Change color
- if level_one[row][column] == "b":
- color = "brown"
- elif level_one[row][column] == "B":
- color = "blue"
- elif level_one[row][column] == "R":
- color = "red"
- elif level_one[row][column] == "Y":
- color = "yellow"
- elif level_one[row][column] == "G":
- color = "green"
- elif level_one[row][column] == "g":
- color = "grey"
- # Create brick
- brick = turtle.Turtle()
- brick.color(color)
- brick.shape("square")
- brick.shapesize(stretch_wid=1, stretch_len=3)
- brick.penup()
- brick.setposition(brick_x + column * 75, brick_y - row * 40)
- # Add brick to bricks list
- bricks.append(brick)
- # Function Move lt & rt to edge of screen
- def paddle_rt():
- x = paddle.xcor()
- x += 20
- if x > 340:
- x = 340
- paddle.setx(x)
- def paddle_lt():
- x = paddle.xcor()
- x -= 20
- if x < -340:
- x = -340
- paddle.setx(x)
- def isCollision(t1, t2):
- distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2))+math.pow(t1.ycor()-t2.ycor(),2)
- if distance < 15:
- return True
- else:
- return False
- # binding keyS
- wn.listen()
- wn.onkeypress(paddle_rt, "Right")
- wn.onkeypress(paddle_lt, "Left")
- while True:
- wn.update()
- # move ball
- ball.setx(ball.xcor() + ball.dx)
- ball.sety(ball.ycor() + ball.dy)
- # Border checking
- if ball.ycor( ) > 340:
- ball.sety(340)
- ball.dy *= -1
- if ball.xcor( ) > 390:
- ball.setx(390)
- ball.dx *= -1
- if ball.xcor( ) < -390:
- ball.setx(-390)
- ball.dx *= -1
- if ball.ycor( ) < -340:
- ball.goto(0, -290)
- ball.dx = -.75
- ball.dy = 2
- # Paddle and ball collision
- if (ball.ycor() > - 400 and ball.ycor() < -300) and (ball.xcor() <paddle.xcor() + 40 and ball.xcor() > paddle.xcor() - 50):
- ball.sety(-300)
- ball.dy *= -1
- # Check collision between ball and bricks
- for brick in bricks:
- if isCollision(ball, brick):
- brick.hideturtle()
- brick.setposition(-340, -400)
- ball.dy *= -1
- score += 10
- scorestring = "Score: %s" %score
- score_pen.clear()
- score_pen.write(scorestring, False, align="left", font=("Arial", 14,"normal"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement