pygame没有更新屏幕和敌人的AI移动过去的玩家虽然没有达到条件

时间:2013-09-11 10:38:04

标签: python artificial-intelligence pygame

这是我的剧本,一切都有效,当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()

1 个答案:

答案 0 :(得分: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