【问题标题】:pygame not updating the screen and enemy AI moving past player though conditons aren't metpygame 不更新屏幕和敌人 AI 移动过去的玩家虽然条件不满足
【发布时间】:2013-09-12 18:34:53
【问题描述】:

这是我的脚本,一切正常,当按下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()

【问题讨论】:

    标签: python artificial-intelligence pygame


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-11
      • 1970-01-01
      • 2023-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-05
      • 1970-01-01
      相关资源
      最近更新 更多