python播放器模块_如何在单独的模块中调用播放器



我最近实施了一个敌人,该敌人会定期在屏幕上的指定点射击。但是,在尝试使玩家适应这一点时,它拒绝工作。由于敌人类别是在房间模块中定义的,然后在主游戏模块中定义的,因此我不确定如何在敌人模块中称呼玩家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

解决方案

There’s an answer you want, and an answer you need. Respectively:

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

Now, that’s the way you’d normally access that kind of stuff, and this would be perfectly fine. In this case though:

a) You’ll spawn a whole new game loop, because you put the game setup at module scope rather than in some function or, at very least, directly under an if name == ‘main‘. Importing a module executes all the code in the module scope.

b) The very fact you have to import a non-singleton instance, directly, is a code smell – what the very existence of this problem should signal to you is that you likely have a place where your bits of code must be able talk to each other, but you have nothing unambiguously responsible for mediating that process.

So, to address the second part of my promised answer: you should not let this problem occur in the first place – build something dedicated to managing players and enemies, import just the class definitions, then instance and interface between them in the manager.

Original: https://blog.csdn.net/weixin_33642922/article/details/112900721
Author: 林桂鑫
Title: python播放器模块_如何在单独的模块中调用播放器

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/783468/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球