如何在窗口中弹跳圆圈?

时间:2019-03-25 14:35:03

标签: python-3.x pyqt5

我只是将此程序作为一种练习,我想使圆在源代码中出现时从头到尾跳动。

import PyQt5,sys
from PyQt5 import QtCore,QtGui,QtWidgets
from PyQt5.QtCore import Qt,QTimer,QPoint
from PyQt5.QtWidgets import QWidget,QApplication,QMainWindow
from PyQt5.QtGui import QPainter

class Circle(QWidget):
    def __init__(self):
        QMainWindow.__init__(self)
        self.resize(250,500)
        self.setWindowTitle("Bounce-Man")
        self.color = Qt.red
        self.fixedplace = 125
        self.mover = 450


    def paintEvent(self,event):
        bouncer.setPen(Qt.black)
        bouncer.setBrush(self.color)
        bouncer.drawEllipse(QPoint(self.fixedplace,self.mover),50,50)
    @QtCore.pyqtSlot() 
    def timer(self):    
        timer = QTimer()
        while self.mover >=50:
            timer.start(1000)
            if self.mover == 50:
                self.mover = 450
            self.mover -= 1
            self.update()



if __name__ == "__main__":
    window = QApplication(sys.argv)
    app = Circle()
    app.show()
sys.exit( window.exec_() )

1 个答案:

答案 0 :(得分:0)

您必须使用QTimer来调用更新位置的函数,请勿使用while循环:

SDL_Window* window = NULL;

//The surface contained by the window
SDL_Surface* screenSurface = NULL;

//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
    printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
}
else
{
    //Create window
    window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
    if( window == NULL )
    {
        printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
    }
    else
    {
        //Get window surface
        screenSurface = SDL_GetWindowSurface( window );

        //Fill the surface white
        SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF ) );

        //Update the surface
        SDL_UpdateWindowSurface( window );
        cout << " Ok" << endl;
        //Wait two seconds
        SDL_Delay( 20000 );
    }
}

//Destroy window
SDL_DestroyWindow( window );

//Quit SDL subsystems
SDL_Quit();

return 0;