python - Pygame - 创建 "Enemy"类,然后将其导入游戏

标签 python class import pygame

好吧,基本上我想做的是保持主文件更干净一些,我从“僵尸”敌人开始,制作它自己的文件,该文件很可能包含所有敌人,并将其导入.

所以我对如何为 Sprite 设置类感到困惑,你不必告诉我如何让它移动或类似的东西,我只是想让它简单地出现。当我按原样运行时,游戏不会中断,我只是想在 sleep 前问这个问题,这样我就有希望在明天完成的项目中完成很多工作(与学校相关)

代码未完成,就像我说的,只是想在我 sleep 时问一下,只需进行一些谷歌搜索和尝试。

最终,我也会采纳此处给出的建议来制作“英雄”类,并且如果有时间的话,还会导入其他因素。

僵尸代码:

import pygame
from pygame.locals import *

class ZombieEnemy(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('images/zombie.png')
#       self.images.append(img)
        # self.image = self.images[0]
        self.rect = self.image.get_rect()

zombieX = 100
zombieY = 340
zombieX_change = 0

主要代码:

import pygame
from pygame.locals import *
import Zombie
# Intialize the pygame
pygame.init()

# Create the screen 
screen = pygame.display.set_mode((900, 567))


#Title and Icon
pygame.display.set_caption("Fighting Game")

# Add's logo to the window 
# icon = pygame.image.load('')
# pygame.display.set_icon(icon)

# Player
playerImg = pygame.image.load('images/character.png')
playerX = 100
playerY = 340
playerX_change = 0

def player(x,y):
    screen.blit(playerImg,(x,y))

Zombie.ZombieEnemy()

def zombie(x,y):
    screen.blit()

# Background

class Background(pygame.sprite.Sprite):
    def __init__(self, image_file, location):
        pygame.sprite.Sprite.__init__(self)  #call Sprite initializer
        self.image = pygame.image.load('images/background.png')
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = location

BackGround = Background('background.png', [0,0])

#  Game Loop
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    # If keystroke is pressed check right, left.
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            #playerX_change = -2.0
            BackGround.rect.left = BackGround.rect.left + 2.5
        if event.key == pygame.K_RIGHT:
            #playerX_change = 2.0
            BackGround.rect.left = BackGround.rect.left - 2.5
    # if event.type == pygame.KEYUP:
    #     if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
    #         BackGround.rect.left = 0

    screen.blit(BackGround.image, BackGround.rect)
    playerX += playerX_change
    player(playerX,playerY)
    pygame.display.flip()

最佳答案

你的 Sprite 代码基本上已经存在了。但正如你所说,它需要一个 update() 函数来以某种方式移动 Sprite 。

Sprites in PyGame 的想法是将它们添加到 SpriteGroup ,然后使用组功能来一起处理 Sprite 。

您可能想要修改 Zombie 类以获取初始坐标位置:

class ZombieEnemy(pygame.sprite.Sprite):
    def __init__( self, x=0, y=0 ):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('images/zombie.png')
        self.rect = self.image.get_rect()
        self.rect.center = ( x, y )           # NOTE: centred on the co-ords

这允许游戏在特定起点(甚至可能是随机起点)创建僵尸。

因此,要拥有一个 Sprite 组,首先需要创建容器:

all_zombies = pygame.sprite.Group()

然后当你创建一个新的僵尸时,将其添加到组中。假设您想从 3 个随机放置的僵尸开始:

for i in range( 3 ):
    new_x = random.randrange( 0, WINDOW_WIDTH )       # random x-position
    new_y = random.randrange( 0, WINDOW_HEIGHT )      # random y-position
    all_zombies.add( Zombie( new_x, new_y ) )         # create, and add to group

然后在主循环中,对 Sprite 组调用.update().draw()。这将移动并绘制添加到组中的所有 Sprite 。通过这种方式,您可以拥有单独的敌人、子弹、背景项目等组。 Sprite 组允许轻松在其他组之间进行绘制和碰撞检测。想象一下用一百颗子弹与一千个敌人相撞!

while running:
    for event in pygame.event.get():
        # ... handle events

    # Move anything that needs to
    all_zombies.update()                 # call the update() of all zombie sprites
    playerX += playerX_change

    # Draw everything
    screen.blit(BackGround.image, BackGround.rect)
    player(playerX,playerY)                          
    all_zombies.draw( screen )           # paint every sprite in the group

    pygame.display.flip()

编辑:向 all_zombies.draw() 添加了 screen 参数

也可能值得将您的玩家定义为 Sprite ,并拥有 single-entry group也为了它。

关于python - Pygame - 创建 "Enemy"类,然后将其导入游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59889471/

相关文章:

python - 计算两个字符串之间的差异

r - 为什么当 class(a$alpha) 返回 "-none-"时 summary(a$alpha) 返回 class "numeric"?

php - $class->method() 或 $class::method()

python - ImportError : attempted relative import with no known parent package :(

javascript - 关于 javascript 导入的问题。为什么将这些导入为常量?

python - 有什么方法可以开发 Python GUI 来控制 LabVIEW VI?

python - 如何在Python中的主线程继续工作的情况下在单独的线程中实现阻塞监听功能

python - 为什么admin.autodiscover()在使用admin时在Django中没有自动调用,为什么它被设计成显式调用?

swift - 如果没有更多上下文,表达式类型不明确 - CoreBluetooth

java - 在Java中导入LinkedList报错