Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to create some 2D game, using PyQt5. So far I have created map (16x16) matrix.

I am drawing inside QFrame element, which is inside of QMainWindow, as central widget.

Now as I understand, inside QFrame, I have paintEvent method, which have some QPainter.

Now when I put some log at first line of that method, it gets called, everything is OK, but now after I have drown map, I want to draw enemies and character. Now I can not call paintEvent directly, but calling self.update inside QFrame also does not help. I don't see that log I have put there called at all.

So can someone explain how this is working, whet if I want to move my character and draw it in different location?

so in map.py i have something like this:

def updateMap(self):
    self.testUpdate = True
    self.update()


def paintEvent(self, event):
    print("Event is: ", event)
    painter = QPainter(self)
    for p in self.allPositions:
        if p.available == False:
            painter.drawPixmap(p.column*self.block_w, p.row*self.block_h, self.block_w, self.block_h, QPixmap('map/map_block.png'))
        else:
            painter.fillRect(p.column*self.block_w, p.row*self.block_h, self.block_w, self.block_h, Qt.black)
    
    self.drawLifes()

    if self.testUpdate == True:
        painter.fillRect(0, 0, self.block_w, self.block_h, Qt.black)

Now in my main window, i have initalized map object, and when i create players, i want to redraw map:

def init_ui(self):        
    self.setWindowTitle('Try to win, if u can :P')
    self.setFixedSize(SCREEN_WIDTH, SCREEN_HEIGHT)
    self.center_window()
    self.setCentralWidget(self.map)
    
    # Set position for player's
    self.move_player(self.player1, P1_INIT_POS)
    self.move_player(self.player2, P2_INIT_POS)

    self.map.updateMap()

This will produce segmentation fault. But i am not sure what approach i should use to redraw map, or just to draw on that specific position on map. Is there any example similar to what i want to achive?

question from:https://stackoverflow.com/questions/65648951/drawing-on-specific-events-inside-qframe

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
74 views
Welcome To Ask or Share your Answers For Others

1 Answer

Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...