Beispiel #1
0
class Car():
    WHEEL_TO_TIRE_RATIO = 0.6

    def __init__(self, back_wheel_center, back_tire_radius, front_wheel_center,
                 front_tire_radius, body_height):
        upper_left_point = Point(back_wheel_center.x,
                                 back_wheel_center.y - body_height)
        bottom_right_point = front_wheel_center
        self.body = Rectangle(upper_left_point, bottom_right_point)

        self.back_wheel = Wheel(back_wheel_center,
                                back_tire_radius * Car.WHEEL_TO_TIRE_RATIO,
                                back_tire_radius)
        self.front_wheel = Wheel(front_wheel_center,
                                 front_tire_radius * Car.WHEEL_TO_TIRE_RATIO,
                                 front_tire_radius)

    def set_color(self, tire_color, wheel_color, body_color):
        self.body.setFill(body_color)

        self.back_wheel.set_color(wheel_color, tire_color)
        self.front_wheel.set_color(wheel_color, tire_color)

    def draw(self, win):
        self.body.draw(win)
        self.back_wheel.draw(win)
        self.front_wheel.draw(win)

    def animate(self, win, dx, dy, n):
        pass
Beispiel #2
0
class Game():   
    
    def __init__(self, gru_file=None):
        
        self.compiler = Compiler()
        if gru_file:
            self.stream = self.compiler.decompile(gru_file) 
        else:            
            self.stream = self.compiler.compile(None)   
        self.metadata = self.stream.metadata   
        self.flags = Flags(self.stream)
        self.wheel = Wheel(config)
        self.title = Title(config)
        self.inventory = Inventory(self.stream, self.flags, config)
        self.combiner = Combiner(self.stream, self.flags, self.inventory)
        self.page = Page(config, self.flags, self.combiner, self.inventory)
        self.state = State(self.stream, self.flags, self.inventory, self.wheel, self.combiner, self.title, self.page)               
        if self.metadata.has_key("start"):
            start = self.metadata["start"]
            self.state.update(start)
        else:
            self.state.update("start")

    def draw(self, tick):
        self.inventory.draw()
        self.wheel.draw()
        self.title.draw()
        self.page.draw(tick)

    
        
        
        
class Car():
    def __init__(self, wheel_point1, wheel_radius1, wheel_point2,
                 wheel_radius2, height):
        self.wheel1 = Wheel(wheel_point1, wheel_radius1 * 0.6, wheel_radius1)
        self.wheel2 = Wheel(wheel_point2, wheel_radius2 * 0.6, wheel_radius2)
        # TODO - Get the point coordinates to get the corners
        self.body = Rectangle(
            Point(wheel_point1.getX(),
                  wheel_point1.getY() - height),
            Point(wheel_point2.getX(), wheel_point2.getY()))

    def draw(self, win):
        self.wheel1.draw(win)
        self.wheel2.draw(win)
        self.body.draw(win)

    def move(self, dx, dy):
        self.wheel1.move(dx, dy)
        self.wheel2.move(dx, dy)
        self.body.move(dx, dy)

    def set_color(self, tire_color, wheel_color, body_color):
        # TODO - Set the colors
        self.wheel1.set_color(wheel_color, tire_color)
        self.wheel2.set_color(wheel_color, tire_color)
        self.body.setFill(body_color)

    def undraw(self):
        self.wheel1.undraw()
        self.wheel2.undraw()
        self.body.undraw()


#     def get_size(self):
#         return self.tire_circle.getRadius()
#
#     def get_center(self):
#         return self.tire_circle.getCenter()

    def animate(self, win, dx, dy, n):
        # TODO - Move the body and the wheels
        if n > 0:
            self.wheel1.move(dx, dy)
            self.wheel2.move(dx, dy)
            self.body.move(dx, dy)
            win.after(50, self.animate, win, dx, dy, n - 1)
