Jump to content
  • Advertisement
Sign in to follow this  
bacon

Need help with movement

This topic is 4847 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, first time post here. I'm writing an isometric game in Python using pygame (SDL bindings for python). I need to have my unit move in an exact straight line. So when I click the mouse somewhere, I want the unit to move there, at a certain speed held in a variable. However, I'm having a lot of problems. I have tried a few different ways, none of them work exactly. I need it to move in a straight line in a speed defined by me. I think what I have to do is to calculate the distance between the start and end (which is what nx and ny are right now), then calculate a line, and then decide which pixels intersect with that line, then store them in an array, then in my main loop move the image to another pixel on the line. Even if you don't know python, just help me with the concept, that is what I'm having trouble with. I tried searching, but I didn't come up with anything. Mods if this has been done or is in the wrong forum, please move or lock. Here's my current code:
import pygame
import sys
pygame.init()

size = window_width, window_height = 320, 240
black = 0, 0, 0

screen = pygame.display.set_mode(size)

ball = pygame.image.load("ball.png")
ballrect = ball.get_rect()
height = ballrect.bottom
width = ballrect.right

pos_start = ballrect.left, ballrect.top
pos_end = pygame.mouse.get_pos()

clock = pygame.time.Clock()
move = 0
m = 0.0
nx = 1.0
ny = 1.0
x = 0
y = 0

while 1:
    clock.tick(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()

        elif event.type == pygame.MOUSEBUTTONDOWN:
            pos_start = ballrect.left, ballrect.top
            pos_end = pygame.mouse.get_pos()
            nx = 1.0 * (pos_end[0] - pos_start[0] - width / 2)
            ny = 1.0 * (pos_end[1] - pos_start[1] - height / 2)
            if nx != 0:
                m=ny/nx
            else:
                m=0
            x = 0
            y = 0
            step=1
            move=1
    if move == 1:
        if nx < 0 and ny > 0:   #m is negative
            x = x - 1
            y = m*x+y
            if abs(y) >= 1:
                ystep= 1
                y=0
            else:
                ystep = 0
            ballrect = ballrect.move(-1*step, ystep)  
        elif nx > 0 and ny > 0 : #m is positive
            x = x + 1
            y=m*x+y
            if abs(y) >= 1:
                ystep= 1
                y=0
            else:
                ystep = 0
            ballrect = ballrect.move(step,ystep)
        elif nx > 0 and ny < 0 : #m is negative
            x = x + 1
            y=m*x+y
            if abs(y) >= 1:
                ystep= 1
                y=0
            else:
                ystep = 0
            ballrect = ballrect.move(step,-1*ystep)
        elif nx < 0 and ny < 0 : #m is positive
            x = x - 1
            y=m*x+y
            if abs(y) >= 1:
                ystep= 1
                y=0
            else:
                ystep = 0
            ballrect = ballrect.move(-1*step,-1*ystep)
        print "x=", x , "y=", y
    if x == nx:
        x = 0
        y = 0
        move = 0
        
    screen.fill(black)
    screen.blit(ball, ballrect)
    pygame.display.flip()
As you can see I'm still using the ball from the pygame intro. I'm lazy :-D Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!