Jacob Lawson Jacob Lawson - 1 month ago 10
Python Question

how can i get an object to move with the mouse horizontally but has a set y position in python

im trying to replicate the spaceship in the arcade game galaga. the spaceship needs to move slower than the mouse but still follows it. this is what i have so far and im not sure where to go from here. please help
keep in mind im new to programming so im sure this is hard to look at.

import pygame
import math
pygame.display.init()
pygame.mixer.init()
screen = pygame.display.set_mode((800, 600))

#fireSnd = pygame.mixer.Sound()
#pygame.mixer.music.load()
#pygame.mixer.music.play(-1)
x1 = 400
x2 = 405
x3 = 395
point1 = (x1,585)
point2 =(x2,600)
point3 = (x3,600)
SpaceShip = (point1,point2,point3)
Hdir = 0

done = False

while not done:
# Step1. Erase the screen
screen.fill((0, 0, 0))
# Step2. Update

# Step3. Process variables
event = pygame.event.poll()
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
done = True
elif event.type == pygame.MOUSEMOTION:
x,y = event.pos
# Step4. Drawing
pygame.draw.polygon(screen, (255,0,0) ,(SpaceShip), 0)
pygame.display.flip()



pygame.display.quit()

Answer

Just have your ship move left at x rate if the mouse is left of it, and right at x rate if the mouse is to the right of it.

To get mouse position:

mousePos = pygame.mouse.get_pos()

And you may want to change the way that you draw your spaceship. Right now you have designated a triangle with point1, point2, and point3. It doesn't change. Let's do something like this:

spaceshipPos = [400]

Spaceship = ((spaceshipPos[0],585),(spaceshipPos[0] + 5,600),(spaceshipPos[0] - 5,600))

This just takes the spaceship's position and give the location of each of the triangle's points.

while True: # game loop

    if spaceshipPos[0] > mousePos[0]:  # if the mouse is to the left, move left
        spaceshipPos[0] -= 5
    elif spaceshipPos[0] < mousePos[1]:  # if the mouse is to the right, move right
        spaceshipPos[0] += 5

    pygame.draw.polygon(screen, (255,0,0), Spaceship, 0)

So your full code would be:

import pygame
import math
pygame.display.init()
pygame.mixer.init()
screen = pygame.display.set_mode((800, 600))

#fireSnd = pygame.mixer.Sound()
#pygame.mixer.music.load()
#pygame.mixer.music.play(-1)

Hdir = 0

moveRate = 1

spaceshipPos = [400]

done = False

while not done:

    screen.fill((0, 0, 0))

    event = pygame.event.poll()
    if event.type == pygame.QUIT:
        done = True
    elif event.type == pygame.KEYDOWN:
        if event.key == pygame.K_ESCAPE:
            done = True

    mousePos = pygame.mouse.get_pos()

    if spaceshipPos[0] > mousePos[0]:  # if the mouse is to the left, move left
        spaceshipPos[0] -= moveRate

    elif spaceshipPos[0] < mousePos[0]:  # if the mouse is to the right, move right
        spaceshipPos[0] += moveRate

    elif spaceshipPos[0] == mousePos[0]:
        pass

    Spaceship = ((spaceshipPos[0],585),(spaceshipPos[0] + 5,600),(spaceshipPos[0] - 5,600))
    pygame.draw.polygon(screen, (255,0,0) ,(Spaceship), 0)

    pygame.display.flip()



pygame.display.quit()