我无法让我的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()
答案 0 :(得分:0)
您的问题相当含糊,而且您发布的代码非常混乱。
我认为您的问题很可能是您正在将选项文字绘制到屏幕上,然后用您的星空完全覆盖它。
您应该在screen.blit(background, (0,0))
之后移动选项和光标图
但是,发布的代码存在相当多的其他问题:
__name__
永远不会等于'__start_menu__'
。这个代码通常是从其他地方调用的吗?很难知道如何正确运行它。start_menu
接受一个参数,但是没有参数。pygame.init()
,但不呼叫pygame.display.set_mode()
,因此在此示例中不会创建任何窗口。如果您认为显示已经初始化并传入,则不应再次初始化pygame。答案 1 :(得分:0)
好的,正如@Weeble所述,您的代码非常错误 我改变了很多关于你的代码的事情,包括:
我不得不花很多时间在这上面。
但是,(仍然)
我没有将代码作为函数或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)