2014-02-21 148 views
0

我正在努力爭取這項工作。基本上,我希望我的敵方精靈追逐我的玩家精靈。此刻,它沿着對角線移動到屏幕的右下角。無法使敵人向玩家移動

我正在學習python我做了一個遊戲,我仍然是新的,所以我很抱歉,如果這是一個非常簡單的事情,我遇到了問題。

相關代碼:

class Enemy(pygame.sprite.Sprite): 
    def __init__(self): 
     pygame.sprite.Sprite.__init__(self) 

     self.bitmap = pygame.image.load("ball.bmp") 
     self.enemy_rect = self.bitmap.get_rect() 
     self.enemy_rect.topleft = [100, 200] 
     self.speed = 2 

    def move_to_player(self, Player): 
     dx, dy = self.enemy_rect.x - player.player_rect.x, self.enemy_rect.y - player.player_rect.y 
     dist = math.hypot(dx, dy) 
     if dist == 0: 
      dist = 1 
     else: 
      dx, dy = dx/dist, dy/dist 

     self.enemy_rect.x += dx * self.speed 
     self.enemy_rect.y += dx * self.speed 

while 1: # main game loop 
enemy.move_to_player(player) 

而且所有的代碼,用於上下文:

import pygame 
import sys 
import pygame.locals 
import math 

pygame.init() 

size = width, height = 800, 500 
colour = 100, 20, 30 # colour for background 

screen = pygame.display.set_mode(size) 
pygame.display.set_caption('Test') 

class Player(pygame.sprite.Sprite): 
    def __init__(self): 
     pygame.sprite.Sprite.__init__(self) 

     self.bitmap = pygame.image.load("ball.bmp") 
     self.player_rect = self.bitmap.get_rect(
     self.player_rect.topleft = [100, 200] 

    def move(self, x, y): 
     self.player_rect.centerx += x 
     self.player_rect.centery += y 

    def render(self): 
     screen.blit(self.bitmap, (self.shipRect)) 

class Enemy(pygame.sprite.Sprite): 
    def __init__(self): 
     pygame.sprite.Sprite.__init__(self) 

     self.bitmap = pygame.image.load("ball.bmp") 
     self.enemy_rect = self.bitmap.get_rect() 
     self.enemy_rect.topleft = [100, 200] 
     self.speed = 2 

    def move_to_player(self, Player): 
     dx, dy = self.enemy_rect.x - player.player_rect.x, self.enemy_rect.y - player.player_rect.y 
     dist = math.hypot(dx, dy) 
     if dist == 0: 
      dist = 1 
     else: 
      dx, dy = dx/dist, dy/dist 

     self.enemy_rect.x += dx * self.speed 
     self.enemy_rect.y += dx * self.speed 

player = Player() 
enemy = Enemy() 

player.player_rect.x = 50 
player.player_rect.y = 50 

enemy.enemy_rect.x = 500 
enemy.enemy_rect.y = 300 

while 1: # main game loop 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      sys.exit() 

     if event.type == pygame.KEYDOWN: 
      if event.key == pygame.K_DOWN: 
       player.move(0, 5) 
      if event.key == pygame.K_UP: 
       player.move(0, -5) 
      if event.key == pygame.K_LEFT: 
       player.move(-5, 0) 
      if event.key == pygame.K_RIGHT: 
       player.move(5, 0) 

     if player.player_rect.colliderect(enemy.enemy_rect): 
      pygame.display.set_caption('Hit!') 

    screen.fill(colour) 
    screen.blit(player.bitmap, player.player_rect) 
    screen.blit(enemy.bitmap, enemy.enemy_rect) 
    enemy.move_to_player(player) 
    pygame.display.flip() 

回答

2

首先,self.enemy_rect.y += dx * self.speed大概應該是self.enemy_rect.y += dy * self.speed

其次,我覺得你有你的數學向後這樣你的敵人會逃跑。讓我們嘗試一個簡單的例子:

Player at 0,0; enemy at 2,0. 
Expected result if speed = 2: both at 0,0 
dx = 2, dy = 0 (before normalizing by hypot) 
dx = 1, dy = 0 (after) 
New result: player still at 0,0; enemy at 4,0 
+0

啊哈,謝謝!我的數學倒退了,是的。現在完美運作。 – PurpleMontart

0

這裏,這是我用這個...

if PlayerX > AI_X: 
    AI_X += 3 
if PlayerX < AI_X: 
    AI_X -= 3 
if PlayerY > AI_Y: 
    AI_Y += 3 
if PlayerY < AI_Y: 
    AI_Y -= 3 

希望這有助於!