self.background = "">
self.background.fill((0,0,0))
#Score board label
pygame.font.init()
text = "Up: "+ str(self.nb_up) + " vs. Down: " + str(self.nb_down)
self.lbl_font = pygame.font.SysFont("Comic Sans MS", 12)
self.label = self.lbl_font.render(text, 1, (200,200,200))
##Update score label
def upd_score(self):
text = "Up: "+ str(self.nb_up) + " vs. Down: " + str(self.nb_down)
self.label = self.lbl_font.render(text, 1, (200,200,200))
##Interpret pressed key
def interpret_key(self):
#get pressed key
keystate = pygame.key.get_pressed()
if keystate[pygame.locals.K_UP]:
self.nb_up += 1
if keystate[pygame.locals.K_DOWN]:
self.nb_down += 1
#player wants to stop game - no effect?
if keystate[pygame.locals.K_ESCAPE]:
return False
return True
##Method called in thread
def run(self):
#tick the clock
self.clock.tick(30)
#update key score or exit
if not self.interpret_key():
self.replay = False
self.gameover = True
return
#Quit the game
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.gameover = True
self.replay = False
#update graphical components
self.screen.blit(self.background, (0,0))
self.upd_score()
self.screen.blit(self.label, (10,10))
pygame.display.flip()
##quit game
def quit(self):
pygame.quit()
#pygame.display.quit()
##main window class
class mainwin(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self)
#Main window settings
self.setWindowTitle("Trying to exit pygame")
self.setMinimumSize(300 , 300)
#self.move(0,0)
self.winthread = None
self.game = None
#menu
#Create the Menu Bar
self.menu = self.menuBar()
#Create the File menu entry in the bar
self.file_menu = self.menu.addMenu('&File')
#Create the File entry actions
self.file_quit = self.file_menu.addAction('&Quit')
self.file_quit.setShortcut(QKeySequence('Ctrl+Q'))
self.connect(self.file_quit, SIGNAL("triggered()"), self.quit)
#toolbar
self.toolbar = QToolBar(self)
self.toolbar.setObjectName("toolbar")
self.addToolBar(self.toolbar)
#add menu actions to toolbar
self.toolbar.addAction(self.file_quit)
#main widget
self.mainWidget = QWidget()
self.setCentralWidget(self.mainWidget)
layv = QVBoxLayout()
self.b_startgame = QPushButton('Go!', self.mainWidget)
self.b_startgame.setMaximumWidth(100)
self.connect(self.b_startgame, SIGNAL('clicked()'), self.start_game)
layv.addWidget(self.b_startgame)
self.mainWidget.setLayout(layv)
##exit app window
def quit(self):
self.close()
#start game and thread calling its run
def start_game(self):
self.winthread = mythread(self)
self.winthread.start()
self.game = mygame()
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setApplicationName('TestPyGame')
form = mainwin()
form.show()
app.exec_()