使用Tkinter为我的游戏创建菜单

时间:2013-01-13 22:57:43

标签: python tkinter pygame

我需要帮助使用Tkinter为我的游戏创建一个菜单,主要是一个播放按钮。我正在使用Pygame,如果这有任何不同。

from pygame import *
import random
from datetime import datetime

startTime = datetime.now()

class Sprite:
    def __init__(self, xpos, ypos, filename):
        self.x = xpos
        self.y = ypos
        self.bitmap = image.load(filename)
        self.bitmap.set_colorkey((0, 0, 0))
    def set_position(self, xpos, ypos):
        self.x = xpos
        self.y = ypos
    def render(self):
        screen.blit(self.bitmap, (self.x, self.y))

def Intersect(s1_x, s1_y, s2_x, s2_y):
    if (s1_x > s2_x - 32) and (s1_x < s2_x + 32) and (s1_y > s2_y - 32) and (s1_y < s2_y + 32):
            return 1
    else:
        return 0

init()
screen = display.set_mode((640, 480))
key.set_repeat(1, 1)
display.set_caption('PyInvaders')
backdrop = image.load('data/backdrop.bmp')
enemies = []
x = 0

for count in range(10):
    enemies.append(Sprite(50 * x + 50, 50, 'data/enemy.bmp'))
    enemies.append(Sprite(50 * x + 50, 100, 'data/enemy.bmp'))
    x += 1

hero = Sprite(304, 400, 'data/hero.bmp')
ourmissile = Sprite(0, 480, 'data/heromissile.bmp')
enemymissile = Sprite(0, 480, 'data/enemymissile.bmp')
sandwich = Sprite(304, 20, 'data/sandwich.bmp')

quit = 0
score = 0
enemyspeed = 4

while quit == 0:
    screen.blit(backdrop, (0, 0))

    for count in range(len(enemies)):
        enemies[count].x += + enemyspeed
        enemies[count].render()

    if len(enemies) > 0 and enemies[-1].x > 590:
        enemyspeed = -4
        for count in range(len(enemies)):
            enemies[count].y += 5

    if len(enemies) > 0 and enemies[0].x < 10:
        enemyspeed = 4
        for count in range(len(enemies)):
            enemies[count].y += 5

    if ourmissile.y < 479 and ourmissile.y > 0:
        ourmissile.render()
        ourmissile.y -= 5

    if enemymissile.y >= 480 and len(enemies) > 0:
        enemymissile.x = enemies[random.randint(0, len(enemies) - 1)].x
        enemymissile.y = enemies[0].y

    if Intersect(hero.x, hero.y, enemymissile.x, enemymissile.y):
        quit = 1
        print "...where mah sammich."

    for count in range(0, len(enemies)):
        if Intersect(ourmissile.x, ourmissile.y, enemies[count].x, enemies[count].y):
            score += 1
            ourmissile.y = 480
            del enemies[count]
            break

    if Intersect(ourmissile.x, ourmissile.y, enemymissile.x, enemymissile.y):
        ourmissile.y = 480
        enemymissile.y = 480

    if len(enemies) == 0:
        sandwich.y += 2
        if Intersect(hero.x, hero.y, sandwich.x, sandwich.y):
            score += 10
            quit = 1
            print "YEEEEE GOT ME MAH SAMMICH!"

    for ourevent in event.get():
        if ourevent.type == QUIT:
            quit = 1
        if ourevent.type == KEYDOWN:
            if ourevent.key == K_RIGHT and hero.x < 590:
                hero.x += 3
            if ourevent.key == K_LEFT and hero.x > 10:
                hero.x -= 3
            if ourevent.key == K_SPACE:
                ourmissile.x = hero.x
                ourmissile.y = hero.y

    enemymissile.render()
    enemymissile.y += 5
    hero.render()
    sandwich.render()
    display.update()

print "You scored", score, "/30"
print "It took you", (datetime.now() - startTime), "to play the game."

Code on Pastebin

1 个答案:

答案 0 :(得分:2)

Pygame和Tkinter不混合。 您的UI应该由一个或另一个提供 - 您甚至可以在窗口(没有全屏)pygame应用程序中使用Tkinter弹出瞬态对话框,但这不常见。

可以在运行任何pygame代码之前提供配置/游戏estart对话框,停止Tkinter主循环并启动你的pygame。

否则,为了获得一致的体验,您应该在项目中添加一个使用Pygame进行游戏内使用的GUI。 (由于纯pygame没有支持buttos,菜单,文本条目等)。检查http://www.pygame.org/wiki/gui是否适合您。