Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #your name here
- from graphics import *
- def main():
- #here are some sample colors, you can add more from the book
- availableColors = ('blue', 'yellow', 'green', 'pink','black', 'white')
- #you must set all of the following variables
- title = # this is the text in the title bar of the window
- wHeight = # This is the height of the window
- wWid = # This is the width of the window
- bSize = # This is the radius of the ball
- bxStart = # This is the horizontal Position of the ball when it starts you should randomly generate this
- byStart = # This is the vertical Position of the ball when it starts - should be top of window
- bColor = # this is the ball color
- pLen = # this is the length of the paddle
- pColor = # this is th color of the paddle
- # create the graphics window
- win = GraphWin(title, wWid, wHeight)
- b = Ball(bSize, bColor, bxStart, byStart, win)
- p = Paddle (pLen, pColor,wWid/2, wHeight, win)
- # this is so the ball doesn't start falling until you press a key
- letter = win.getKey()
- # add a loop to go through each drop of the ball
- # inside that have another loop which allows the user use the keyboard to move
- # the paddle left and right (use win.checkKey()) and allow the ball to drop by
- # one per time through the loop. At the end of this second loop you probably want
- # to put a delay to slow down the movement. This would be done using time.sleep (number of seconds)
- # start with .1 which would be a tenth of a second, and it will move slow you can change the number to be smaller
- # to go faster
- win.close()
- class Ball:
- def __init__(self, radius, color, x, y, win):
- self.__window = win
- self.__circle = Circle(Point(x,y), radius)
- self.__circle.setFill(color)
- self.__circle.draw(self.__window)
- def __str__(self):
- return ("The balls position is " + str(self.getXPos) + "," + str(self.getYPos) +
- ", has a radius of " + str(self.getRadius) )
- def getRadius(self):
- return self.__circle.getRadius()
- def setRadius(self, r):
- self.__circle.setRadius(r)
- def getXPos(self):
- return self.__circle.getCenter().getX()
- def getYPos(self):
- return self.__circle.getCenter().getY()
- def setXPos(self, x):
- self.moveCircle(x, self.getYpos())
- def setYPos(self, y):
- self.__moveCircle(self.getXPos(), y)
- def getPos(self):
- return self.__circle.getCenter()
- def setPos(self, x, y):
- self.moveCircle(x, y)
- def getColor(self):
- return self.__color
- def setColor(self, color):
- self.__color = color
- self.__circle.setFill(color)
- def __moveCircle(self, x, y):
- xdiff = x - self.__circle.getCenter().getX()
- ydiff = y - self.__circle.getCenter().getY()
- self.__circle.move(xdiff, ydiff)
- def fall(self):
- self.__circle.move(0,1)
- def draw(self):
- self.__circle.draw(self.__window)
- def undraw(self):
- self.__circle.undraw()
- class Paddle:
- def __init__(self, length, color, x, y, win):
- self.__window = win
- self.__pad = Rectangle(Point(x,y), Point(x+length, y -7))
- self.setColor(color)
- self.__pad.draw(self.__window)
- def __str__(self):
- return ("The paddle's position is " + str(self.getXPos) + "," + str(self.getYPos) +
- ", has a length of " + str(self.getLength) + " and is has a color of " + self.__color)
- def getLength(self):
- return self.__pad.getP2().getX() - self.__pad.getP1().getX()
- def getColor(self):
- return self.__color
- def setColor(self, c):
- self.__color = c
- self.__pad.setFill(self.__color)
- def getXPos(self):
- return self.__pad.getP1().getX()
- def getYPos(self):
- return self.__pad.getP1().getY()
- def setXPos(self, x):
- self.__movePad(x, self.getYPos())
- def setYPos(self, y):
- self.__movePad(self.getXPos(), y)
- def getPos(self):
- return self.__pad.getP1()
- def setPos(self, x, y):
- self.__movePad(x, y)
- def move1(self, direction):
- amount = 5
- if direction == 'Left':
- self.__pad.move(-amount,0)
- elif direction == 'Right':
- self.__pad.move(amount,0)
- def __movePad(self,x, y):
- xdiff = x - self.__pad.getP1().getx()
- ydiff = y - self.__pad.getP1().getY()
- self.__pad.move(xdiff, ydiff)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement