我最近部署了一个敌人,该敌人会定期在屏幕上的指定点射击。但是,在尝试使玩家适应这一点时,它拒绝工作。由于敌人类别是在房间模块中定义的,然后在主游戏模块中定义的,因此我不确定如何在敌人模块中称呼玩家rect。
工作代码如下:
import pygame
from constants import *
from player import Player
from pygame.math import Vector2
from enemy import *
from Rooms import Room0
pygame.init()
screen_rect = pygame.display.set_mode([500, 500])
pygame.display.set_caption('Labyrinth')
all_sprites_list = pygame.sprite.Group()
projectiles = pygame.sprite.Group()
enemy_sprites = pygame.sprite.Group()
# Assign rooms
rooms = []
room = Room0()
rooms.append(room)
current_room_no = 0
current_room = rooms[current_room_no]
# Spawn player
player = Player(50, 50)
all_sprites_list.add(player)
clock = pygame.time.Clock()
done = False
# ----- Event Loop
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# ----- Game Logic
all_sprites_list.update()
current_room.projectiles.update()
current_room.enemy_sprites.update()
screen_rect.fill(GREEN)
all_sprites_list.draw(screen_rect)
current_room.projectiles.draw(screen_rect)
current_room.enemy_sprites.draw(screen_rect)
pygame.display.flip()
clock.tick(60)
pygame.quit()
import pygame
from enemy import Enemy
import Projectile
from pygame.math import Vector2
class Room(object):
enemy_sprites = None
projectiles = None
def __init__(self):
self.enemy_sprites = pygame.sprite.Group()
self.projectiles = pygame.sprite.Group()
class Room0(Room):
def __init__(self):
super().__init__()
enemy = Enemy(380, 280, self.projectiles)
self.enemy_sprites.add(enemy)
from constants import *
import pygame
class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
self.image = pygame.Surface([15, 15])
self.image.fill(BLACK)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
from constants import *
import pygame
from Projectile import Bullet
from pygame.math import Vector2
target = Vector2(400, 400)
class Enemy(pygame.sprite.Sprite):
def __init__(self, x, y, projectiles):
super().__init__()
self.image = pygame.Surface([10, 10])
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.previous_time = pygame.time.get_ticks()
self.shoot_delay = 1000
self.speed = 12
self.projectiles = projectiles
def update(self):
now = pygame.time.get_ticks()
if now - self.previous_time > self.shoot_delay:
self.previous_time = now
bullet = Bullet(self.rect.x, self.rect.y, target)
self.projectiles.add(bullet)
import pygame
from constants import *
from pygame.math import Vector2
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y, target):
super().__init__()
self.image = pygame.Surface((10, 10))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.position = Vector2(self.rect.x, self.rect.y)
direction = target - self.position
radius, angle = direction.as_polar()
self.image = pygame.transform.rotozoom(self.image, -angle, 1)
self.velocity = direction.normalize() * 11
def update(self):
self.position += self.velocity
self.rect.center = self.position
答案 0 :(得分:2)
有一个您想要的答案,一个您需要的答案。分别:
SomeModule.py:
from game import player # imports the Player *instance* you've created in the main module
some_func(player) # as argument
player.another_func # method access
现在,这就是您通常访问此类内容的方式,这将是非常好的。不过在这种情况下:
a)您将产生一个全新的游戏循环,因为您将游戏设置置于模块作用域而不是某个功能中,或者至少将其置于if __name__ == '__main__'
之下。导入模块将执行模块范围内的所有代码。
b)您必须直接导入非单个 instance 的事实是代码异味-这个问题的存在应该向您传达的信号是您可能有一个位置您的代码段必须能够彼此对话,但是您没有明确负责调解该过程。
因此,要解决我答应的答案的第二部分:首先不要让这个问题发生-构建专门用于管理玩家和敌人的东西,仅导入类定义,然后实例和它们在管理器之间的接口。