我正在用 pygame 制作一个小游戏,但到目前为止我只能创建玩家,现在我想添加另一个类,该类应该跟随玩家,如果它接触玩家一段时间设置玩家死亡的时间。我想了解如何为我的其他项目做到这一点,所以我更喜欢为 pygame 初学者提供更通用的答案。
我已经尝试创建基于此类的类。:
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()
这显然不起作用,然后我放弃了,因为我能想到的一切都超出了我对 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)
BGColor = (117,168,55)
RED = (255,0,0)
BLUE = (0,0,255)
BLACK = (0,0,0)
MOVE = 2.5
size = (1200, 620)
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 moveRight(self, pixels):
self.rect.x += pixels
pass
def moveLeft(self, pixels):
self.rect.x -= pixels
pass
def moveUp(self, pixels):
self.rect.y -= pixels
pass
def moveDown(self, pixels):
self.rect.y += pixels
pass
all_sprites_list = pygame.sprite.Group()
playerChar = Char(BLUE, [0, 0], 30, 0)
playerChar.rect.x = 0
playerChar.rect.y = 0
all_sprites_list.add(playerChar)
carryOn = True
clock = pygame.time.Clock()
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.moveLeft(MOVE)
if keys[pygame.K_d]:
playerChar.moveRight(MOVE)
if keys[pygame.K_w]:
playerChar.moveUp(MOVE)
if keys[pygame.K_s]:
playerChar.moveDown(MOVE)
screen.fill(BGColor)
screen.blit(playerChar.image,playerChar.rect)
pygame.display.flip()
clock.tick(60)
pygame.quit()
最佳答案
要找到问题的根源,我们需要获取更多信息。无法“让第二个类(class)出现”可能是一百万种不同的事情。为了把这个变成一个问题,我可以帮助你做一些测试。
我建议的第一个是为你的类创建几个实例。例如:
playerChar1 = Char(BLUE, [0, 0], 30, 0)
playerChar2 = Char(BLUE, [0, 30], 30, 0)
playerChar3 = Char(BLUE, [30, 0], 30, 0)
这会让 3 个角色出现在不同的位置吗?如果不是,也许您正在绘制除最新角色之外的所有角色?
如果您可以创建 Char 类的多个实例,接下来我们需要第二个类。我们将从您的 char 类开始,并将其重命名为 Char2
:
class Char2(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 moveRight(self, pixels):
self.rect.x += pixels
pass
def moveLeft(self, pixels):
self.rect.x -= pixels
pass
def moveUp(self, pixels):
self.rect.y -= pixels
pass
def moveDown(self, pixels):
self.rect.y += pixels
pass
里面到处都是同样的东西。就像以前一样,你可以创建这个类的实例吗?它们会出现在屏幕上吗?
最后,如果第二个类出现,您需要做的就是调整第二个类中的逻辑,直到它像您想要的僵尸一样工作。
关于python - 如何添加另一个类(class)并使其跟随玩家? (在 PyGame 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54711504/