我如何从游戏屏幕的每一个角落制造敌人?

2024-09-27 09:33:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在为学校编写一些游戏代码,并通过一些教程学习如何让玩家精灵跟踪鼠标,但现在我很难弄清楚如何从屏幕的四面八方而不是顶部滋生敌人。(澄清代码分为两部分/文件,也完全无关)。我还想知道,我怎么可能让怪物/NPC追逐并旋转到玩家身上

import random
import math
import pygame as py
from PlayerSprite import *

WIDTH = 800
HEIGHT = 600
FPS = 60

# define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# initialize pygame and create window
py.init()
py.mixer.init()
screen = py.display.set_mode((WIDTH, HEIGHT))
py.display.set_caption("Dimensional Drifter")
clock = py.time.Clock()

all_sprites = py.sprite.Group()
NPCs = py.sprite.Group()
player = Player()
all_sprites.add(player)
for i in range(8):
    n = NPC()
    all_sprites.add(n)
    NPC.add(n)
# Game loop
running = True
while running:
    # keep loop running at the right speed
    clock.tick(FPS)

    for event in py.event.get():
        # check for closing window
        if event.type == py.QUIT:
            running = False

    # Update
    all_sprites.update()
    #
    mouse_x, mouse_y = py.mouse.get_pos()
    player.rotate(mouse_x, mouse_y)
    # render
    screen.fill(BLACK)
    all_sprites.draw(screen)
    # flip the display
    py.display.flip()

py.quit()
import pygame as py
import math
import random

WIDTH = 800
HEIGHT = 600
FPS = 60

# define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)


class Player(py.sprite.Sprite):
    def __init__(self):
        py.sprite.Sprite.__init__(self)
        self.image = py.Surface((50, 50), py.SRCALPHA)
        self.image.fill(GREEN)
        self.rect = self.image.get_rect()
        self.rect.centerx = WIDTH / 2
        self.rect.bottom = HEIGHT / 2
        self.Yspeed = 0
        self.rotatableimage = self.image

    def update(self):
        self.Xspeed = 0
        self.Yspeed = 0
        # line below allow for key press to equate to move of sprite
        keypreesed = py.key.get_pressed()
        if keypreesed[py.K_a]:
            self.Xspeed = - 11
        if keypreesed[py.K_d]:
            self.Xspeed = 11
        if keypreesed[py.K_w]:
            self.Yspeed = - 11
        if keypreesed[py.K_s]:
            self.Yspeed = 11
        self.rect.x += self.Xspeed
        self.rect.y += self.Yspeed
        # line below allow the sprite to wrap around the screen
        if self.rect.left > WIDTH:
            self.rect.right = 0
        if self.rect.right < 0:
            self.rect.left = WIDTH
        if self.rect.top > HEIGHT:
            self.rect.top = 0
        if self.rect.bottom < 0:
            self.rect.bottom = HEIGHT

    def rotate(self, mouse_x, mouse_y):
        rel_x = mouse_x - self.rect.x
        rel_y = mouse_y - self.rect.y
        angle = (180 / math.pi) * -math.atan2(rel_y, rel_x)

        self.image = py.transform.rotate(self.rotatableimage, int(angle))
        self.rect = self.image.get_rect(center=(self.rect.centerx, self.rect.centery))
        return


class NPC(py.sprite.Sprite):
    def __init__(self):
        py.sprite.Sprite.__init__(self)
        self.image = py.Surface((30, 30))
        self.image.fill(RED)
        self.rect = self.image.get_rect()
        self.rect.x = random.randrange(WIDTH - self.rect.width)
        self.rect.y = random.randrange(-100, -40)
        self.Yspeed = random.randrange(1, 8)

    def update(self):
        self.rect.y += self.Yspeed
        if self.rect.top > HEIGHT + 10:
            self.rect.x = random.randrange(WIDTH - self.rect.width)
            self.rect.y = random.randrange(-100, -40)
            self.Yspeed = random.randrange(1, 8)

提前谢谢


Tags: pyrectimageimportselfgetifinit
1条回答
网友
1楼 · 发布于 2024-09-27 09:33:41

必须为x和y方向添加一个随机方向和一个单独的速度(self.Xspeedself.Yspeed):

self.direction = random.randrange(4)

以及根据随机方向设置self.rect.xself.rect.yself.Xspeedself.Yspeed

if self.direction == 0:
    self.rect.x = random.randrange(WIDTH - self.rect.width)
    self.rect.y = random.randrange(-100, -40)
    self.Xspeed = 0
    self.Yspeed = random.randrange(1, 8)
elif self.direction == 1:
    # [...]
elif self.direction == 2:
    # [...]
elif self.direction == 3:
    # [...]

创建一个方法spawn,该方法生成一个具有随机方向和随机速度的敌人,并在NPC的构造函数中调用它:

class NPC(py.sprite.Sprite):
    def __init__(self):
        # [...]        

        self.spawn()

    def spawn(self):
        self.direction = random.randrange(4)
        if self.direction == 0:
            # [...]

update中,速度必须分别为和到x和y位置:

self.rect.x += self.Xspeed
self.rect.y += self.Yspeed

此外,如果对象不在窗口中,则必须根据direction计算。如果对象不在窗口中,则再次调用spawn(),因此对象将获得一个新的随机方向、位置和速度:

if self.direction == 0:
    if self.rect.top > HEIGHT + 10:
        self.spawn()
elif self.direction == 1:
    if self.rect.bottom < -10:
        self.spawn()
elif self.direction == 2:
    if self.rect.left > WIDTH + 10:
        self.spawn()
elif self.direction == 3:
    if self.rect.right < -10:
        self.spawn()

要旋转图像,必须使用每像素alpha(^{})创建曲面,并保持原始曲面:

self.image = py.Surface((30, 30)).convert_alpha()
self.image.fill(RED)
self.originalimage = self.image  

计算敌人与玩家之间的角度,并将图像旋转^{}: (另见How to rotate a triangle to a certain angle in pygame?

dir_x, dir_y = player.rect.x - self.rect.x, player.rect.y - self.rect.y
self.rot = (180 / math.pi) * math.atan2(-dir_x, -dir_y)
self.image = py.transform.rotate(self.originalimage, self.rot)

NPC

class NPC(py.sprite.Sprite):
    def __init__(self, player):
        py.sprite.Sprite.__init__(self)
        self.player = player 
        self.image = py.Surface((30, 30)).convert_alpha()
        self.image.fill(RED)
        self.originalimage = self.image
        self.rect = self.image.get_rect()
        self.spawn()

    def spawn(self):
        self.direction = random.randrange(4)
        if self.direction == 0:
            self.rect.x = random.randrange(WIDTH - self.rect.width)
            self.rect.y = random.randrange(-100, -40)
            self.Xspeed = 0
            self.Yspeed = random.randrange(1, 8)
        elif self.direction == 1:
            self.rect.x = random.randrange(WIDTH - self.rect.width)
            self.rect.y = random.randrange(HEIGHT, HEIGHT+60)
            self.Xspeed = 0
            self.Yspeed = -random.randrange(1, 8)
        elif self.direction == 2:
            self.rect.x = random.randrange(-100, -40)
            self.rect.y = random.randrange(HEIGHT - self.rect.height)
            self.Xspeed = random.randrange(1, 8)
            self.Yspeed = 0
        elif self.direction == 3:
            self.rect.x = random.randrange(WIDTH, WIDTH+60)
            self.rect.y = random.randrange(HEIGHT - self.rect.height)
            self.Xspeed = -random.randrange(1, 8)
            self.Yspeed = 0

    def update(self):
        self.rect.x += self.Xspeed
        self.rect.y += self.Yspeed

        dir_x, dir_y = self.player.rect.x - self.rect.x, self.player.rect.y - self.rect.y
        self.rot = (180 / math.pi) * math.atan2(-dir_x, -dir_y)
        self.image = py.transform.rotate(self.originalimage, self.rot)

        if self.direction == 0:
            if self.rect.top > HEIGHT + 10:
                self.spawn()
        elif self.direction == 1:
            if self.rect.bottom < -10:
                self.spawn()
        elif self.direction == 2:
            if self.rect.left > WIDTH + 10:
                self.spawn()
        elif self.direction == 3:
            if self.rect.right < -10:
                self.spawn()
player = Player()
all_sprites.add(player)
for i in range(8):
    n = NPC(player)
    all_sprites.add(n)
    NPC.add(n)

相关问题 更多 >

    热门问题