Python 如何在pygame中移动按钮?

Python 如何在pygame中移动按钮?,python,pygame,Python,Pygame,我试图将我的开始按钮()移动到屏幕底部,但我不知道如何移动它,因为我尝试了pygame.Rect.move()和pygame.Rect.move\u ip(),但他们似乎没有移动按钮。我想重新定位按钮,以便单击按钮后,可以退出游戏的标题页。开始按钮的位置由功能开始按钮中的对象按钮定义。第二个参数定义按钮的顶部。只需更改第二个参数,例如,高度为50,而不是100: def start_按钮(): #button=pygame.Rect(5010010050) 按钮=pygame.Rect(50,高

我试图将我的开始按钮()移动到屏幕底部,但我不知道如何移动它,因为我尝试了
pygame.Rect.move()
pygame.Rect.move\u ip()
,但他们似乎没有移动按钮。我想重新定位按钮,以便单击按钮后,可以退出游戏的标题页。

开始按钮的位置由功能
开始按钮中的对象
按钮定义。第二个参数定义按钮的顶部。只需更改第二个参数,例如,高度为50,而不是100:

def start_按钮():
#button=pygame.Rect(5010010050)
按钮=pygame.Rect(50,高度-50,100,50)
# [...]
另一种可能是在定义矩形后,将
按钮的
.top
设置为与窗口底部相等:

def start_按钮():
button=pygame.Rect(5010010050)
按钮。底部=高度
# [...]
注意,Rect对象有几个虚拟属性,可用于移动和对齐矩形。看


如果要在应用程序的后面移动按钮,则必须在全局命名空间中定义
start\u button\u rect

start\u button\u rect=pygame.rect(5010010050)
def start_按钮():
pygame.draw.rect(屏幕[0,255,0],开始按钮)
# [...]
现在,您可以更改
start\u button\u rect
,例如:

启动按钮直接移动ip(0400)
或者通过设置虚拟属性:

start\u按钮\u rect.bottom=高度

启动按钮的位置由功能
启动按钮
中的对象
按钮
定义。第二个参数定义按钮的顶部。只需更改第二个参数,例如,高度为50,而不是100:

def start_按钮():
#button=pygame.Rect(5010010050)
按钮=pygame.Rect(50,高度-50,100,50)
# [...]
另一种可能是在定义矩形后,将
按钮的
.top
设置为与窗口底部相等:

def start_按钮():
button=pygame.Rect(5010010050)
按钮。底部=高度
# [...]
注意,Rect对象有几个虚拟属性,可用于移动和对齐矩形。看


如果要在应用程序的后面移动按钮,则必须在全局命名空间中定义
start\u button\u rect

start\u button\u rect=pygame.rect(5010010050)
def start_按钮():
pygame.draw.rect(屏幕[0,255,0],开始按钮)
# [...]
现在,您可以更改
start\u button\u rect
,例如:

启动按钮直接移动ip(0400)
或者通过设置虚拟属性:

start\u按钮\u rect.bottom=高度
import pygame

pygame.init()

width = 800
height = 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Blackjack")
icon = pygame.image.load('jack.jpg')
pygame.display.set_icon(icon)
over_font = pygame.font.Font('GOODDP__.TTF', 64)
jack = pygame.image.load('rsz_jack.png')
jackX = 295
jackY = 100
running = True

def jack_title():
    screen.blit(jack, (jackX, jackY))

def display_title_text():
    over_text = over_font.render("Blackjack", True, (0, 0, 0))
    screen.blit(over_text, (292, 300))

def start_button():
    button = pygame.Rect(50, 100, 100, 50)
    pygame.draw.rect(screen, [0, 255, 0], button)
    button.move(0, 0)
    over_text = over_font.render("START!", True, (0, 0, 0))
    screen.blit(over_text, (0, 75))
    clicked = pygame.mouse.get_pressed()
    if clicked[0] == 1:
        print('Clicked left button!')
    elif clicked[1] == 1:
        print('Clicked middle button!')
    elif clicked[-1] == 1:
        print('Clicked right button!')
    else:
        pass


def quit_button():
    button = pygame.Rect(50, 100, 100, 50)
    pygame.draw.rect(screen, [255, 0 ,0], button)

def display_gameover_message():
    over_text = over_font.render("GAME OVER", True, (0, 0, 0))
    screen.blit(over_text, (292, 300))

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    screen.fill((150, 173, 252))
    jack_title()
    display_title_text()
    start_button()
    quit_button()
    pygame.display.update()