-2

我无法让我的 pygame GUI 按钮在我创建的星表上工作,谁能帮我解决这个问题。

我的代码

import os,sys,random
import pygame
from pygame.locals import *
pygame.init()



def start_menu(screen):

    font = pygame.font.Font('freesansbold.ttf', 40)
    image = pygame.Surface((50,50))
    image.fill((0,0,0))
    pygame.draw.polygon(image,(255,255,255),[(200,200),(0,50),(25,25)],5)
    pos = 1
    option1 = font.render("BEGIN",True,(255,255,255))
    option2 = font.render("MY PROFILE",True,(255,255,255))
    option3 = font.render("INSTRUCTIONS",True,(255,255,255))
    option4 = font.render("QUIT",True,(255,255,255))

    N = 200
    SCREEN_W, SCREEN_H = (900, 600)
    pygame.init()

    pygame.display.set_caption('Starts Ja')

    background = pygame.Surface(screen.get_size())
    background = background.convert()


    stars = [
        [random.randint(0, SCREEN_W),random.randint(0, SCREEN_H)]
        for x in range(N)
    ]
    clock = pygame.time.Clock()
    while 1:
        for e in pygame.event.get():
            if e.type == QUIT:
                exit()
            elif e.type == KEYDOWN:
                if e.key == K_DOWN:
                    pos += 1
                    if pos > 4: pos = 1
                elif e.key == K_UP:
                    pos -= 1
                    if pos < 1: pos = 4
                elif e.key == K_RETURN:
                    if pos == 1:
                        import MathsvadersReal
                    elif pos == 4:
                        exit()
            clock.tick(22)
            screen.blit(option1,(100,100))
            screen.blit(option2,(200,200))
            screen.blit(option3,(200,300))
            screen.blit(option3,(200,400))
            screen.blit(image,(20,pos*100))
            background.fill((0,0,0))
            for star in stars:
                    pygame.draw.line(background,
                        (255, 255, 255), (star[0], star[1]), (star[0], star[1]))
                    star[0] = star[0] - 1
                    if star[0] < 0:
                        star[0] = SCREEN_W
                        star[1] = random.randint(0, SCREEN_H)
            screen.blit(background, (0,0))
            pygame.display.flip()



if __name__ == '__start_menu__': start_menu()
4

2 回答 2

0

您的问题相当模糊,您发布的代码非常混乱。

我认为您的问题很可能是您将选项文本绘制到屏幕上,然后用您的星空完全覆盖它。
您应该将选项和光标向下移动到screen.blit(background, (0,0)).
但是,发布的代码还有很多其他问题:

  1. 压痕无处不在。我怀疑这是一个复制和粘贴问题,而不是您代码中的实际错误,但这意味着没有人可以在不首先尝试弄清楚您的意思并自己修复它的情况下运行它。
  2. __name__永远不等于'__start_menu__'。这段代码通常是从其他地方调用的吗?很难知道如何正确运行它。
  3. start_menu接受一个参数,但没有调用。
  4. 您调用了pygame.init(),但没有调用 ,pygame.display.set_mode()因此在此示例中未创建任何窗口。如果你假设显示已经被初始化并传入,你不应该再次初始化 pygame。
  5. 你画了两次option3,不画option4。
于 2013-02-12T22:58:24.077 回答
0

好的,正如@Weeble所述,您的代码非常错误。
我对您的代码进行了很多更改,包括:

  1. 制作了显示文本和绘制星星的功能,以提高可读性。
  2. 不需要背景屏幕(已删除);屏幕就够了。
  3. 通过按类别划分块使代码更具可读性。
  4. 为提高可读性而进行了许多更改。

我不得不花很多时间在这上面。

但是,(仍然)
我没有将代码作为函数或if __name__ == ...检查。
你需要做点什么!对?(如果没有,请退出编程!)

这是您的代码的重新组装版本,结果证明它更具可读性。

import os,sys,random
import pygame
# NEVER use from ... import *, even if pygame docs tell you to do so.

def disp(phrase, loc, screen, color): # func to display text
    s = font.render(phrase, True, color)
    screen.blit(s, loc)

def draw_star(star): # drawing a star
    # you only need to change a pixel, so use set_at, not draw.line
    screen.set_at((star[0], star[1]), (255, 255, 255)) 
    star[0] -= 1
    if star[0] < 0:
        star[0] = screen.get_width()
        star[1] = random.randint(0, screen.get_height())

# Divide your code into parts on the basis of what it does like
# initialising pygame
pygame.init()
screen = pygame.display.set_mode((640,480))
pygame.display.set_caption('Menu')
font = pygame.font.Font('freesansbold.ttf', 40)
clock = pygame.time.Clock()

# defining the image/marker
image = pygame.Surface((30,50))
image.fill((0,0,0))
pygame.draw.polygon(image,(255,255,255),[(0,0),(0,50),(25,25)],5)

# creating list of stars, used multi-line for loop for readability
stars = []
for i in range(200):
    x = random.randint(0, screen.get_width())
    y = random.randint(0, screen.get_height())
    stars.append([x,y])

pos = 1
while True:
    # events
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif e.type == pygame.KEYDOWN:
            if e.key == pygame.K_DOWN:
                pos += 1
                if pos > 4:
                    pos = 1
            elif e.key == pygame.K_UP:
                pos -= 1
                if pos < 1:
                    pos = 4
            elif e.key == pygame.K_RETURN:
                if pos == 1:
                    import MathsvadersReal
                elif pos == 4:
                    pygame.quit()
                    sys.exit()

    screen.fill((0,0,0))

    # displaying text,image
    disp("Begin"       , (100,100), screen, (255, 255, 255))
    disp("My Profile"  , (100,200), screen, (255, 255, 255))
    disp("Instructions", (100,300), screen, (255, 255, 255))
    disp("Quit"        , (100,400), screen, (255, 255, 255))
    screen.blit(image,(50, (pos * 100)-10 ))

    # drawing stars
    for star in stars:
        draw_star(star)

    pygame.display.flip()
    clock.tick(22)
于 2013-02-13T12:30:38.797 回答