我已经把木乃伊编程到我的游戏中,我想让他们跟着我的玩家。我想做一个函数,我可以用它来应用于我选择创建的所有敌人,但我有一个不是很有效。木乃伊会产卵,但他们不会跟着玩家。如何更改函数以使其工作?在
import random
import pygame
import sys
import time
from pygame import mixer
screenx = 800
screeny = 600
clock = pygame.time.Clock()
#PICTURES
#name = pygame.display.set_caption('Brutal Survival')
#win = pygame.display.set_mode((screenx,screeny))
#player_right = pygame.image.load('maincharright.png')
#player_left = pygame.image.load('maincharleft.png')
#player_up = pygame.image.load('maincharup.png')
#player_down = pygame.image.load('mainchardown.png')
#background = pygame.image.load('background.jpg')
#mummychar = pygame.image.load('mummychar.png')
randomspawnabove = (random.randint(0, screenx), -100)
randomspawnbelow = (random.randint(0, screenx), (screeny + 100))
randomspawnleft = (-100, random.randint(0, screeny))
randomspawnright = ((screenx + 100), random.randint(0, screeny))
enemyalive = True
multiplier = 2
level = 1
nextlevel = True
spawn = True
up = True
down = False
left = False
right = False
run = True
class player():
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.alive = True
self.vel = 5
class enemy():
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.alive = False
self.vel = 2
def enemyfollow(self):
if self.alive == True and self.x > mainChar.x:
self.x = self.x - self.vel
if self.alive == True and self.x < mainChar.x:
self.x = self.x + self.vel
if self.alive == True and self.y > mainChar.y:
self.y = self.y - self.vel
if self.alive == True and self.y < mainChar.y:
self.y = self.y + self.vel
def redrawScreen():
pygame.display.update()
if run == True:
win.blit(background, (0,0))
if up == True and left == False and right == False and down == False:
win.blit(player_up, (mainChar.x, mainChar.y))
if up == False and left == False and right == False and down == True:
win.blit(player_down, (mainChar.x, mainChar.y))
if up == False and left == False and right == True and down == False:
win.blit(player_right, (mainChar.x, mainChar.y))
if up == False and left == True and right == False and down == False:
win.blit(player_left, (mainChar.x, mainChar.y))
if enemyalive == True:
win.blit(mummychar, (mummy1.x, mummy1.y))
enemyfollow(mummy1)
mainChar = player(screenx/2 - 30, screeny/2, 60, 60)
maxenemies = (level * multiplier)
mummy_Spawn = [randomspawnleft, randomspawnright, randomspawnabove, randomspawnbelow]
mummy1 = enemy(*random.choice(mummy_Spawn), 50, 50)
while run:
clock.tick(60)
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
for x in range(maxenemies):
Enemies.append(enemy(*random.choice(mummy_Spawn), 50, 50))
if nextlevel == True:
if level >= 1:
enemyalive = True
if keys[pygame.K_ESCAPE]:
run = False
pygame.quit()
if keys[pygame.K_UP]:
mainChar.y = mainChar.y - mainChar.vel
up = True
down = False
left = False
right = False
if keys[pygame.K_DOWN]:
mainChar.y = mainChar.y + mainChar.vel
down = True
left = False
up = False
right = False
if keys[pygame.K_RIGHT]:
mainChar.x = mainChar.x + mainChar.vel
right = True
left = False
up = False
down = False
if keys[pygame.K_LEFT]:
mainChar.x = mainChar.x - mainChar.vel
left = True
right = False
down = False
up = False
if (mainChar.y + mainChar.height) > screeny:
mainChar.y = mainChar.y - 5
if mainChar.y < 0:
mainChar.y = mainChar.y + 5
if mainChar.x < 0:
mainChar.x = mainChar.x + 5
if (mainChar.x + mainChar.width) > screenx:
mainChar.x = mainChar.x - 5
redrawScreen()
此版本的代码可以工作。我认为主要的问题是
enemy.alive
默认为False
的组合,我不确定重新绘制的顺序。在因为我必须用我自己的一些位图来替换位图,所以我对常量大小的参数做了进一步的修改——尤其是50x50的大小,但是还有其他一些参数。在
我花了不少时间才对我有用,而且我有点迷路了。在
if语句的一部分是if}是False
self == True
,但是{所以木乃伊需要跟随玩家的目标。进一步完善此问题描述:
那我们怎么做呢?在
想象一下,我们从木乃伊的位置画一支箭给玩家。这将确定木乃伊应该移动的方向。在形式(数学)术语中,这是一个方向向量。为了求出方向向量,我们求出两点之间的直线,然后用它的长度除以直线的长度使之正规化。在
结果我们得到了一个简单的
x
和y
单位向量,它们的分量可以乘以物体的速度来获得它的下一个位置。在在下面的例子中,有3个精灵将向鼠标的位置移动。重要的部分是
MySprite.update()
函数。这将获取鼠标位置(在您的例子中,这是玩家)和精灵的位置(木乃伊位置),使用这两点来计算向量。一旦确定了矢量,分量将与精灵的速度一起用于计算下一个位置。我把位置保持在内部作为一个浮动,这样可以更好地处理小角度的变化和低速。在任何PNG图像都可以替换
tiny_alien.png
。在gif的画面有点不自然,但有点不自然
相关问题 更多 >
编程相关推荐