我想通過調用該方法和我想移動它的距離來移動一定距離的字符。到目前爲止,我可以通過調用函數並放置我的x和y值來確定字符位置。當用戶點擊一個鼠標。在主要方法中,我希望移動方法被調用。我試過兩種移動實現,第一步移動和第二步move_c,都沒有工作。我使用python3和pygame通過一種方法移動一個字符 - python
有什麼建議嗎?
:
繼承人我的代碼
import pygame, math
from pygame.locals import *
pygame.init()
class Vector():
'''
creates operations to handle vectors such
as direction, position, and speed
'''
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self): # used for printing vectors
return "(%s, %s)"%(self.x, self.y)
def __getitem__(self, key):
if key == 0:
return self.x
elif key == 1:
return self.y
else:
raise IndexError("This "+str(key)+" key is not a vector key!")
def __sub__(self, o): # subtraction
return Vector(self.x - o.x, self.y - o.y)
def length(self): # get length (used for normalize)
return math.sqrt((self.x**2 + self.y**2))
def normalize(self): # divides a vector by its length
l = self.length()
if l != 0:
return (self.x/l, self.y/l)
return None
class Sprite(pygame.sprite.Sprite):
def __init__(self):
'''
Class:
creates a sprite
Parameters:
- self
'''
self.image = pygame.image.load("Images/green.png").convert() # load image
self.rect = self.image.get_rect()
self.speed = 10 # movement speed of the sprite
self.speedX = 0 # speed in x direction
self.speedY = 0 # speed in y direction
self.target = None # starts off with no target
def set_position(self, x,y):
self.rect.centerx= x
self.rect.centery= y
self.rect.center=(self.rect.centery, self.rect.centery)
def get_position(self):
return self.rect.center
def move(self, distance):
if self.target:
direction =self.get_direction(self.target)
position = self.get_position() # create a vector from center x,y value
direction = Vector(direction[0], direction[1]) # and one from the target x,y
distance = target - position # get total distance between target and position
return distance
def get_direction(self, target):
'''
Function:
takes total distance from sprite.center
to the sprites target
(gets direction to move)
Returns:
a normalized vector
Parameters:
- self
- target
x,y coordinates of the sprites target
can be any x,y coorinate pair in
brackets [x,y]
or parentheses (x,y)
'''
if self.target: # if the square has a target
position = Vector(self.rect.centerx, self.rect.centery) # create a vector from center x,y value
target = Vector(target[0], target[1]) # and one from the target x,y
self.dist = target - position # get total distance between target and position
direction = self.dist.normalize() # normalize so its constant in all directions
return direction
def distance_check(self, dist):
'''
Function:
tests if the total distance from the
sprite to the target is smaller than the
ammount of distance that would be normal
for the sprite to travel
(this lets the sprite know if it needs
to slow down. we want it to slow
down before it gets to it's target)
Returns:
bool
Parameters:
- self
- dist
this is the total distance from the
sprite to the target
can be any x,y value pair in
brackets [x,y]
or parentheses (x,y)
'''
dist_x = dist[0] ** 2 # gets absolute value of the x distance
dist_y = dist[1] ** 2 # gets absolute value of the y distance
t_dist = dist_x + dist_y # gets total absolute value distance
speed = self.speed ** 2 # gets aboslute value of the speed
if t_dist < (speed): # read function description above
return True
def update(self):
'''
Function:
gets direction to move then applies
the distance to the sprite.center
()
Parameters:
- self
'''
self.dir = self.get_direction(self.target) # get direction
if self.dir: # if there is a direction to move
if self.distance_check(self.dist): # if we need to stop
self.rect.center = self.target # center the sprite on the target
else: # if we need to move normal
self.rect.centerx += (self.dir[0] * self.speed) # calculate speed from direction to move and speed constant
self.rect.centery += (self.dir[1] * self.speed)
self.rect.center = (round(self.rect.centerx),round(self.rect.centery)) # apply values to sprite.center
def move_c(self,distance):
self.dir = self.get_direction(self.target) # get direction
if self.dir: # if there is a direction to move
if self.distance_check(self.dist): # if we need to stop
self.rect.center = self.target # center the sprite on the target
else: # if we need to move normal
self.rect.centerx += (self.dir[0] * self.speed) # calculate speed from direction to move and speed constant
self.rect.centery += (self.dir[1] * self.speed)
self.rect.center = (round(self.rect.centerx),round(self.rect.centery)) # apply values to sprite.center
return distance
def main():
screen = pygame.display.set_mode((640,480))
pygame.display.set_caption("Test game")
background_color = pygame.Surface(screen.get_size()).convert()
background_color.fill((240,50,0))
## line_points = [] # make a list for points
## line_color = (0, 255, 255) # color of the lines
sprite = Sprite() # create the sprite
clock = pygame.time.Clock()
sprite.set_position(100,400)
running = True
while running:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == MOUSEBUTTONDOWN:
sprite.move(100)
## sprite.move_c(150)
## sprite.target = event.pos # set the sprite.target to the mouse click position
screen.blit(background_color, (0,0))
## sprite.update() # update the sprite
screen.blit(sprite.image, sprite.rect.topleft) # blit the sprite to the screen
pygame.display.flip()
pygame.quit() # for a smooth quit
if __name__ == "__main__":
main()
這裏是移動的方法之一,我給予的距離,我想移動精靈移動精靈。我得到當前位置的精靈方向。通過位置計算距離i減去目標並返回位置。但是,當我調用這個函數:
sprite.move(50):
精靈不動彈更不用說50
def move(self, distance):
if self.target:
direction =self.get_direction(self.target)
position = self.get_position() # create a vector from center x,y value
direction = Vector(direction[0], direction[1]) # and one from the target x,y
distance = target - position # get total distance between target and position
return distance
究竟不起作用? – Mailerdaimon
也許嘗試縮小你的問題。究竟你的最新方法沒有工作?代碼導致問題的部分是什麼? – Lycha
一個矩形的移動,它現在不移動,即使通過一個距離 –