繁体   English   中英

为 Python 游戏创建库存

[英]Create an inventory for a python game

所以我尝试为python游戏Dodger创建一个库存。 在库存中我希望能够选择不同的玩家来玩游戏。 但是当我运行游戏时,只选择了选项 images/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()

//播放器.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()

在此处输入图像描述

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM