Example #1
0
def main():
    pygame.init()

    width = 1200
    height = 800

    screen = pygame.display.set_mode([width, height])
    screen_center = Vec2d(width / 2, height / 2)
    coords = Coords(screen_center.copy(), 1, True)
    # ^ Center of window is (0,0), scale is 1:1, and +y is up

    coords.zoom_at_coords(Vec2d(0, 0), 2)
    # ^Sets camera center

    # Used to manage how fast the screen updates
    clock = pygame.time.Clock()

    planets = []

    frame_rate = 60
    playback_speed = 1
    dt = playback_speed / frame_rate
    paused = False
    done = False

    mouseClicked = False
    mousePosDown = Vec2d(0, 0)
    mousePosUp = Vec2d(0, 0)

    wall1 = Wall(Vec2d(150, 0), 45, 213, 4)
    wall2 = Wall(Vec2d(-150, 0), 315, 213, 4)

    print(wall1.normal)
    print(wall2.normal)

    while not done:
        gameMouse = pygame.mouse  #Mouse obj
        mousePosTup = gameMouse.get_pos(
        )  #Mouse pos in default coordinates as list
        mousePosVec = Vec2d(
            mousePosTup[0],
            mousePosTup[1])  #Mouse pos in default coordinates as vec2d
        mouseCoordPos = coords.pos_to_coords(
            mousePosVec)  #Mouse pos in standardized coords as vec2d

        initVelocity = 0

        # --- Main event loop
        for event in pygame.event.get():
            if event.type == pygame.QUIT:  # If user clicked close
                done = True
            if event.type == pygame.MOUSEBUTTONDOWN:
                paused = True
                if mouseClicked == False:
                    mouseClicked = True
                    mousePosDown = mouseCoordPos  #wiil get the pos of Mouse at first click location
                    print("setting planet")
                    global newPlanet
                    newPlanet = CircleLine(Vec2d(0, 0), mousePosDown, 15)

                if mouseClicked == True:
                    mousePosUp = mouseCoordPos  #For drawing initial velocity vector
                    #draw a vector on screen here

            if event.type == pygame.MOUSEBUTTONUP:
                mouseClicked = False
                mousePosUp = mouseCoordPos

                initVelocity = mousePosUp - mousePosDown
                print("Vel", initVelocity.x, initVelocity.y)
                initVelocity *= 0.1
                print("Vel", initVelocity.x, initVelocity.y)

                initMom = initVelocity * newPlanet.mass
                newPlanet.vel = initVelocity
                newPlanet.mom = initMom
                planets.append(newPlanet)
                #will add new planet to the array of planets with vector of mouse up and mouse click
                paused = False

                print("Have to send planet in vector from center to mouse")

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    paused = not paused
                if event.key == pygame.K_0 or pygame.K_KP0:
                    com = ForceCalculator.calculateCOM(planets)
                    comVel = ForceCalculator.calculateCOMVel(planets)
                    for obj in planets:
                        obj.center -= com
                        obj.vel -= comVel
                        obj.mom = obj.vel * obj.mass
        #If the game is not paused, update the objects in it
        if (not paused):
            objLen = len(planets)
            for i in range(0, objLen):
                #                forces = []
                #                for j in range(0, objLen):
                #                    #Calculate a force that each object has on this object and append it to the list of foces acting on it
                #                    if(not i==j):
                #                        forces.append(ForceCalculator.calculateGravity(planets[i].center, planets[i].mass, planets[j].center, planets[j].mass, 10))
                #
                #                summationForce = ForceCalculator.sumForces(forces)  #Currently result of all gravitational forces from other planets
                summationForce = ForceCalculator.calculateGravity(
                    planets[i].center, planets[i].mass, Vec2d(0, -1000000000),
                    600000000000000000,
                    10)  #Gravity of "Earth" acting on this "ball"
                planets[i].update_force(summationForce)

            for i in range(0, objLen):
                planets[i].update(dt)

            #goes through the array of planets to see if they are colliding and if they are then, caclulate the collision
            for i in range(0, objLen):
                for j in range(0, objLen):
                    if (not i == j):
                        ForceCalculator.calculateCollision(
                            planets[i], planets[j])
                ForceCalculator.calculateWallCollision(planets[i], wall1)
                ForceCalculator.calculateWallCollision(planets[i], wall2)

            for i in range(0, objLen):
                planets[i].update(dt)

        screen.fill(BLACK)  # wipe the screen
        for obj in planets:
            obj.draw(screen, coords)  # draw object to screen
        wall1.draw(screen, coords)
        wall2.draw(screen, coords)

        textFont = pygame.font.Font(None, 72)
        mousePosSurface = textFont.render(
            "x: " + str(mouseCoordPos.x) + " y: " + str(mouseCoordPos.y), 0,
            WHITE)
        screen.blit(mousePosSurface, (500, 500))
        # --- Update the screen with what we've drawn.
        pygame.display.update()

        # This limits the loop to 60 frames per second
        clock.tick(frame_rate)

    pygame.quit()
def main():
    pygame.init()

    width = 800
    height = 600
    screen = pygame.display.set_mode([width, height])
    screen_center = Vec2d(width / 2, height / 2)
    coords = Coords(screen_center.copy(), 1, True)
    zoom = 100
    coords.zoom_at_coords(Vec2d(0, 0), zoom)

    # Used to manage how fast the screen updates
    clock = pygame.time.Clock()

    # Create initial objects to demonstrate
    objects = []
    points = (
        Vec2d(0, 0),
        Vec2d(1, 0),
        Vec2d(0, 1),
    )
    zero = Vec2d(0, 0)
    objects.append(Polygon(zero, zero, 1, make_polygon(1.5, 5, 0, 1.5), RED))
    objects.append(Polygon(zero, zero, 1, make_polygon(2, 3, 0, 0.5), BLUE))
    # -------- Main Program Loop -----------\
    seconds_per_frame = 0.5
    frame_rate = 1 / seconds_per_frame
    done = False
    paused = False
    step = True
    background_color = WHITE
    collided = False
    while not done:
        # --- Main event loop
        for event in pygame.event.get():
            if event.type == pygame.QUIT:  # If user clicked close
                done = True
                paused = True
            elif event.type == pygame.MOUSEBUTTONDOWN:
                paused = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    done = True
                    paused = True
                elif event.key == pygame.K_SPACE:
                    paused = not paused
                    step = False
                else:
                    paused = False
                    step = True

        if not paused:
            # If not overlapping, move objects to a new random position
            if not collided:
                for obj in objects:
                    x = uniform(width * 0.25, width * 0.75)
                    y = uniform(height * 0.25, height * 0.75)
                    obj.pos = coords.pos_to_coords(Vec2d(x, y))
                    obj.angle = uniform(0, degrees(360))
                    obj.update_points_normals()

            # Check for collision
            collided = False
            result = []
            for i1 in range(len(objects)):
                for i2 in range(i1):
                    if check_collision(objects[i1], objects[i2], result):
                        collided = True

            # Drawing
            if collided:
                background_color = YELLOW
            else:
                background_color = WHITE
            screen.fill(background_color)  # wipe the screen

            for obj in objects:
                obj.draw(screen, coords)  # draw object to screen

            if collided:
                (obj1, obj2, overlap, normal, point) = result
                #pygame.draw.circle(screen, BLACK, coords.pos_to_screen(point).int(), 5)
                #pygame.draw.line(screen, BLACK, coords.pos_to_screen(point).int(),
                #                 coords.pos_to_screen(point + overlap*normal).int())
                # Separate objects accoring to position
                """ Move obj1 overlap distance in the direction of normal. """
                obj1.pos += overlap * normal

        # --- Update the screen with what we've drawn.
        pygame.display.update()

        # This limits the loop to the specified frame rate
        if step:
            paused = True
            clock.tick(60)
        else:
            clock.tick(frame_rate)

    pygame.quit()
Example #3
0
def main():
    global objects, screen, coords, ballIndex
    
    pygame.init()
 
    width = 1200
    height = 600
    screen = pygame.display.set_mode([width,height])
    screen_center = Vec2d(width/2, height/2)
    coords = Coords(screen_center.copy(), 1, True)
    zoom = 100
    coords.zoom_at_coords(Vec2d(0,0), zoom) 
    
    # Used to manage how fast the screen updates
    clock = pygame.time.Clock()
    
    # Text font
    basic_font = pygame.font.Font('freesansbold.ttf', 24)
    large_font = pygame.font.Font('freesansbold.ttf', 48)
    
    # Pause Text
    intro_surf = large_font.render('READY', True, GREEN)
    intro_rect = intro_surf.get_rect()
    intro_rect.center = (width / 2, (height * 0.4))
    
    # Strokes text
    stroke_surf = basic_font.render('Stroke: 0', True, WHITE)
    stroke_rect = stroke_surf.get_rect()
    stroke_rect.center = (width / 2, (height * 0.05))
    
    # Stage 1 Strokes Text
    stage1_surf = basic_font.render('Stage 1: 0 hits', True, RED)
    stage1_rect = stage1_surf.get_rect()
    stage1_rect.center = (100, height - 20)
    
    # Stage 2 Strokes Text
    stage2_surf = basic_font.render('Stage 2: 0 hits', True, RED)
    stage2_rect = stage2_surf.get_rect()
    stage2_rect.center = (250, height - 20) 
    
    # Stage 3 Strokes Text
    stage3_surf = basic_font.render('Stage 3: 0 hits', True, RED)
    stage3_rect = stage3_surf.get_rect()
    stage3_rect.center = (400, height - 20)     

    # Create initial objects to demonstrate
    objects = []

    # Goal
    goalRadius = 0.12
    objects.append(Polygon(Vec2d(-5,2), Vec2d(0,0), 1, make_polygon(goalRadius,36,0,1), BLACK, 0, 0))
    objects[-1].type = "goal"
    
    # Golf ball
    objects.append(Polygon(Vec2d(5,2), Vec2d(0,0), 1, make_polygon(0.1,36,0,1), WHITE, 0, 0))
    ballIndex = len(objects) - 1
    
    # Surrounding walls
    objects.append(Wall(coords.pos_to_coords(Vec2d(0, screen.get_height())), Vec2d(0, 1), BLACK)) # Bottom
    objects.append(Wall(coords.pos_to_coords(Vec2d(0, 0)), Vec2d(0, -1), BLACK)) # Top
    objects.append(Wall(coords.pos_to_coords(Vec2d(0, 0)), Vec2d(1, 0), BLACK)) # Left
    objects.append(Wall(coords.pos_to_coords(Vec2d(screen.get_width(), 0)), Vec2d(-1, 0), BLACK)) # Right
    
    # Triangle walls
    triangle1 = Polygon(coords.pos_to_coords(Vec2d(screen.get_width() * 0.25, 0)), Vec2d(0,0), 999999, make_polygon(3,3,180,0.5), BLACK, 0, 0)
    triangle2 = Polygon(coords.pos_to_coords(Vec2d(screen.get_width() * 0.50, screen.get_height())), Vec2d(0,0), 999999, make_polygon(3,3,0,0.5), BLACK, 0, 0)
    triangle3 = Polygon(coords.pos_to_coords(Vec2d(screen.get_width() * 0.75, 0)), Vec2d(0,0), 999999, make_polygon(3,3,180,0.5), BLACK, 0, 0)
    
    triangle1.type = "obstacle"
    triangle2.type = "obstacle"
    triangle3.type = "obstacle"
    objects.append(triangle1)
    objects.append(triangle2)
    objects.append(triangle3)
    
    # Smaller spinning obstacle
    spinningLine1 = Polygon(coords.pos_to_coords(Vec2d(screen.get_width() * 0.5, screen.get_height() * 0.25)), Vec2d(0, 0), 5, make_rectangle(1, 0.1), DARKBLUE, 0, 1)
    spinningLine1.type = "obstacle"
    objects.append(spinningLine1)
    
    # Larger spinning obstacle
    spinningLine2 = Polygon(coords.pos_to_coords(Vec2d(screen.get_width() * 0.75, screen.get_height() * 0.75)), Vec2d(0, 0), 50, make_rectangle(2, 0.1), DARKBLUE, 0, 5)
    spinningLine2.type = "obstacle"
    objects.append(spinningLine2)
    
    # Small obstacles
    pentaObstacle = Polygon(coords.pos_to_coords(Vec2d(screen.get_width() * 0.2, screen.get_height() * 0.4)), Vec2d(0, 0), 1, make_polygon(0.1, 5, 0, 1), DARKBLUE, 0, 0)
    pentaObstacle.type = "obstacle"
    objects.append(pentaObstacle)
    pentaObstacle = Polygon(coords.pos_to_coords(Vec2d(screen.get_width() * 0.15, screen.get_height() * 0.4)), Vec2d(0, 0), 1, make_polygon(0.1, 5, 0, 1), DARKBLUE, 0, 0)
    pentaObstacle.type = "obstacle"
    objects.append(pentaObstacle)
    pentaObstacle = Polygon(coords.pos_to_coords(Vec2d(screen.get_width() * 0.1, screen.get_height() * 0.4)), Vec2d(0, 0), 1, make_polygon(0.1, 5, 0, 1), DARKBLUE, 0, 0)
    pentaObstacle.type = "obstacle"
    objects.append(pentaObstacle)
    pentaObstacle = Polygon(coords.pos_to_coords(Vec2d(screen.get_width() * 0.05, screen.get_height() * 0.4)), Vec2d(0, 0), 1, make_polygon(0.1, 5, 0, 1), DARKBLUE, 0, 0)
    pentaObstacle.type = "obstacle"
    objects.append(pentaObstacle)
    
    # -------- Main Program Loop -----------\
    frame_rate = 60
    n_per_frame = 10
    playback_speed = 1 # 1 is real time, 10 is 10x real speed, etc.
    dt = playback_speed/frame_rate/n_per_frame
    done = False
    paused = True
    draw = False
    goalScored = False
    max_collisions = 1
    result = []
    
    # Strokes
    strokeNumber = 0
    stage1Stroke = 0
    stage2Stroke = 0
    stage3Stroke = 0
    
    # Stage completion
    winStage1 = False
    winStage2 = False
    winStage3 = False
    
    while not done:
        for event in pygame.event.get(): 
            if event.type == pygame.QUIT: # If user clicked close
                done = True
                paused = True
            #elif event.type == pygame.MOUSEBUTTONDOWN:
                #paused = False
            elif event.type == pygame.KEYDOWN: 
                if event.key == pygame.K_ESCAPE:
                    done = True
                    paused = True 
                elif event.key == pygame.K_SPACE:
                    paused = not paused

            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    pos = pygame.mouse.get_pos()
                    if (objects[ballIndex].draw(screen, coords)).collidepoint(pos):
                        draw = True
            
            # Mouse Button Down Inputs
            if event.type == pygame.MOUSEBUTTONDOWN and paused:
                # Assign mouse buttons
                (pressed1, pressed2, pressed3) = pygame.mouse.get_pressed()
                
            elif event.type == pygame.MOUSEBUTTONUP and paused:
                # Assign mouse buttons
                (pressed1, pressed2, pressed3) = pygame.mouse.get_pressed()
                
                # Mouse left press to draw a particle
                if pressed1 == 0 and paused == True:                    
                    # The velocity line can be drawn
                    if draw:
                        strokeNumber += 1
                    draw = False
                    paused = False

        if not paused:
            # Stroke Text
            # If the polygon ball has stopped, allow the user to hit it again
            if(objects[ballIndex].vel.mag() <= 0.05):
                paused = True
            for N in range(n_per_frame):
                # Physics
                # Calculate the force on each object
                for obj in objects:
                    obj.force.zero()
                    obj.force += -1 * obj.vel * obj.mass
                    
                    # Slow down the ball
                    if obj.type == "polygon":
                        obj.torque = -0.75 * obj.angvel
                        pass

                # Move each object according to physics
                for obj in objects:
                    obj.update(dt)
                    
                for i in range(max_collisions):
                    collided = False
                    for i1 in range(len(objects)):
                        for i2 in range(i1):
                            if check_collision(objects[i1], objects[i2], result):
                                resolve_collision(result)
                                collided = True
                    if not collided: # if all collisions resolved, then we're done
                        break
                
                # Check distance between ball and goal
                goalDistanceVec = objects[0].pos - objects[ballIndex].pos
                goalDistance = goalDistanceVec.mag()
                if goalDistance < goalRadius * 0.95 and objects[ballIndex].vel.mag() < 3:
                    objects[ballIndex].pos = Vec2d(5,2) # set ball position to start position
                    stage1Stroke = strokeNumber
                    strokeNumber = 0
                    winStage1 = True
            
            # Drawing
            screen.fill(WHITE) # wipe the screen
            for y in range (0, 10):
                for x in range (0, 10):
                    screen.blit(GRASS, (x * 346, y * 346))
                    
            for obj in objects:
                obj.draw(screen, coords) # draw object to screen
                
            # Show stroke text
            stroke_surf = basic_font.render('Stroke: ' + str(strokeNumber), True, WHITE)
            screen.blit(stroke_surf, stroke_rect)
            
            # Show each stages' stroke text
            if(winStage1):
                stage1_surf = basic_font.render('Stage 1: ' + str(stage1Stroke), True, WHITE)
                screen.blit(stage1_surf, stage1_rect)
            if(winStage2):
                stage2_surf = basic_font.render('Stage 2: ' + str(stage2Stroke), True, WHITE)
                screen.blit(stage2_surf, stage2_rect)
            if(winStage3):
                stage3_surf = basic_font.render('Stage 3: ' + str(stage3Stroke), True, WHITE)
                screen.blit(stage3_surf, stage3_rect)
            
            # --- Update the screen with what we've drawn.
            pygame.display.update()
            
            # This limits the loop to the specified frame rate
            clock.tick(frame_rate)
        elif paused:
            for N in range(n_per_frame):
                # Physics
                # Move each object according to physics
                for obj in objects:
                    if obj != objects[ballIndex]:
                        obj.update(dt)
            # Drawing
            screen.fill(WHITE) # wipe the screen
            for y in range (0, 10):
                for x in range (0, 10):
                    screen.blit(GRASS, (x * 346, y * 346))
            
            for obj in objects:
                obj.draw(screen, coords) # draw object to screen
            if draw:
                objects[ballIndex].set_vel(make_velocity_line() / 35)
            
            # Show the pause text
            screen.blit(intro_surf, intro_rect)
            
            # Show stroke text
            stroke_surf = basic_font.render('Stroke: ' + str(strokeNumber), True, WHITE)
            screen.blit(stroke_surf, stroke_rect)
            
           # Show each stages' stroke text
            if(winStage1):
                stage1_surf = basic_font.render('Stage 1: ' + str(stage1Stroke), True, WHITE)
                screen.blit(stage1_surf, stage1_rect)
            if(winStage2):
                stage2_surf = basic_font.render('Stage 2: ' + str(stage2Stroke), True, WHITE)
                screen.blit(stage2_surf, stage2_rect)
            if(winStage3):
                stage3_surf = basic_font.render('Stage 3: ' + str(stage3Stroke), True, WHITE)
                screen.blit(stage3_surf, stage3_rect)
    
            # --- Update the screen with what we've drawn.
            pygame.display.update()
            
            # This limits the loop to the specified frame rate
            clock.tick(frame_rate)

    pygame.quit()