在pygame中创建玩家类并在游戏主模块中使用

问题描述 投票:0回答:1

我在主游戏的单独文件中创建了一个玩家类,该类具有 x y 速度、颜色大小属性 我创建了一个名为“渲染”的函数,它在玩家类文件内部使用给定的这些属性绘制一个矩形,但是当我收到错误时,我很难在主文件中使用这个渲染函数来渲染正方形

Traceback (most recent call last):
  File "c:\Users\leeun\OneDrive\Desktop\Python\NEA\mainmenu.py", line 3, in <module>
    from players import Player
  File "c:\Users\leeun\OneDrive\Desktop\Python\NEA\players.py", line 2, in <module>
    from mainmenu import screen
  File "c:\Users\leeun\OneDrive\Desktop\Python\NEA\mainmenu.py", line 3, in <module>
    from players import Player
ImportError: cannot import name 'Player' from partially initialized module 'players' (most likely due to a circular import) (c:\Users\leeun\OneDrive\Desktop\Python\NEA\players.py)

我的主要代码是

import pygame
import button
from players import Player

pygame.init()

# screen size variables
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

# defining screen size/name
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Start Menu")

# game states
game_paused = False
menu_state = "main"

# text font
font = pygame.font.SysFont("arialblack", 40)
font2 = pygame.font.SysFont("arialblack", 10)
font_header = pygame.font.SysFont("arialblack", 60)

# text colour
TEXT_COL = (255, 255, 255)
TEXT_COL2 = (0, 0, 0)

# loading / setting buttons
play_btn = pygame.image.load("NEA/images/button_play.png").convert_alpha()
quit_btn = pygame.image.load("NEA/images/button_quit.png").convert_alpha()
leaderboard_btn = pygame.image.load("NEA/images/button_leaderboard.png").convert_alpha()
back_btn = pygame.image.load("NEA/images/button_back.png").convert_alpha()

leaderboard_btn = button.Button(264, 300, leaderboard_btn, 1)
quit_btn = button.Button(304, 400, quit_btn, 1)
play_btn = button.Button(304, 200, play_btn, 1)
back_btn = button.Button(25, 25, back_btn, 1)



def draw_text(text, font, text_col, x, y):
    img = font.render(text, True, text_col)
    screen.blit(img, (x, y))

# runs the game
run = True
game_menu = True
player = Player(250, 250, 0.1, (0, 0, 255), 20)

while run:
    
    screen.fill((79, 101, 176))
    
    # main screen
    if game_menu == True:
        
        # check game state
        if menu_state == "main":

            # display the game name
            draw_text("Worlds Easiest Game", font_header, TEXT_COL, 50, 75)
            
            # draw main menu buttons
            if play_btn.draw(screen):
                game_menu = False
            if leaderboard_btn.draw(screen):
                menu_state = "leaderboard"
            if quit_btn.draw(screen):
                run = False
                
        # check if leaderboard is open
        if menu_state == "leaderboard":
            
            # display the leaderboard
            draw_text("Leaderboard display", font, TEXT_COL, 160, 250)
            
            # return back to main menu
            if back_btn.draw(screen):
                menu_state = "main"
    
    # the main game screen, this will include the player, level objects
    else:
        screen.fill((255, 255, 255))
        draw_text("Press ESC to pause", font2, TEXT_COL2, 25, 25)
        # this is where i want to render the square 
    
    for event in pygame.event.get():
        
        
        # checks if ESC has been pressed to go to main menu
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                game_menu = True
                
        # quits the game
        if event.type == pygame.QUIT:
            run = False
    
    pygame.display.update()

pygame.quit()    

我的玩家等级是

import pygame
from mainmenu import screen

class Player():
    def __init__(self, x, y, speed, colour, size):
        self.x = x
        self.y = y
        self.speed = speed
        self.colour = colour
        self.size = size
        
        
    def render(self):
        pygame.draw.rect(screen, self.colour, (self.x, self.y, self.size, self.size))
        
    def up(self):
        self.y = self.y - self.speed
        
    def down(self):
        self.y = self.y + self.speed
        
    def left(self):
        self.x = self.x - self.speed
        
    def right(self):
        self.x = self.x + self.speed

我尝试过放

player = Player(250, 250, 0.1, (0, 0, 255), 20)

在主游戏循环开始之前,然后在我的代码中放置注释的位置使用渲染函数,但我只是得到了所描述的错误。

python oop pygame
1个回答
0
投票

我认为你的问题是循环导入。您正在从主类和播放器导入“屏幕”。但随后在“player”类中,您还从“main”类导入“screen”,从而创建循环依赖关系。

您需要删除

from mainmenu import screen
来自您的“玩家”类别。

一般来说,将游戏逻辑和绘图分离到单独的类/文件中是一个好主意。这样你就可以避免这些循环依赖问题。

© www.soinside.com 2019 - 2024. All rights reserved.