python播放器模块_如何在单独的模块中调用播放器
发布日期:2021-06-24 15:50:13 浏览次数:2 分类:技术文章

本文共 4209 字,大约阅读时间需要 14 分钟。

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

转载地址:https://blog.csdn.net/weixin_33642922/article/details/112900721 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:java吕局部峰值_JVM的运行数据区划分
下一篇:发那科机器人没有码垛指令_FANUC 机器人码垛编程详细讲解

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年04月19日 05时33分25秒