Advertisement
tokyoedtech

SImple Breakout Demo

Nov 17th, 2018
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.88 KB | None | 0 0
  1. # Built by jgrumbre1 using Christian Thompson's pong and missile command games
  2. # Updated by Christian Thompson (@TokyoEdTech) to simplify building levels and update scoring
  3.  
  4. import turtle
  5. import os
  6. import math
  7. import random
  8.  
  9. wn = turtle.Screen()
  10. wn.title("Brick Break")
  11. wn.bgcolor("black")
  12. wn.setup(width=800, height=700)
  13. wn.tracer(0)
  14.  
  15. # Set score to zero
  16. score = 0
  17.  
  18. #Draw score
  19. score_pen = turtle.Turtle()
  20. score_pen.speed(0)
  21. score_pen.color("white")
  22. score_pen.penup()
  23. score_pen.goto(-380, 330)
  24. scorestring = "Score: %s" %score
  25. score_pen.write(scorestring, False, align="left", font=("Arial", 14,"normal"))
  26. score_pen.hideturtle()
  27.  
  28. # paddle
  29. paddle= turtle.Turtle()
  30. paddle.speed(0)
  31. paddle.shape("square")
  32. paddle.color("red")
  33. paddle.shapesize(stretch_wid=1, stretch_len=5)
  34. paddle.penup()
  35. paddle.goto(0, -320)
  36.  
  37. # ball
  38. ball= turtle.Turtle()
  39. ball.speed(0)
  40. ball.shape("circle")
  41. ball.color("blue")
  42. ball.shapesize(1, 1)
  43. ball.penup()
  44. ball.goto(0, -300)
  45. ball.dx = .75
  46. ball.dy = 2
  47. ball.speed = 15
  48.  
  49. # Levels
  50. # b = brown, B = blue, Y = yellow, R = red, G = green, g = grey
  51.  
  52. level_one = ["bbbbbbbbbb",
  53. "BBBBBBBBBB",
  54. "YYYRYYRYYY",
  55. "GGGGGGGGGG",
  56. "gggggggggg"]
  57.  
  58. # Start at top left
  59. brick_x = -340
  60. brick_y = 220
  61.  
  62. bricks = []
  63.  
  64. for row in range(len(level_one)):
  65.     print(len(level_one))
  66.     for column in range(len(level_one[row])):
  67.         print(row, column)
  68.         # Select Color
  69.         # Set default color
  70.         color = "orange"
  71.         # Change color
  72.         if level_one[row][column] == "b":
  73.             color = "brown"
  74.         elif level_one[row][column] == "B":
  75.             color = "blue"
  76.         elif level_one[row][column] == "R":
  77.             color = "red"
  78.         elif level_one[row][column] == "Y":
  79.             color = "yellow"
  80.         elif level_one[row][column] == "G":
  81.             color = "green"
  82.         elif level_one[row][column] == "g":
  83.             color = "grey"
  84.  
  85.         # Create brick
  86.         brick = turtle.Turtle()
  87.         brick.color(color)
  88.         brick.shape("square")
  89.         brick.shapesize(stretch_wid=1, stretch_len=3)
  90.         brick.penup()
  91.         brick.setposition(brick_x + column * 75, brick_y - row * 40)
  92.  
  93.         # Add brick to bricks list
  94.         bricks.append(brick)
  95.  
  96. # Function Move lt & rt to edge of screen
  97. def paddle_rt():
  98.     x = paddle.xcor()
  99.     x += 20
  100.     if x > 340:
  101.         x = 340
  102.     paddle.setx(x)
  103. def paddle_lt():  
  104.     x = paddle.xcor()
  105.     x -= 20
  106.     if x < -340:
  107.         x = -340
  108.     paddle.setx(x)
  109.  
  110. def isCollision(t1, t2):
  111.     distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2))+math.pow(t1.ycor()-t2.ycor(),2)
  112.     if distance < 15:
  113.         return True
  114.     else:
  115.         return False
  116.  
  117. # binding keyS
  118. wn.listen()
  119. wn.onkeypress(paddle_rt, "Right")  
  120. wn.onkeypress(paddle_lt, "Left")  
  121.  
  122. while  True:
  123.      
  124.     wn.update()
  125.  # move ball
  126.     ball.setx(ball.xcor() + ball.dx)
  127.     ball.sety(ball.ycor() + ball.dy)
  128.  
  129. # Border checking
  130.     if ball.ycor( ) > 340:
  131.         ball.sety(340)
  132.         ball.dy *= -1
  133.  
  134.     if ball.xcor( ) > 390:
  135.         ball.setx(390)
  136.         ball.dx *= -1
  137.  
  138.     if ball.xcor( ) < -390:
  139.         ball.setx(-390)
  140.         ball.dx *= -1
  141.  
  142.     if ball.ycor( ) < -340:
  143.         ball.goto(0, -290)
  144.         ball.dx = -.75
  145.         ball.dy = 2
  146.  
  147.  # Paddle and ball collision
  148.     if (ball.ycor() > - 400 and ball.ycor() < -300) and (ball.xcor() <paddle.xcor() + 40 and ball.xcor() > paddle.xcor() - 50):
  149.         ball.sety(-300)
  150.         ball.dy *= -1                
  151.    
  152.    # Check collision between ball and bricks
  153.     for brick in bricks:
  154.         if isCollision(ball, brick):
  155.             brick.hideturtle()
  156.             brick.setposition(-340, -400)
  157.             ball.dy *= -1
  158.             score += 10
  159.             scorestring = "Score: %s" %score
  160.             score_pen.clear()
  161.             score_pen.write(scorestring, False, align="left", font=("Arial", 14,"normal"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement