Opinion on GUI for a Battleship game in Python

2019-09-18 04:37发布

问题:

I am making a battleship game for a project. While I have completed the logic and the game works with text input. I was hoping make a (very basic) GUI while still use the methods created for the text.

The two options I've been looking at are PyGame and Tkinter. PyGame does not seem to have a text output/label function. Tkinter does, but it doesn't seem as easy (i feel) as PyGame is.

Though I would love to give either of these frameworks the time they deserve, I have just over 60hrs before this is due.

I wanted to know if anyone one has any experience or insights and if it is a realistic option.

Thanks as always!

回答1:

This is a fairly trivial thing to do in Tkinter. A battleship game shows an array of coordinates which you can display as a grid of checkbuttons.



回答2:

You could check Kivy, it's working in top of OpenGL, provide severals basics widgets (label, button, slider, textinput, layouts, ...), and you can create your own / display graphics etc. Works as a python framework, almost all platforms.

You can also check the recent game contest to see what you can do with it :)



回答3:

Why don't you try Cocos2D? It's higher level, and it supports rich labeling (Class cocos.text.RichLabel)

http://cocos2d.org/



回答4:

I suggest you use pyqt/pyside for this task. This lets you access the powerful Qt framework which has great documentation. If you design a game that does not need fast graphics you can simply use QGraphicsView/QGraphicsScene and related classes to display icons/numbers/whatever. Of course you can use OpenGL with Python and Qt as well.



回答5:

You can render text on pygame, just use:

class pygame.font.Font

#create a new Font object from a file
pygame.font.Font(filename, size): return Font
pygame.font.Font(object, size): return Font

method Font.render

#method of Font() to draw text on a new Surface
Font.render(text, antialias, color, background=None): return Surface

Simple example how to use text on pygame:

from pygame import font as pgfont, sprite as pgspr
import pygame as pg

    class FontSprite(pgspr.DirtySprite):
        def __init__(self, text, x, y):
            '''self.image = surface'''
            pgspr.DirtySprite.__init__(self)
            self.text = text
            self.color = [0,0,0]
            self.image = self.get_image()
            self.rect = pg.Rect((x, y), self.image.get_size())

        def get_image(self):
            self.dirty = 1
            return pgfont.Font('fonts\\BRLNSR.TTF', self.size).render(self.text,
                                                                      True,
                                                                      self.color)