Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎯 Unreal

Need help with movement

Started by bacon Aug 6, 2005 at 4:16 PM 0 replies 350+ views
Original Post
bacon
bacon
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!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.