class Car:  # Car class definition
    def __init__(
        self, back_wheel_pos, back_wheel_rad, front_wheel_pos, front_wheel_rad, car_height
    ):  # constructor method to instantiate
        self.back_wheel = Wheel(back_wheel_pos, back_wheel_rad, back_wheel_rad + 5)  # a class object
        self.front_wheel = Wheel(
            front_wheel_pos, front_wheel_rad, front_wheel_rad + 5
        )  # attributes of the Car class are defined
        self.car_body = Rectangle(
            Point(back_wheel_pos.getX(), back_wheel_pos.getY() - car_height), front_wheel_pos  # and initialized
        )
        self.car_color = ""

        self.Road_1 = Line(Point(0, 100), Point(1200, 100))

        self.Road_2 = Line(Point(0, 40), Point(1200, 40))

    def carMove(self, dx, dy):  # function to effect motion of the car object
        self.back_wheel.move(dx, dy)  # by individually moving each item
        self.front_wheel.move(dx, dy)
        self.car_body.move(dx, dy)

    def setColour(self, tireColour, wheelColour, carColour):  # function to set the colour attribute of each item
        self.back_wheel.set_color(wheelColour, tireColour)
        self.front_wheel.set_color(wheelColour, tireColour)
        self.car_body.setFill(carColour)

    def animate(
        self, win, dx, dy, n
    ):  # function to perform the animation. This function is looped by the 'after' function until n = 0
        if n > 0:
            self.carMove(dx, dy)
        win.after(100, self.animate, win, dx, dy, n - 1)

    def draw(
        self, win
    ):  # draw the car object by drawing each individual item. The last item drawn appears on the foreground
        self.car_body.draw(win)
        self.back_wheel.draw(win)
        self.front_wheel.draw(win)
def main():
    new_win = GraphWin("A Car", 700, 300)

    # 1st wheel centered at 50,50 with radius 15
    wheel_center_1 = Point(50, 50)
    wheel_radius_1 = 15
    tire_radius_1 = 0.6 * tire_radius_1

    # 2nd wheel centered at 100,50 with radius 15
    wheel_center_2 = Point(100, 50)
    wheel_radius_2 = 15
    tire_radius_2 = 0.6 * tire_radius_1

    # rectangle with a height of 40
    upper_left_point = Point(15, 55)
    bottom_right_point = Point(75, 15)

    wheel_1 = Wheel(wheel_center_1, 0.6 * tire_radius_1, tire_radius_1)
    wheel_2 = Wheel(wheel_center_2, 0.6 * tire_radius_2, tire_radius_2)
    body = Rectangle(upper_left_point, bottom_right_point)

    # Set its color
    wheel_1.set_color('OliveDrab1', 'Pink')
    wheel_2.set_color('OliveDrab1', 'Pink')
    body.setFill('Blue')

    # And finally, draw it
    wheel_1.draw(new_win)
    wheel_2.draw(new_win)
    body.draw(new_win)

    car1 = Car(wheel_1, wheel_2, body)
    car1.draw(new_win)

    # make the car move on the screen
    car1.animate(new_win, 1, 0, 400)

    new_win.mainloop()
Beispiel #6
0
def main():
    #SQLite connection
    conn = sqlite3.connect("./wheel.db")
    #conn = sqlite3.connect(":memory:")
    c = conn.cursor()
    #Create table to hold the items if one does not already exist.
    #Prevent it from accepting duplicate titles
    c.execute('''CREATE TABLE IF NOT EXISTS movies(title TEXT,
                    CONSTRAINT no_duplicates UNIQUE (title));''')
    pygame.init()
    #Try to use comic sans as the font. If it's not available,
    #pygame automatically uses the default system font
    myfont = pygame.font.SysFont('Comic Sans MS', 20)
    ifont = pygame.font.SysFont('Comic Sans MS', 30)
    white = (255, 255, 255)
    black = (0, 0, 0)
    #Grab the items currently in the database
    movies = getFromDatabase(conn)
    print("MOVIES:", movies)
    running = True
    spinning = False
    #Note, display size currently affects how fast the circle spins
    screen = pygame.display.set_mode((700, 700),
                                     pygame.HWSURFACE | pygame.RESIZABLE)
    screen.fill(white)
    surface = pygame.display.get_surface()
    width, height, center, radius, circumfrence = updateWheelSize(screen)
    #Perform initial circle calculations
    arclen, theta, chord = updateCalculations(movies, circumfrence, radius)
    #The current angle of rotation
    angle = radians(0)
    pygame.display.flip()
    pygame.display.set_caption("Wheel of the Worst!")
    wheel = Wheel(center, radius, movies)
    speed = random.randint(10, 50)
    slowdown = random.randint(1, 7) / 1000
    print("SPEED:", speed)
    print("SLOWDOWN:", slowdown)
    #Spin button
    spinButton = Button("SPIN", (width - 125, 25), (100, 50), (0, 255, 0),
                        (247, 255, 5), (0, 135, 23), ifont, black)
    textInputer = InputField((center[0] - 150, height - 75), (300, 50), ifont)
    addButton = Button("Add Item", (center[0] + 175, height - 75), (125, 50),
                       (0, 255, 0), (247, 255, 5), (0, 135, 23), myfont, black)
    delButton = Button("Delete Item", (center[0] - 300, height - 75),
                       (125, 50), (255, 0, 0), (247, 255, 5), (128, 0, 0),
                       myfont, black)
    #Game loop
    while running:
        #Points of the arrow that will point to the winning item
        tri_points = [(center[0], center[1] - radius - 10),
                      (center[0] - 10, center[1] - radius - 30),
                      (center[0] + 10, center[1] - radius - 30)]
        mousePos = pygame.mouse.get_pos()
        if spinning and len(movies) > 0:
            #Modify the angle so that the sections will rotate
            angle += radians(speed)
            speed -= slowdown
            if (speed <= 0):
                spinning = False
        #Reset the screen
        screen.fill(white)
        pygame.draw.polygon(surface, (255, 0, 0), tri_points, 0)
        #Draw the wheel and the sections within it
        wheel.draw(surface, black, angle, radians(theta))
        #Draw the buttons
        spinButton.draw(surface, (width - 125, 25), black)
        addButton.draw(surface, (center[0] + 175, height - 75), black)
        delButton.draw(surface, (center[0] - 300, height - 75), black)
        textInputer.draw(surface, (center[0] - 150, height - 75))
        #update the display
        pygame.display.update()
        #Handle user events such as key presses or clicks
        for event in pygame.event.get():
            #Handle if the user hits the X button
            if event.type == pygame.QUIT:
                conn.close()
                pygame.quit()
                running = False
            if event.type == pygame.VIDEORESIZE:
                screen = pygame.display.set_mode(
                    (event.w, event.h), pygame.HWSURFACE | pygame.RESIZABLE)
                width, height, center, radius, circumfrence = updateWheelSize(
                    screen)
                arclen, theta, chord = updateCalculations(
                    movies, circumfrence, radius)
                wheel.setCenter(center)
                wheel.setRadius(radius)
            #If the user is moving the mouse around, check
            #if they are hovering over any of the buttons
            if event.type == pygame.MOUSEMOTION:
                spinButton.mouseHover(mousePos)
                addButton.mouseHover(mousePos)
                delButton.mouseHover(mousePos)
                textInputer.mouseHover(mousePos)
            #If the user clicked
            if event.type == pygame.MOUSEBUTTONDOWN:
                #If the user clicked while hovering over the button
                if spinButton.mouseHover(mousePos):
                    spinButton.click()
                    angle = radians(0)
                    speed = random.randint(10, 50)
                    slowdown = random.randint(1, 7) / 1000
                    spinning = True
                    print("ANGLE:", angle)
                    print("SPEED:", speed)
                    print("SLOWDOWN:", slowdown)
                if addButton.mouseHover(mousePos):
                    addButton.click()
                    addText = textInputer.getText()
                    #If the add text is not just whitespace, add it
                    if addText.strip() != "":
                        try:
                            addToDatabase(conn, addText)
                            movies.clear()
                            movies += getFromDatabase(conn)
                            arclen, theta, chord = updateCalculations(
                                movies, circumfrence, radius)
                            wheel.addItem(movies[-1])
                            textInputer.clearText()
                        except sqlite3.IntegrityError:
                            #Item already exists in the table
                            print("Already in table")
                            pass
                if delButton.mouseHover(mousePos):
                    delButton.click()
                    remText = textInputer.getText()
                    #If the remove text is not just whitespace, remove it
                    if remText.strip() != "":
                        deleteFromDatabase(conn, remText)
                        movies.clear()
                        movies += getFromDatabase(conn)
                        arclen, theta, chord = updateCalculations(
                            movies, circumfrence, radius)
                        wheel.deleteItem(remText)
                        textInputer.clearText()
                if textInputer.mouseHover(mousePos):
                    textInputer.click()
                else:
                    #Release the text inputer if the user clicked anywhere
                    #outside of it
                    textInputer.release()
            #Change the color of buttons back when mouse is released
            if event.type == pygame.MOUSEBUTTONUP:
                spinButton.release()
                addButton.release()
                delButton.release()
            #Key events
            if event.type == pygame.KEYDOWN:
                #if the user pressed a key while the text inputer is active
                if textInputer.isClicked():
                    #Input that key
                    textInputer.keyInput(event.unicode)