Jump to content
  • Advertisement
Sign in to follow this  
TristanAnselT.Angeles

Can someone help me with this?

This topic is 2566 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

I'm trying this out. Basically, what I want to happen is make the square move and stop where I clicked. So far here's what I've done)got some of the code(like the vector class from a book btw):

import pygame,os,sys,math
from pygame.locals import*

white=(255,255,255)
green=(0,255,0)
blue=(0,0,255)
red=(255,0,0)
black=(0,0,0)


class Creep:

def __init__(self,x,y):
self.x=x
self.y=y
self.clicked=0 # creep is not activate
self.speed=100
self.location=(self.x,self.y)
self.image=pygame.draw.rect(screen,white,[self.x,self.y,10,10])
self.image.center=(self.x,self.y)




def draw(self):
self.x=position.x
self.y=position.y
self.image=pygame.draw.rect(screen,white,[self.x,self.y,10,10])
self.image.center=(self.x,self.y)




class Vector2(object):

def __init__(self,x=0,y=0):
self.x=x
self.y=y

def __str__(self):
return "(%s,%s)"%(self.x,self.y)

@classmethod
def from_points(cls,p1,p2):
return cls( p2[0]-p1[0],p2[1]-p1[1])

def get_magnitude(self):
return math.sqrt(self.x**2+self.y**2)

def normalize(self):
magnitude=self.get_magnitude()
self.x/=magnitude
self.y/=magnitude

def __add__(self,rhs):
return Vector2(self.x+rhs.x,self.y+rhs.y)

def __sub__(self,rhs):
return Vector2(self.x-rhs.x,self.y-rhs.y)

def __neg__(self):
return Vector2(-self.x,-self.y)

def __mul__(self,scalar):
return Vector2(self.x*scalar,self.y*scalar)

def __div__(self,scalar):
return Vector2(self.x*scalar,self.y*scalar)







pygame.init()

screen=pygame.display.set_mode((640,480))
clock=pygame.time.Clock()
creep1=Creep(320.,240.)
position=Vector2(creep1.x,creep1.y)
heading=Vector2()

final=None

while True:

for event in pygame.event.get():
if event.type==KEYDOWN:
if event.key==K_ESCAPE:
pygame.quit()
sys.exit()

if event.type==MOUSEBUTTONDOWN:
destination=Vector2(*event.pos)
final=(destination.x,destination.y)
heading=Vector2.from_points((position.x,position.y),(destination.x,destination.y))
heading.normalize()


time_passed=clock.tick()
time_passed_seconds=time_passed/1000.


distance_moved=creep1.speed*time_passed_seconds

screen.fill(black)
creep1.draw()
if creep1.image.center!=final:
position+=heading*distance_moved







pygame.display.flip()

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!