简体   繁体   English

如何获取图像以在pygame中追逐我的角色?

[英]How to get images to chase my character in pygame?

I would like to create a simple game were you move a base image and a bunch of zombie images spawn and chase you. 我想创建一个简单的游戏,如果您移动基本图像,然后生成一堆僵尸图像并追逐您。 The other answers for this dident seem to help. 这样做的其他答案似乎有所帮助。 Can someone give me something easy to understand that will help me acheieve this 有人可以给我一些容易理解的东西来帮助我实现这一目标

Here is my code. 这是我的代码。 Though the indentation is off, I can never copy it right: 尽管缩进不可用,但我永远无法正确复制它:

import pygame,sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((894, 894))
pygame.display.set_caption('Zombie Survival')
grumpy = pygame.image.load('grumpy.jpg')
grumpy_x = 800
grumpy_y = 100
grumpy = pygame.transform.scale(grumpy, (50, 50))
background  = pygame.image.load('grass.jpg').convert()
while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
            if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_RIGHT:
                            grumpy_x = grumpy_x + 20
            if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_LEFT:
                            grumpy_x = grumpy_x - 20
            if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_UP:
                            grumpy_y = grumpy_y - 20
            if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_DOWN:
                            grumpy_y = grumpy_y + 20
    screen.blit(background, (0, 0))
    screen.blit(grumpy, (grumpy_x, grumpy_y))
    pygame.display.update()

Ok, lets break it down a bit: 好吧,让我们分解一下:

a bunch of zombie images spawn 一堆僵尸图像生成

We need a list of zombies coordinates then, so we know where to draw the zombies. 然后,我们需要一个僵尸坐标列表,因此我们知道在哪里绘制僵尸。

zombies = []

Then we make the zombies have an initial position somewhere random. 然后我们使僵尸的初始位置随机。

for _ in range(10):
    new_zombie = (random.randrange(SCREENX), random.randrange(SCREENY))
    zombies.append(new_zombie)

We want to be able to draw the zombies as well. 我们也希望能够绘制僵尸。

for zombie in zombies:
    screen.blit(zombieimg, zombie)

Now lets look at the other part, updating the zombie positions. 现在,让我们看看另一部分, 更新僵尸位置。

To move our zombies, we want to shift them by some amount in the direction of the player. 要移动僵尸,我们想将它们向玩家的方向移动一些。

ZOMBIE_SPEED = 3

def update(zombie):
    x, y = zombie
    if grumpy_x > x:
        x += ZOMBIE_SPEED
    else:
        x -= ZOMBIE_SPEED
    if grumpy_y > y:
        y += ZOMBIE_SPEED
    else:
        y -= ZOMBIE_SPEED
    return x, y

zombies = [update(zombie) for zombie in zombies]

In other words, at each time step, we want to move each zombie towards a player by some amount. 换句话说,在每个时间步骤中,我们都希望将每个僵尸向玩家移动一定量。


Warning: This is probably the easiest way to do it, because it glosses over some of the complexity you will face later on. 警告:这可能是最简单的方法,因为它掩盖了您稍后将要面对的一些复杂性。 You should learn about objects and classes and how to use them in the context of pygame sprites before you get overwhelmed. 在不知所措之前,您应该了解对象和类以及如何在pygame精灵的上下文中使用它们。

Keeping track of zombies and players and bullets and everything else individually will be painful, and it is much easier to just track sprites once your game gets complex enough. 跟踪僵尸,玩家和子弹以及其他所有东西都会很痛苦,一旦游戏变得足够复杂,仅跟踪精灵就容易得多。

You should start thinking the game before going around a code it, what you have doesn't look good to me, any modularity for the game just will cause your work to be harder... Try creating a class for the zombie, and make and update() function with the behaviour you want them to do (in this case seeking for the main character), they should have an image and x and y position as well as your player; 您应该在编写代码之前就开始思考游戏,您觉得这对我来说不好,游戏的任何模块化都将使您的工作变得更加艰辛...尝试为僵尸创建一个类,并制作一个和update()函数具有您希望他们执行的行为(在本例中为寻找主要角色),他们应该具有图像,x和y位置以及您的播放器; transform that x and y coordinates so the zoombie get closer to the current position of the player. 转换x和y坐标,使缩放对象更靠近播放器的当前位置。

class Zombie:
    def __init__(self, initX, initY, image = "default.png"):
        self.x = initX
        self.y = initY
        self.image = pygame.image.load(image)

    def update(self, playerX, playerY):
         ##stuff to get to player and update the zombie position

generate the zombies: zombiLst = [] for i in range(10): zombiLst.append(Zombie(100,100, "zombie.png")) 为范围(10)中的i生成僵尸:zombiLst = []:zombiLst.append(Zombie(100,100,“ zombie.png”))

Then in your loop: 然后在您的循环中:

while True:
    ##your stuff here
    ##update the zombies
    for i in zombiLst.length():
        zombiLst[i].update()

something like this should do. 这样的事情应该做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM