敌人没有跟随玩家(pygame)

时间:2019-02-17 15:22:55

标签: python pygame

所以我遵循了在StackOverflow上提出的另一个问题的答案,但似乎我错过了一些东西。阅读答案后,我继续进行操作,并复制了代码并将其调整为我的变量和类名。

以下是空闲给我的错误代码:

Traceback (most recent call last):
  File "D:\Programme (x86)\Python\Games\Zombie Game\Zombie Game_Test1.py", line 133, in <module>
  Zombie.move_towards_Char(Char)
TypeError: move_towards_Char() missing 1 required positional argument: 'Char'

这是我看过的地方: How to make an enemy follow the player in pygame?

import pygame
import turtle
import time
import math
import random
import sys
import os
pygame.init()

WHITE = (255,255,255)
GREEN = (0,255,0)
RED = (255,0,0)
BLUE = (0,0,255)
BLACK = (0,0,0)

BGColor = (96,128,56)
ZColor = (225,0,0)
PColor = (0,0,255)

MOVE = 2.5

size = (1920, 1080)

screen = pygame.display.set_mode(size)
pygame.display.set_caption("Zombie Game")

class Char(pygame.sprite.Sprite):
    def __init__(self, color, pos, radius, width):
        super().__init__()
        self.image = pygame.Surface([radius*2, radius*2])
        self.image.fill(WHITE)
        self.image.set_colorkey(WHITE)
        pygame.draw.circle(self.image, color, [radius, radius], radius, width)
        self.rect = self.image.get_rect()

    def moveRightP(self, pixels):
        self.rect.x += pixels
        pass

    def moveLeftP(self, pixels):
        self.rect.x -= pixels
        pass

    def moveUpP(self, pixels):
        self.rect.y -= pixels
        pass

    def moveDownP(self, pixels):
        self.rect.y += pixels
        pass


class Zombie(pygame.sprite.Sprite):
    def __init__(self2, color, pos, radius, width):
        super().__init__()
        self2.image = pygame.Surface([radius*2, radius*2])
        self2.image.fill(WHITE)
        self2.image.set_colorkey(WHITE)
        pygame.draw.circle(self2.image, color, [radius, radius], radius, width)
        self2.rect = self2.image.get_rect()
        self2.rect.center = pos

    def move_towards_Char(self2, Char):
        dx, dy = self2.rect.x - Char.rect.x, self2.rect.y - Char.rect.y
        dist = math.hypot(dx, dy)
        dx, dy = dx / dist, dy / dist
        self2.rect.x += dx * self2.speed
        self2.rect.y += dy * self2.speed


    def moveRightZ(self2, pixels):
        self2.rect.x += pixels
        pass

    def moveLeftZ(self2, pixels):
        self2.rect.x -= pixels
        pass

    def moveUpZ(self2, pixels):
        self2.rect.y -= pixels
        pass

    def moveDownZ(self2, pixels):
        self2.rect.y += pixels
        pass


all_sprites_list = pygame.sprite.Group()

playerChar = Char(PColor, [0, 0], 15, 0)
playerChar.rect.x = 960
playerChar.rect.y = 505

all_sprites_list.add(playerChar)

carryOn = True
clock = pygame.time.Clock()

zombie_list = []
zombie_rad = 15   
zombie_dist = (200, 900)
next_zombie_time = pygame.time.get_ticks() + 10000

zombie_list = []
zombie_rad = 15   
zombie_dist = (200, 900)
next_zombie_time = 10000

while carryOn:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            carryOn=False
        elif event.type==pygame.KEYDOWN:
            if event.key==pygame.K_x:
                carryOn=False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_a]:
        playerChar.moveLeftP(MOVE)
    if keys[pygame.K_d]:
        playerChar.moveRightP(MOVE)
    if keys[pygame.K_w]:
        playerChar.moveUpP(MOVE)
    if keys[pygame.K_s]:
        playerChar.moveDownP(MOVE)

    current_time = pygame.time.get_ticks()
    if current_time > next_zombie_time:
        next_zombie_time = current_time + 2000

        on_screen_rect = pygame.Rect(zombie_rad, zombie_rad, size[0]-2*zombie_rad, size[1]-2*zombie_rad)
        zombie_pos = (-1, -1)
        while not on_screen_rect.collidepoint(zombie_pos):
            dist  = random.randint(*zombie_dist)
            angle = random.random() * math.pi * 2 
            p_pos = (playerChar.rect.centerx, playerChar.rect.centery)
            zombie_pos = (p_pos[0] + dist * math.sin(angle), p_pos[1] + dist * math.cos(angle))

        new_pos = (random.randrange(0, size[0]), random.randrange(0, size[1]))
        new_zombie = Zombie(RED, zombie_pos, zombie_rad, 0)
        zombie_list.append(new_zombie)

    screen.fill(BGColor)    
    screen.blit(playerChar.image,playerChar.rect)   
    for zombie in zombie_list:
        screen.blit(zombie.image,zombie.rect)      

    pygame.display.flip()    
    clock.tick(60)    
pygame.quit()

2 个答案:

答案 0 :(得分:1)

L {Zombie.move_towards_Char}是一种自我方法。您需要创建Zombie类的对象,并传递L {Zombie。 init }中提到的必需参数。

如下所示:

zm = Zombie(color=<color>, pos=<pos>, radius=<radius>, width=<width>)
zm.move_towards_Char(Char)

答案 1 :(得分:0)

主要问题是,您使用整数数据类型进行僵尸移动计算。如果僵尸的移动量为1个像素,并且移动量为对角线,则该移动量的x和y分量<1。使用整数数据类型,由于截断为 {{ 1}} 。请注意,int的成员是整数值。

您必须切换到浮点值才能解决此问题。使用pygame.math.Vector2进行计算。

将类型为pygame.Rect的成员pos添加到类Vector2中,该成员存储僵尸的浮点位置:

Zombie

向类class Zombie(pygame.sprite.Sprite): def __init__(self2, color, pos, radius, width): super().__init__() self2.image = pygame.Surface([radius*2, radius*2]) self2.image.fill(WHITE) self2.image.set_colorkey(WHITE) pygame.draw.circle(self2.image, color, [radius, radius], radius, width) self2.rect = self2.image.get_rect() self2.speed = 1 self2.pos = pygame.Vector2(pos[0], pos[1]) # [...] 中添加一个新方法draw,该方法在位置Zombie上绘制(blit)僵尸:

pos

根据class Zombie(pygame.sprite.Sprite): # [...] def draw(self2): self2.rect.center = (int(round(self2.pos.x)), int(round(self2.pos.y))) screen.blit(self2.image, self2.rect) 计算僵尸的运动。确保播放器与僵尸之间的距离大于0,并且僵尸不会越过播放器的位置(Vector2):

min(len, self2.speed)

在应用程序的主循环中,为每个僵尸调用方法class Zombie(pygame.sprite.Sprite): # [...] def move_towards_Char(self2, Char): deltaVec = pygame.Vector2(Char.rect.center) - self2.pos len = deltaVec.length() if len > 0: self2.pos += deltaVec/len * min(len, self2.speed) move_towards_Char

draw

相关问题