将simplegui转换为pygame

时间:2014-01-18 20:08:59

标签: python pygame

所以最初我被要求使用simplegui在python中制作一个配对游戏。我现在需要将所有代码更改为pygame。到目前为止我的代码:

import pygame
import random

num_list = []
exposed = []
state = 0
first_pick = 0
second_pick = 0
moves = 0

# helper function to initialize globals
def init():
    global num_list, exposed, moves
    moves = 0
    num_list = [i%8 for i in range(16)]
    random.shuffle(num_list)
    exposed = [False for i in range(16)]
    pass



# define event handlers
def mouseclick(pos):
    global state, first_pick, second_pick, moves
    this_pick = int(pos[0] / 50)
    if state == 0:
        first_pick = this_pick
        exposed[first_pick] = True
        state = 1
        moves += 1
    elif state == 1:
        if not exposed[this_pick]:
            second_pick = int(pos[0] / 50)
            exposed[second_pick] = True
            state = 2
            moves += 1
    elif state == 2:
        if not exposed[this_pick]:
            if num_list[first_pick] == num_list[second_pick]:
                pass
            else:
                exposed[first_pick] = False
                exposed[second_pick] = False
            first_pick = this_pick
            exposed[first_pick] = True
            state = 1
            moves += 1
    l.set_text("Moves = " + str(moves))
    pass


# cards are logically 50x100 pixels in size
def draw(canvas):
    offset = 50
    hor_pos = -25
    for i in range(len(num_list)):
        hor_pos += offset
        canvas.draw_text(str(num_list[i]), [hor_pos, 50], 30, "White")
    exposed_pos = -50
    for i in exposed:
        exposed_pos += offset
        if not i:
            canvas.draw_polygon([(exposed_pos, 0), (exposed_pos + 50, 0), (exposed_pos + 50, 100), (exposed_pos + 0, 100)], 10, "White", "Orange")


pygame.init()

clock = pygame.time.Clock()
size = [800,100]
screen = pygame.display.set_mode(size)
done = False
# Program Loop -----------------------------------------------------------------
while done == False:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: # If clicked close
            print ("User asked to quit.")
            done = True # Exits loop

    pygame.display.set_caption("Match-Making Game")


    # register event handlers
    pygame.set_mouseclick_handler(mouseclick)
    pygame.set_draw_handler(draw)
    pygame.add_button("Restart", init)
    l=pygame.add_label("Moves = 0")

    pygame.start()
    # Updates screen

    clock.tick(10)
    # Limits to 10 Frames Per Second

pygame.quit()

上面代码的问题出在程序循环中。我需要将“simplegui”代码转换为pygames。任何人都可以帮我找到相当于set_mouseclick_handler(),set_draw_handler(),add_button(),add_label()和start()的pygame。如果可以的话,有人可以尝试让这些代码运行并帮助我解决代码问题。感谢。

1 个答案:

答案 0 :(得分:0)

Pygame是Simple DirectMedia Layer的python包装器。 SDL不是图形用户界面库。如果你想拥有这样的功能,你可以使用existing gui libaries for pygame,或创建你自己的。

如果您只想要按钮和标签的功能,您可以自己实现它。

首先,您应该创建一个具有绘图方法的Button,将其自身绘制到屏幕上,它在屏幕上的位置,以及将通过set_mouseclick处理程序设置为函数的大小和函数变量。

您可以像这样获得鼠标坐标:

for event in pygame.event.get():
    if event.type == pygame.MOUSEBUTTONDOWN:
        print(event.pos)

然后,您可以将其传递给所有按钮,以便他们确定是否已按下它们。 如果是,他们会调用他们的鼠标点击处理程序。

您必须解决的另一个问题是文字。您可以使用pygame font模块来绘制文本。有很多关于SO的问题涉及pygame中的文本生成,所以我不会深入研究。