Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么可以';我不能正确地绘制和更新地图吗?_Python_Pyqt_Pyqt5 - Fatal编程技术网

Python 为什么可以';我不能正确地绘制和更新地图吗?

Python 为什么可以';我不能正确地绘制和更新地图吗?,python,pyqt,pyqt5,Python,Pyqt,Pyqt5,我正在尝试为我的学校项目制作轰炸机,此代码显示主菜单和地图 我使用Pyqt5,并尝试在任何地方使用self.update(),但它不会改变任何东西 导入系统 从数学导入* 从PyQt5.QtCore导入* 从PyQt5.QtGui导入* 从PyQt5.QtWidgets导入* 从随机导入* 班级人员: 定义初始化(self,imgName): self.vie=1 self.x=1 self.y=1 self.score=0 self.imgName=imgName def左移(自): self

我正在尝试为我的学校项目制作轰炸机,此代码显示主菜单和地图

我使用Pyqt5,并尝试在任何地方使用self.update(),但它不会改变任何东西

导入系统 从数学导入* 从PyQt5.QtCore导入* 从PyQt5.QtGui导入* 从PyQt5.QtWidgets导入* 从随机导入* 班级人员: 定义初始化(self,imgName): self.vie=1 self.x=1 self.y=1 self.score=0 self.imgName=imgName def左移(自): self.x-=1 def右移动(自身): self.x+=1 def移动(自): self.y+=1 def向下移动(自): self.y-=1 班级申请(QApplication): 定义初始化(self,argv): super().\uuuu init\uuuu(argv) self.initUI() def initUI(self): self.font=self.setFont(QFont(“Arial”,14)) self.setStyle(QStyleFactory.create('fusion')) p=self.palete(); p、 setColor(Qpalete.Window,QColor(33,53,73)) p、 设置颜色(Qpalete.Button,QColor(53,53,53)) p、 setColor(Qpalete.Highlight,QColor(142,45197)) p、 setColor(qpalete.ButtonText,QColor(255255)) p、 setColor(qpalete.WindowText,QColor(255255)) self.setPalette(p) 类映射(): 定义初始化(自): self.LONG=15 自我放大=15 self.grid=self.initGrid() def初始网格(自): 网格=[] 案例dep=[(2,1)、(1,2)、(1,12)、(1,13)、(2,13)、(12,1)、(13,1)、(13,2)、(12,13)、(13,13)、(13,12)] 对于范围内的i(self.LONG): 行=[] 如果(i==0)或(i==self.LONG-1): 对于范围内的j(自放大): 行。追加(0) 其他: 对于范围内的j(自放大): 如果(j==0)或(j==14): 行。追加(0) elif(i%2==0)和(j%2==0): 行。追加(0) elif(i,j)在以下情况下: 行。追加(1) elif(i,j)=(1,1): 行。追加(2) 其他:
如果(random()尝试在def paintEvent(self,event)中添加行self.painter.end():

导入系统 从数学导入* 从PyQt5.QtCore导入* 从PyQt5.QtGui导入* 从PyQt5.QtWidgets导入* 从随机导入* 班级人员: 定义初始化(self,imgName): self.vie=1 self.x=1 self.y=1 self.score=0 self.imgName=imgName def左移(自): self.x-=1 def右移动(自身): self.x+=1 def移动(自): self.y+=1 def向下移动(自): self.y-=1 班级申请(QApplication): 定义初始化(self,argv): super().\uuuu init\uuuu(argv) self.initUI() def initUI(self): self.font=self.setFont(QFont(“Arial”,14)) self.setStyle(QStyleFactory.create('fusion')) p=self.palete(); p、 setColor(Qpalete.Window,QColor(33,53,73)) p、 设置颜色(Qpalete.Button,QColor(53,53,53)) p、 setColor(Qpalete.Highlight,QColor(142,45197)) p、 setColor(qpalete.ButtonText,QColor(255255)) p、 setColor(qpalete.WindowText,QColor(255255)) self.setPalette(p) 类映射(): 定义初始化(自): self.LONG=15 自我放大=15 self.grid=self.initGrid() def初始网格(自): 网格=[] 案例dep=[(2,1)、(1,2)、(1,12)、(1,13)、(2,13)、(12,1)、(13,1)、(13,2)、(12,13)、(13,13)、(13,12)] 对于范围内的i(self.LONG): 行=[] 如果(i==0)或(i==self.LONG-1): 对于范围内的j(自放大): 行。追加(0) 其他: 对于范围内的j(自放大): 如果(j==0)或(j==14): 行。追加(0) elif(i%2==0)和(j%2==0): 行。追加(0) elif(i,j)在以下情况下: 行。追加(1) elif(i,j)=(1,1): 行。追加(2) 其他:
if(random()我添加了类映射,够了吗?我添加了,但我不确定要为您提供什么,因为我添加了很多文件,请让我知道这是否足够确定,对不起,我现在将我的所有类分组,因此如果您使用此代码,它应该会产生相同的问题什么是
movePlayerUp
movePlayerDown
movePlayer>左
?与movePlayerRight相同,self.player.x和self.player.y被更新为向上、向下或向左,但我没有在此处添加它们,因为我使用movePlayerRight进行了测试,问题已经存在
import sys
from math import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from random import *

class Perso:
    def __init__(self,imgName):
        self.vie=1
        self.x=1
        self.y=1
        self.score = 0
        self.imgName=imgName
    def moveLeft(self):
        self.x-=1

    def moveRight(self):
        self.x+=1

    def moveUp(self):
        self.y+=1

    def moveDown(self):
        self.y-=1

class Application(QApplication):
    def __init__(self, argv):
        super().__init__(argv)
        self.initUI()

    def initUI(self):
        self.font=self.setFont(QFont("Arial",14))
        self.setStyle(QStyleFactory.create('fusion'))
        p = self.palette();
        p.setColor(QPalette.Window, QColor(33,53,73))
        p.setColor(QPalette.Button, QColor(53,53,53))
        p.setColor(QPalette.Highlight, QColor(142,45,197))
        p.setColor(QPalette.ButtonText, QColor(255,255,255))
        p.setColor(QPalette.WindowText, QColor(255,255,255))
        self.setPalette(p)

class Map():
    def __init__(self):
        self.LONG=15
        self.LARG=15
        self.grid=self.initGrid()

    def initGrid(self):
        grid=[]

        case_dep=[(2,1),(1,2),(1,12),(1,13),(2,13),(12,1),(13,1),(13,2),(12,13),(13,13),(13,12)]

        for i in range(self.LONG):
            line=[]
            if (i==0) or (i==self.LONG-1):
                for j in range(self.LARG):
                    line.append(0)
            else:
                for j in range(self.LARG):
                    if (j==0) or (j==14):
                        line.append(0)
                    elif (i%2==0) and (j%2==0):
                        line.append(0)
                    elif (i,j) in case_dep:
                        line.append(1)
                    elif (i,j)==(1,1):
                        line.append(2)
                    else:
                        if(random()<0.80):
                            line.append(3)
                        else:
                            line.append(1)
            grid.append(line)
        return grid

class Window(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def closeEvent(self,event):
        QCoreApplication.instance().quit()

    def initUI(self):
        # Initialise la fenêtre
        self.setWindow()
        self.player="indien"
        self.stackedLayout = QStackedLayout()

        self.stackedLayout.addWidget(self.MainMenu())
        self.stackedLayout.addWidget(self.MenuOptions())
        self.stackedLayout.addWidget(self.MenuDifficulte())
        self.stackedLayout.addWidget(self.MenuJeu())
        self.stackedLayout.addWidget(self.MenuPerso())

        self.mainWidget = QWidget()
        self.mainWidget.setLayout(self.stackedLayout)
        self.setCentralWidget(self.mainWidget)

        self.setImage()
        self.setCenter()
        self.show()

    def onClickedPerso(self):
        self.stackedLayout.setCurrentIndex(4)

    def onClickedDiff(self):
        self.stackedLayout.setCurrentIndex(3)

    def onClickedJouerNinja(self):
        self.stackedLayout.setCurrentIndex(2)
        self.perso="ninja"

    def onClickedJouerCowboy(self):
        self.stackedLayout.setCurrentIndex(2)
        self.perso="ninja"

    def onClickedJouerChevalier(self):
        self.stackedLayout.setCurrentIndex(2)
        self.perso="chevalier"

    def onClickedJouerIndien(self):
        self.stackedLayout.setCurrentIndex(2)
        self.perso="indien"

    def onClickedOption(self):
        self.stackedLayout.setCurrentIndex(1)

    def onClickedReturn(self):
        self.stackedLayout.setCurrentIndex(0)

    def MainMenu(self):
        widget = QWidget()
        self.vLayout = QVBoxLayout()

        midWidth = self.frameGeometry().width()/2
        midHeight = self.frameGeometry().height()/2

        #Bouton pour lancer le jeu
        self.buttonJeu = QPushButton("Jouer")
        self.buttonJeu.move(midWidth - 50,midHeight-50)
        self.buttonJeu.clicked.connect(self.onClickedPerso)

        self.buttonOpt = QPushButton("Options")
        #self.buttonOpt.setFont(font)
        self.buttonOpt.move(midWidth - 50,midHeight)
        self.buttonOpt.clicked.connect(self.onClickedOption)

        self.buttonQuit= QPushButton("Quitter")
        #self.buttonQuit.setFont(font)
        self.buttonQuit.move(midWidth - 50,midHeight+50)
        self.buttonQuit.clicked.connect(QCoreApplication.instance().quit)

        self.vLayout.addWidget(self.buttonJeu)
        self.vLayout.addWidget(self.buttonOpt)
        self.vLayout.addWidget(self.buttonQuit)

        self.styleSheet = "QPushButton {background : #e85941; border-radius: 10px; font: bold 28px; border-color: white; padding: 6px;}"

        widget.setStyleSheet(self.styleSheet)


        self.vLayout.setAlignment(Qt.AlignCenter)

        widget.setLayout(self.vLayout)

        return widget

    def MenuOptions(self):
        widget = QWidget()
        self.vLayout = QVBoxLayout()

        self.buttonVolume = QPushButton("Volume")
        self.buttonReturn = QPushButton("Retour")

        self.buttonReturn.clicked.connect(self.onClickedReturn)

        self.vLayout.addWidget(self.buttonVolume)
        self.vLayout.addWidget(self.buttonReturn)

        self.styleSheet = "QPushButton {background : #e85941; border-radius: 10px; font: bold 28px; border-color: white; padding: 6px; margin-right: 10px;}"

        widget.setStyleSheet(self.styleSheet)

        self.vLayout.setAlignment(Qt.AlignCenter)

        widget.setLayout(self.vLayout)

        return widget

    def MenuPerso(self):
        #ajouter QHBoxLayout avec img perso remière ligne et boutons de choix 2e ligne qui lance la difficulte
        widget = QWidget()
        images=QWidget()
        buttons=QWidget()

        self.hLayout1=QHBoxLayout()

        imgNinja=QLabel()
        imgIndien=QLabel()
        imgChevalier=QLabel()
        imgCowboy=QLabel()

        imgNinja.setPixmap(QPixmap("img1.png").scaled(100,100))
        imgIndien.setPixmap(QPixmap("img2.png").scaled(100,100))
        imgChevalier.setPixmap(QPixmap("img3.png").scaled(100,100))
        imgCowboy.setPixmap(QPixmap("img4.png").scaled(100,100))

        imgNinja.setMargin(15)
        imgIndien.setMargin(15)
        imgChevalier.setMargin(15)
        imgCowboy.setMargin(15)

        self.hLayout1.addWidget(imgNinja)
        self.hLayout1.addWidget(imgIndien)
        self.hLayout1.addWidget(imgChevalier)
        self.hLayout1.addWidget(imgCowboy)

        self.hLayout1.setAlignment(Qt.AlignCenter)
        images.setLayout(self.hLayout1)

        self.hLayout2 = QHBoxLayout()

        self.buttonNinja = QPushButton("Ninja")
        self.buttonIndien = QPushButton("Indien")
        self.buttonChevalier = QPushButton("Chevalier")
        self.buttonCowboy = QPushButton("Cowboy")
        self.buttonReturn = QPushButton("Retour")

        self.buttonNinja.clicked.connect(self.onClickedJouerNinja)
        self.buttonIndien.clicked.connect(self.onClickedJouerIndien)
        self.buttonChevalier.clicked.connect(self.onClickedJouerChevalier)
        self.buttonCowboy.clicked.connect(self.onClickedJouerCowboy)
        self.buttonReturn.clicked.connect(self.onClickedReturn)


        self.hLayout2.addWidget(self.buttonNinja)
        self.hLayout2.addWidget(self.buttonIndien)
        self.hLayout2.addWidget(self.buttonChevalier)
        self.hLayout2.addWidget(self.buttonCowboy)
        self.hLayout2.addWidget(self.buttonReturn)

        self.hLayout2.setAlignment(Qt.AlignCenter)
        buttons.setLayout(self.hLayout2)

        self.styleSheet = "QPushButton {background : #e85941; border-radius: 10px; font: bold 28px; border-color: white; padding: 6px; margin-right: 10px;}"
        self.styleSheet2 = "buttonReturn {background : #e85941; border-radius: 10px; font: bold 28px; border-color: white; padding: 6px; margin-right: 30px;}"

        widget.setStyleSheet(self.styleSheet)

        vLayout=QVBoxLayout()
        vLayout.addWidget(images)
        vLayout.addWidget(buttons)
        self.vLayout.setSpacing(5)
        widget.setLayout(vLayout)

        return widget

    def MenuDifficulte(self):
        widget = QWidget()
        self.vLayout = QHBoxLayout()
        #essayer QHBoxLayout pour afficher btns horizontalement

        self.buttonFacile = QPushButton("Facile")
        self.buttonNormal = QPushButton("Normal")
        self.buttonDifficile = QPushButton("Difficile")
        self.buttonReturn = QPushButton("Retour")

        self.vLayout.addWidget(self.buttonFacile)
        self.vLayout.addWidget(self.buttonNormal)
        self.vLayout.addWidget(self.buttonDifficile)
        self.vLayout.addWidget(self.buttonReturn)

        self.buttonFacile.clicked.connect(self.onClickedDiff)
        self.buttonNormal.clicked.connect(self.onClickedDiff)
        self.buttonDifficile.clicked.connect(self.onClickedDiff)
        self.buttonReturn.clicked.connect(self.onClickedPerso)

        self.styleSheet = "QPushButton {background : #e85941; border-radius: 10px; font: bold 28px; border-color: white; padding: 6px; margin-right: 10px;}"

        widget.setStyleSheet(self.styleSheet)

        self.vLayout.setAlignment(Qt.AlignCenter)

        widget.setLayout(self.vLayout)

        return widget

    def MenuJeu(self):
        widget = QWidget()
        self.vLayout = QVBoxLayout()

        self.vLayout.addWidget(self.setRenderArea())

        widget.setLayout(self.vLayout)
        return widget

    def setImage(self):
#        image = QImage("../img/Imagefond/MainBackground3.jpg")
        image = QImage("im.png")
        size = image.scaled(QSize(self.frameGeometry().width(),self.frameGeometry().height()))

        palette = QPalette()
        palette.setBrush(10,QBrush(size))
        self.setPalette(palette)

    def setCenter(self):
        # Place la fenêtre au milieu de l'écran
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def setWindow(self):
        width = QDesktopWidget().availableGeometry().width()
        height = QDesktopWidget().availableGeometry().height()
        self.setGeometry(10, 10, width, height)
        self.setWindowTitle("Bomberboy")
        self.statusBar().showMessage("Aucun événement")


    def setRenderArea(self):
        self.renderArea = RenderArea(self.player)
        self.setCentralWidget(self.renderArea)
        return self.renderArea


    def buttonClicked(self):
        sender = self.sender()
        self.statusBar().showMessage(sender.text()+' est préssé')
        print("cliqué")

    def keyPressEvent(self, event):
        key=event.key()
        print(key)
        if key==Qt.Key_Left:
            self.statusBar().showMessage("Touche gauche")
        elif key==Qt.Key_Right:
            self.statusBar().showMessage("Touche droite")
            print("right")
            self.renderArea.movePlayerRight()
            print("right done")
            self.update()
            print("update done")
        elif key==Qt.Key_Up:
            self.statusBar().showMessage("Touche haut")
        elif key==Qt.Key_Down:
            self.statusBar().showMessage("Touche bas")

        elif key==Qt.Key_Escape:
            self.onClickedReturn()

class RenderArea(QWidget):
    def __init__(self,player, parent=None):
        super(RenderArea,self).__init__(parent)
        self.initUI()
        self.caseWidth=50
        self.caseHeight=50
        self.setMap()
        self.player=Perso(player)

    def initUI(self):
        self.pen = QPen(QColor(125,155,155))
        self.pen.setWidth(3)
        self.brush = Qt.NoBrush

    def paintEvent(self, event):
        self.painter = QPainter(self)
        self.painter.setBrush(self.brush)
        self.drawMap(self.painter)
###
        self.painter.end()                                    # <----------------------


    def setMap(self):
        self.map= Map()


    def drawMap(self,painter):
        self.originX = (self.frameGeometry().width()/2)-(self.caseWidth*self.map.LARG/2)
        self.originY = (self.frameGeometry().height()/2)-(self.caseHeight*self.map.LONG/2)

        for i in range(self.map.LONG):
            for j in range(self.map.LARG):
                if(self.map.grid[i][j]==0):
                    r=QRect(QPoint(self.originX+(self.caseWidth*i),self.originY+(self.caseHeight*j)),QSize(self.caseWidth,self.caseHeight))
                    painter.drawPixmap(r,QPixmap("Ok.png").scaled(self.caseWidth,self.caseHeight))

                elif(self.map.grid[i][j]==1):
                    painter.setBrush(Qt.gray)
                    painter.drawRect(QRect(QPoint(self.originX+(self.caseWidth*i),self.originY+(self.caseHeight*j)),QSize(self.caseWidth,self.caseHeight)))
                elif(self.map.grid[i][j]==2):
#                    url="../img/personnages/"+self.player.imgName+"/"+self.player.imgName+".png"
                    url="icono.png"                    
                    r=QRect(QPoint(self.originX+(self.caseWidth*self.player.x),self.originY+(self.caseHeight*self.player.y)),QSize(self.caseWidth,self.caseHeight))
                    self.playerImg=painter.drawPixmap(r,QPixmap(url).scaled(self.caseWidth,self.caseHeight))

                elif(self.map.grid[i][j]==3):
                    r=QRect(QPoint(self.originX+(self.caseWidth*i),self.originY+(self.caseHeight*j)),QSize(self.caseWidth,self.caseHeight))
#                    painter.drawPixmap(r,QPixmap("../img/Blocs/wall.png").scaled(self.caseWidth,self.caseHeight,Qt.KeepAspectRatioByExpanding,))
                    painter.drawPixmap(r,QPixmap("img0.png").scaled(self.caseWidth,self.caseHeight,Qt.KeepAspectRatioByExpanding,))

    def movePlayerRight(self):

        print(self.player.x,self.player.y)
        print(self.map.grid[self.player.x][self.player.y])
        if (self.player.x+1<self.map.LARG-1 and self.map.grid[self.player.x+1][self.player.y]==1):
            self.map.grid[self.player.x+1][self.player.y]=2
            self.map.grid[self.player.x][self.player.y]=1
            self.update()
            print("affect",self.map.grid[self.player.x][self.player.y])


if __name__ == '__main__':
    app = Application(sys.argv)
    win = Window()
    sys.exit(app.exec_())