Pygame不显示窗口

时间:2015-07-29 23:48:17

标签: python pygame

我有一个名为cards的文件夹,它有png扑克牌图像H,D,C和S 01-13。 下面的卡经销商代码适用于Raspbian下的Raspberry Pi,但是当在Pycharm(Python 3.4)中运行时,它会执行所有操作,但不显示任何窗口。我已经在每个部分中放置了调试打印语句(现在已删除)以检查它是否完成了所有操作。有什么线索吗?

# prompts to play/quit and for hand size and no of players
#
import random
import pygame
pygame.init()

def main():
    myvar = input("Press P to Play or Q to Quit ")
    if myvar =="p" or myvar=="P":
        getimage()
    else:
        quit()

def getimage():
    n_players = int(input("How many players? "))
    hand_size = int(input("How many cards in a hand? "))
    if n_players * hand_size > 52:
        print("There are only 52 cards, cannot do.")
        main()
    #card suit and rank lists
    suits = 'S H D C'.split()
    ranks = '01 02 03 04 05 06 07 08 09 10 11 12 13'.split()

    #generate deck of pngs by list comprehension
    pathw = "cards/"
    deck  = [pathw + s + r + ".png" for r in ranks for s in suits]

    #create Surface as "screen"
    cardw = 77  #actual width = 71
    cardh = 120 #actual width = 96
    screen=pygame.display.set_mode([cardw * hand_size, cardh * n_players])

    #set background colour
    GREEN = (0,102,0)
    screen.fill(GREEN)

    random.shuffle(deck)
    hands = deal(deck, n_players, hand_size, cardw)

    #remove quotes and brackets from string
    c=0
    while c < n_players:
        player1 = str(hands[c])
        player1 = player1.strip("['")
        player1 = player1.strip("']")
        slicep1 (player1, cardw, cardh, c)
        c +=1

    pygame.display.update()
    main()

def slicep1 (player1, cardw, cardh, c):
    pics = player1.split("', '")
    count = 0
    for pic in pics:
        showcards(pic, cardw, cardh, c, count)
        count += 1

def deal(deck, n_players, hand_size, cardw):
    return [deck[i * hand_size:(i+1) * hand_size] for i in range(n_players)]

def showcards(pic, cardw, cardh, c, count):
    pic1 = pygame.image.load(pic)
    main_surface = pygame.display.get_surface()
    main_surface.blit(pic1, (count*cardw+1, c*cardh+1))

main()

1 个答案:

答案 0 :(得分:0)

  

Blockquote下面的卡经销商代码适用于Raspbian下的Raspberry Pi,但是当在Pycharm(Python 3.4)中运行时,它会执行所有操作,但不显示任何窗口。

我猜测PyCharm(Python 3.4)评论意味着它没有在Raspberry Pi上运行。如果是这种情况,那么您在PyCharm(Python 3.4)上的环境似乎有问题。

我的猜测是你的Python 3.4与安装的Pygame版本不兼容。例如,当您真正需要3.4 msi(如果您正在运行Windows)时,您下载并安装了 pygame-1.9.2a0.win32-py3.2.msi

另一种可能性是您运行的是64位版本的Python并且安装了32位版本的Pygame。

我希望有所帮助。如果不知道其他环境正在运行什么版本的Python和Pygame,我无法给出更明确的答案。