Advertisement
Guest User

Project 4

a guest
Dec 15th, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. #your name here
  2.  
  3. from graphics import *
  4.  
  5. def main():
  6.  
  7. #here are some sample colors, you can add more from the book
  8. availableColors = ('blue', 'yellow', 'green', 'pink','black', 'white')
  9.  
  10. #you must set all of the following variables
  11. title = # this is the text in the title bar of the window
  12. wHeight = # This is the height of the window
  13. wWid = # This is the width of the window
  14. bSize = # This is the radius of the ball
  15. bxStart = # This is the horizontal Position of the ball when it starts you should randomly generate this
  16. byStart = # This is the vertical Position of the ball when it starts - should be top of window
  17. bColor = # this is the ball color
  18. pLen = # this is the length of the paddle
  19. pColor = # this is th color of the paddle
  20.  
  21. # create the graphics window
  22. win = GraphWin(title, wWid, wHeight)
  23. b = Ball(bSize, bColor, bxStart, byStart, win)
  24. p = Paddle (pLen, pColor,wWid/2, wHeight, win)
  25.  
  26. # this is so the ball doesn't start falling until you press a key
  27. letter = win.getKey()
  28.  
  29. # add a loop to go through each drop of the ball
  30. # inside that have another loop which allows the user use the keyboard to move
  31. # the paddle left and right (use win.checkKey()) and allow the ball to drop by
  32. # one per time through the loop. At the end of this second loop you probably want
  33. # to put a delay to slow down the movement. This would be done using time.sleep (number of seconds)
  34. # start with .1 which would be a tenth of a second, and it will move slow you can change the number to be smaller
  35. # to go faster
  36.  
  37.  
  38.  
  39.  
  40. win.close()
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47. class Ball:
  48. def __init__(self, radius, color, x, y, win):
  49.  
  50. self.__window = win
  51. self.__circle = Circle(Point(x,y), radius)
  52. self.__circle.setFill(color)
  53. self.__circle.draw(self.__window)
  54.  
  55. def __str__(self):
  56.  
  57. return ("The balls position is " + str(self.getXPos) + "," + str(self.getYPos) +
  58. ", has a radius of " + str(self.getRadius) )
  59. def getRadius(self):
  60. return self.__circle.getRadius()
  61.  
  62. def setRadius(self, r):
  63. self.__circle.setRadius(r)
  64.  
  65. def getXPos(self):
  66. return self.__circle.getCenter().getX()
  67.  
  68. def getYPos(self):
  69. return self.__circle.getCenter().getY()
  70.  
  71. def setXPos(self, x):
  72.  
  73. self.moveCircle(x, self.getYpos())
  74.  
  75. def setYPos(self, y):
  76. self.__moveCircle(self.getXPos(), y)
  77.  
  78. def getPos(self):
  79. return self.__circle.getCenter()
  80.  
  81. def setPos(self, x, y):
  82. self.moveCircle(x, y)
  83.  
  84. def getColor(self):
  85. return self.__color
  86.  
  87.  
  88. def setColor(self, color):
  89. self.__color = color
  90. self.__circle.setFill(color)
  91.  
  92. def __moveCircle(self, x, y):
  93.  
  94. xdiff = x - self.__circle.getCenter().getX()
  95. ydiff = y - self.__circle.getCenter().getY()
  96. self.__circle.move(xdiff, ydiff)
  97.  
  98. def fall(self):
  99.  
  100. self.__circle.move(0,1)
  101.  
  102.  
  103. def draw(self):
  104. self.__circle.draw(self.__window)
  105.  
  106. def undraw(self):
  107. self.__circle.undraw()
  108.  
  109. class Paddle:
  110. def __init__(self, length, color, x, y, win):
  111. self.__window = win
  112. self.__pad = Rectangle(Point(x,y), Point(x+length, y -7))
  113. self.setColor(color)
  114. self.__pad.draw(self.__window)
  115.  
  116.  
  117. def __str__(self):
  118.  
  119. return ("The paddle's position is " + str(self.getXPos) + "," + str(self.getYPos) +
  120. ", has a length of " + str(self.getLength) + " and is has a color of " + self.__color)
  121.  
  122. def getLength(self):
  123. return self.__pad.getP2().getX() - self.__pad.getP1().getX()
  124.  
  125. def getColor(self):
  126. return self.__color
  127.  
  128. def setColor(self, c):
  129. self.__color = c
  130. self.__pad.setFill(self.__color)
  131.  
  132. def getXPos(self):
  133. return self.__pad.getP1().getX()
  134.  
  135. def getYPos(self):
  136. return self.__pad.getP1().getY()
  137.  
  138. def setXPos(self, x):
  139.  
  140. self.__movePad(x, self.getYPos())
  141.  
  142. def setYPos(self, y):
  143.  
  144. self.__movePad(self.getXPos(), y)
  145.  
  146. def getPos(self):
  147. return self.__pad.getP1()
  148.  
  149.  
  150. def setPos(self, x, y):
  151. self.__movePad(x, y)
  152.  
  153. def move1(self, direction):
  154. amount = 5
  155. if direction == 'Left':
  156. self.__pad.move(-amount,0)
  157. elif direction == 'Right':
  158. self.__pad.move(amount,0)
  159.  
  160. def __movePad(self,x, y):
  161. xdiff = x - self.__pad.getP1().getx()
  162. ydiff = y - self.__pad.getP1().getY()
  163. self.__pad.move(xdiff, ydiff)
  164.  
  165.  
  166.  
  167. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement