Pygame屏幕不会填充或绘制文本

时间:2018-01-02 16:59:41

标签: python pygame

我是pygame和python的新手,我只是制作一个简单的“道奇”游戏。我创建了一个带有一些按钮的主菜单。

我的主菜单:

import java.util.Stack;

public class ReverseAnEquation {

public static void main(String[] args) {
    // TODO Auto-generated method stub
 String s="2+3*5";
 reverse(s);
}

private static void reverse(String s) {
    // TODO Auto-generated method stub
    Stack st=new Stack<>();

    char[] r=s.toCharArray();
    for(char a:r) {
        st.push(a);
    }
    System.out.println("popping values");

    while(!st.isEmpty()) {
        System.out.print(st.pop());
    }

}

}

我还有一个按钮类:

# show the start screen
done=False
while not done:
    screen.fill(black)
    text_width,text_height=font.size("Dodger")
    #a function for drawing text
    drawText('Dodger', font, screen, (screen_width / 2-(text_width/2)), (screen_height / 2-200))
    font = pygame.font.SysFont(None, 45)
    start_button=button(screen_width/2-125,175,250,50,white,black,'Start')
    start_button.draw()
    instructions_button=button(screen_width/2-125,250,250,50,white,black,'Instructions')
    instructions_button.draw()
    back_button=button(screen_width/2-125,325,250,50,white,black,'Back')
    back_button.draw()
    pygame.display.flip()

两个按钮有效,但第三个没有响应。

我的代码如下所示:

class button(object):
    def __init__(self,x,y,width,height,text_color,background_color,text):
        self.rect=pygame.Rect(x,y,width,height)
        self.image=pygame.draw.rect(screen, background_color,(self.rect),)
        self.x=x
        self.y=y
        self.width=width
        self.height=height
        self.text=text
        self.text_color=text_color

    def check(self):
        return self.rect.collidepoint(pygame.mouse.get_pos())

    def draw(self):
        drawText(self.text,font,screen,self.x+self.width/2,self.y+self.height/2,self.text_color)  
        pygame.draw.rect(screen,self.text_color,self.rect,3) 

但是,我的“说明”按钮不起作用,但另外两个工作。

代码:

#show start screen
done=False
while not done:
    screen.fill(black)
    text_width,text_height=font.size("Dodger")
    drawText('Dodger', font, screen, (screen_width / 2-(text_width/2)), (screen_height / 2-200))
    font = pygame.font.SysFont(None, 45)
    #the start button starts the game
    start_button=button(screen_width/2-125,175,250,50,white,black,'Start')
    start_button.draw()
    #my button that is not working
    instructions_button=button(screen_width/2-125,250,250,50,white,black,'Instructions')
    instructions_button.draw()
    #go back to game selection
    back_button=button(screen_width/2-125,325,250,50,white,black,'Back')
    back_button.draw()
    pygame.display.flip()
    while not done:
        for event in pygame.event.get():
                if event.type==QUIT:
                    terminate()
                elif event.type == pygame.MOUSEBUTTONDOWN:
                    if start_button.check()==True:
                            #main game code
                    if instructions_button.check()==True:
                            #show instructions
                    if back_button.check()==True:
                            #go back to game selection

问题在于,当我点击按钮时,屏幕不会填充(elif instructions_button.check()==True: screen.fill(black) drawText('some instructions',font,screen,screen_width/2-127.5, 185) back_button.draw() done=False while not done: for event in pygame.event.get(): if event.type==QUIT: terminate() elif back_button.check()==True: done=True )或绘制我的文字(screen.fill(black)。

在我尝试调试它时,我放置了各种drawText('some instructions',font,screen,screen_width/2-127.5, 185)以查看它无法正常工作的原因:

print('hello')

它打印但没有用黑色填满屏幕。

感谢所有帮助!

完整代码:

elif instructions_button.check()==True:
    print('hello')
    screen.fill(black)
    print('hello')
    drawText('some instructions',font,screen,screen_width/2-127.5, 185)
    back_button.draw()
    done=False
    while not done:
    for event in pygame.event.get():
        if event.type==QUIT:
            terminate()
        elif back_button.check()==True:
            done=True 

1 个答案:

答案 0 :(得分:3)

问题是因为你没有使用pygame.display.update()

所有函数都在RAM内存中绘制缓冲区,update() / flip()将数据从内存缓冲区发送到视频卡缓冲区,并在屏幕上显示。它被称为"Double Buffering",它被用作图像闪烁/撕裂的解决方案。

enter image description here

顺便说一句:你也忘记了MOUSEBUTTONDOWN,所以当鼠标触摸按钮没有使用鼠标按钮时,“点击”按钮Back

其他问题 - 您使用done变量退出“指令”,但相同的变量控制外部while循环,以便退出游戏。你必须使用不同的变量。如果您将使用此内部函数,那么它不会产生问题,代码将更好地组织起来。

您不必使用==True进行检查。

但您可以在==之后和,之后使用空格来使代码更具可读性 请参阅:PEP 8 -- Style Guide for Python Code

 elif instructions_button.check():
      screen.fill(black)
      drawText('Your Score:', font, screen, screen_width/2-127.5, 185)
      back_button.draw()
      pygame.display.update() # <-- send to video card
      doneX = False # <-- use different variable
      while not doneX:
           for event in pygame.event.get():
               if event.type == QUIT:
                   terminate()
               elif event.type == pygame.MOUSEBUTTONDOWN: # <-- check mouse click
                   if back_button.check():
                       doneX = True 

您不必创建两个函数drawText()terminate()