0

这是我的脚本,一切正常,当按键被击中并且外星人跟随玩家时,角色可以四处移动wasd,但是外星人向玩家倾斜移动,但如果玩家移动,外星人只会移动过去玩家即使他的位置不符合我设置的条件,外星人也只会在玩家击键时回到航线上。

import pygame, sys, random, time, math
from pygame.locals import *
pygame.init()

bifl = 'screeing.jpg'
milf = 'char_fowed_walk1.png'
alien = 'alien_1.png'

screen = pygame.display.set_mode((640, 480))
background = pygame.image.load(bifl).convert()
mouse_c = pygame.image.load(milf).convert_alpha()
nPc = pygame.image.load(alien).convert_alpha()

x, y = 0, 0
movex, movey = 0, 0

z, w = random.randint(10, 480), random.randint(10, 640)
movexz, movew = 0, 0

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        if event.type == KEYDOWN:
            if event.key == K_w:
                movey = - 0.3
            elif event.key == K_s:
                movey = + 0.3
            elif event.key == K_a:
                movex = - 0.3
            elif event.key == K_d:
                movex = + 0.3

        if event.type == KEYUP:
             if event.key == K_w:
                movey = 0
             elif event.key == K_s:
                movey = 0
             elif event.key == K_a:
                movex = 0
             elif event.key == K_d:
                movex = 0

        if w < x:
            movew =+ 0.2
        if w > x:
            movew =- 0.2
        if z < y:
            movez =+ 0.2
        if z > y:
            movez =- 0.2

    x += movex
    y += movey
    w += movew
    z += movez
    print(x, y)






    screen.blit(background, (0, 0))
    screen.blit(mouse_c, (x, y))
    screen.blit(nPc, (w, z))

    pygame.display.update()
4

1 回答 1

1

您的代码仅在某些情况发生时更新外星人的运动方向event,因此它总是会等待某些用户操作才能“转身”。

如果您从内部循环中获得外星人“AI”更新,一切都应该恢复正常

    if w < x:
        movew =+ 0.2
    if w > x:
        movew =- 0.2
    if z < y:
        movez =+ 0.2
    if z > y:
        movez =- 0.2

x += movex
y += movey
w += movew
z += movez

应该看起来像

if w < x:
    movew =+ 0.2
if w > x:
    movew =- 0.2
if z < y:
    movez =+ 0.2
if z > y:
    movez =- 0.2

x += movex
y += movey
w += movew
z += movez
于 2013-09-11T10:46:44.100 回答