为Python游戏创建库存

发布于 2025-02-04 17:58:24 字数 3880 浏览 4 评论 0原文

因此,我尝试为Python Game Dodger创建库存。在库存中,我希望能够选择不同的玩家玩游戏。但是,当我运行游戏时,只选择选项image/player.png。我该如何解决?谢谢 这就是我到目前为止尝试的: //inventory.py

import pygame
import pygwidgets
import pyghelpers
import random
from Player import Player
import Constants
from Constants import *

class Inventory(pyghelpers.Scene):

    def __init__(self, window):
        self.window = window
        self.player = Player(window)
        self.player0 = pygwidgets.CustomButton(self.window, (30, 250), 'images/player_inv.png')
        self.player1 = pygwidgets.CustomButton(self.window, (280, 250), 'images/player1_inv.jpg')
        self.player2 = pygwidgets.CustomButton(self.window, (30, 450), 'images/char1_inv.png')
        self.inventory = []
        self.image = pygwidgets.Image(self.window,(0, 0),'images/inventory.png')

        self.quitButton = pygwidgets.CustomButton(self.window,
                                                  (30, 650),
                                                  up='images/quitNormal.png',
                                                  down='images/quitDown.png',
                                                  over='images/quitOver.png',
                                                  disabled='images/quitDisabled.png')

        self.backButton = pygwidgets.CustomButton(self.window,
                                                  (240, 650),
                                                  up='images/backNormal.png',
                                                  down='images/backDown.png',
                                                  over='images/backOver.png',
                                                  disabled='images/backDisabled.png')

    def getSceneKey(self):
        return SCENE_INVENTORY

    def enter(self, data):
        pass

    def handleInputs(self, eventsList, keyPressedList):
        for event in eventsList:
            if self.quitButton.handleEvent(event):
                self.quit()

            elif self.backButton.handleEvent(event):
                self.goToScene(Constants.SCENE_PLAY)

            if self.player0.handleEvent(event):
                self.player.imagelink = 'images/player.png'
                self.goToScene(Constants.SCENE_PLAY)

            elif self.player1.handleEvent(event):
                self.player.imagelink = 'images/player1.jpg'
                self.goToScene(Constants.SCENE_PLAY)

            elif self.player2.handleEvent(event):
                self.player.imagelink = 'images/player2.png'
                self.goToScene(Constants.SCENE_PLAY)

    def update(self):
        pass

    def draw(self):
        self.image.draw()
        self.quitButton.draw()
        self.backButton.draw()
        self.player0.draw()
        self.player1.draw()
        self.player2.draw()

//player.py

import pygame
import pygwidgets
from Constants import *

class Player():
    def __init__(self, window):
        self.window = window
        self.imagelink = 'images/player.png'
        self.image = pygwidgets.Image(window,
                                (-100, -100), self.imagelink)
        playerRect = self.image.getRect()
        self.maxX = WINDOW_WIDTH - playerRect.width
        self.maxY = GAME_HEIGHT - playerRect.height

    # Every frame, move the Player icon to the mouse position
    # Limits the x- and y-coordinates to the game area of the window
    def update(self, x, y):
        if x < 0:
            x = 0
        elif x > self.maxX:
            x = self.maxX
        if y < 0:
            y = 0
        elif y > self.maxY:
            y = self.maxY

        self.image.setLoc((x, y))
        return self.image.getRect()

    def draw(self):
        self.image.draw()

enter Image Description

So I try to create an inventory for the python game Dodger. In the inventory I want to be able to select different players to play the game. But when I run the game, only the option images/player.png is chosen. How can I fix that? thanks
This is what I have tried so far:
//Inventory.py

import pygame
import pygwidgets
import pyghelpers
import random
from Player import Player
import Constants
from Constants import *

class Inventory(pyghelpers.Scene):

    def __init__(self, window):
        self.window = window
        self.player = Player(window)
        self.player0 = pygwidgets.CustomButton(self.window, (30, 250), 'images/player_inv.png')
        self.player1 = pygwidgets.CustomButton(self.window, (280, 250), 'images/player1_inv.jpg')
        self.player2 = pygwidgets.CustomButton(self.window, (30, 450), 'images/char1_inv.png')
        self.inventory = []
        self.image = pygwidgets.Image(self.window,(0, 0),'images/inventory.png')

        self.quitButton = pygwidgets.CustomButton(self.window,
                                                  (30, 650),
                                                  up='images/quitNormal.png',
                                                  down='images/quitDown.png',
                                                  over='images/quitOver.png',
                                                  disabled='images/quitDisabled.png')

        self.backButton = pygwidgets.CustomButton(self.window,
                                                  (240, 650),
                                                  up='images/backNormal.png',
                                                  down='images/backDown.png',
                                                  over='images/backOver.png',
                                                  disabled='images/backDisabled.png')

    def getSceneKey(self):
        return SCENE_INVENTORY

    def enter(self, data):
        pass

    def handleInputs(self, eventsList, keyPressedList):
        for event in eventsList:
            if self.quitButton.handleEvent(event):
                self.quit()

            elif self.backButton.handleEvent(event):
                self.goToScene(Constants.SCENE_PLAY)

            if self.player0.handleEvent(event):
                self.player.imagelink = 'images/player.png'
                self.goToScene(Constants.SCENE_PLAY)

            elif self.player1.handleEvent(event):
                self.player.imagelink = 'images/player1.jpg'
                self.goToScene(Constants.SCENE_PLAY)

            elif self.player2.handleEvent(event):
                self.player.imagelink = 'images/player2.png'
                self.goToScene(Constants.SCENE_PLAY)

    def update(self):
        pass

    def draw(self):
        self.image.draw()
        self.quitButton.draw()
        self.backButton.draw()
        self.player0.draw()
        self.player1.draw()
        self.player2.draw()

//Player.py

import pygame
import pygwidgets
from Constants import *

class Player():
    def __init__(self, window):
        self.window = window
        self.imagelink = 'images/player.png'
        self.image = pygwidgets.Image(window,
                                (-100, -100), self.imagelink)
        playerRect = self.image.getRect()
        self.maxX = WINDOW_WIDTH - playerRect.width
        self.maxY = GAME_HEIGHT - playerRect.height

    # Every frame, move the Player icon to the mouse position
    # Limits the x- and y-coordinates to the game area of the window
    def update(self, x, y):
        if x < 0:
            x = 0
        elif x > self.maxX:
            x = self.maxX
        if y < 0:
            y = 0
        elif y > self.maxY:
            y = self.maxY

        self.image.setLoc((x, y))
        return self.image.getRect()

    def draw(self):
        self.image.draw()

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好菇凉咱不稀罕他 2025-02-11 17:58:24

使用您的代码player.imagelink ='images/player1.jpg',您正在修改A class属性而不是 class实例属性。您可以通过更改self.player.imagelink ='images/player1.jpg'来解决此问题。您将需要为包括player.imagelink = ...的每行进行类似的更改

With your code Player.imagelink = 'images/player1.jpg', you are modifying a class attribute and not a class instance attribute. You can fix this by changing self.player.imagelink = 'images/player1.jpg'. You will need to make similar changes for each line that includes Player.imagelink = ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文