如何修复此错误:“pygame.Surface”对象不可下标

2024-09-27 00:13:14 发布

您现在位置:Python中文网/ 问答频道 /正文

第34行是出现错误的地方,我是pygame的新手,所以我不知道到底发生了什么。该代码是用来执行一个功能,一旦我点击一个图像。我也不知道这是否有效,因为这个错误阻止我知道

import pygame


black = (0, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)
pygame.init()
size = (500, 400)

screen = pygame.display.set_mode(size)
pygame.draw.rect(screen, red,(150,450,100,50))
button1 = pygame.Rect(100,100,50,50)
button2 = pygame.Rect(200,200,50,50)
button3 = pygame.Rect(130,250,50,50)
pygame.display.set_caption("Yami no Game")

txt = pygame.image.load('txt.png')
Stxt = pygame.transform.scale(txt,(48,48))

exe = pygame.image.load('exe.jpg')
Sexe = pygame.transform.scale(exe,(48,48))
done = False
clock = pygame.time.Clock()

background_image=pygame.image.load('windows_background.jpg').convert()
 
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    mouse = pygame.mouse.get_pos()   # position
    click = pygame.mouse.get_pressed()  # left/right click
    if Sexe[100] + Sexe[48] > mouse[0] > Sexe[100] and Sexe[100] + Sexe(48) > mouse[1] > Sexe[100]:  # Mouse coordinates checking.
        if click[0] == 1: # Left click
            import The_Start #ignore this
    
    screen.blit(background_image, [0,0])
    screen.blit(Stxt,[100,100])
    screen.blit(Sexe,[250,250])

    pygame.display.update()
 

    clock.tick(60)
 

pygame.quit()

Tags: rectimagetxteventgetdisplayloadscreen
1条回答
网友
1楼 · 发布于 2024-09-27 00:13:14

似乎您想要计算鼠标是否位于矩形区域中,该区域由^{}对象Sexe覆盖。 我建议通过^{}表单Sexe获取^{}对象。 使用^{}检查鼠标是否位于矩形区域。 注意,曲面没有位置,当它是blit时,它会获得一个位置。因此,矩形的位置必须由关键字参数(Sexe.get_rect(topleft = (250,250)))设置:

while not done:
    # [...]

    sexe_rect = Sexe.get_rect(topleft = (250, 250)) # position of Sexe as in blit(Sexe, [250,250])
    mouse_pos = pygame.mouse.get_pos()
    if sexe_rect.collidepoint(mouse_pos):
        click = pygame.mouse.get_pressed()
        if click[0] == 1:
            print("Clicked on Sexe")


    # [...]
    screen.blit(Sexe, [250,250])
    # [...]

相关问题 更多 >

    热门问题