简体   繁体   English

Pygame:pygame.mouse.get_pos() 元组超出范围?

[英]Pygame: pygame.mouse.get_pos() tuple out of range?

I'm attempting to create a pygame Tic-Tac-Toe game.我正在尝试创建一个 pygame 井字游戏。 My issue is in my 'mouse movement()' method.我的问题出在我的“鼠标移动()”方法中。 Whenever i use the 'pygame.mouse.get pos()' in my 'collidepoint()' method it gives me a type error and says that my tuples index is out of range.每当我在我的“碰撞点()”方法中使用“pygame.mouse.get pos()”时,它都会给我一个类型错误并说我的元组索引超出范围。 Anyone have any ideas?有人有主意吗?

import pygame

pygame.init()
pygame.font.init()

pygame.display.set_caption("Tic-Tac-Toe")

pygame.mouse.set_visible(True)

FPS = 60
clock = pygame.time.Clock()

WIDTH,HEIGHT = 500,600
BORDER_THICKNESS = 10

WIN = pygame.display.set_mode((WIDTH,HEIGHT))

#delcare the fonts of the characters
GAME_FONT = pygame.font.SysFont("comicsans",100)

#colors
WHITE = (255,255,255)
BLACK = (0,0,0)
GREEN = (0,255,0)

def display_window(squares,marks,turn):
    WIN.fill(WHITE)
    starting_pos = WIDTH // 3
    for l in range(2):  #draws the board
        vert = pygame.Rect((starting_pos - BORDER_THICKNESS//2,0),(BORDER_THICKNESS,HEIGHT))
        hrzn = pygame.Rect((0,starting_pos - BORDER_THICKNESS//2),(WIDTH,BORDER_THICKNESS))
        starting_pos = starting_pos * 2 + BORDER_THICKNESS//2
        pygame.draw.rect(WIN,BLACK,vert)
        pygame.draw.rect(WIN,BLACK,hrzn)
    #draws black background for text box
    more_border = pygame.Rect((0,WIDTH),(WIDTH,HEIGHT - WIDTH))
    pygame.draw.rect(WIN,BLACK,more_border)
    #draws actual text box
    text_box = pygame.Rect((0+BORDER_THICKNESS,WIDTH+BORDER_THICKNESS),(WIDTH-BORDER_THICKNESS*2,HEIGHT - WIDTH))
    pygame.draw.rect(WIN,WHITE,text_box)
    #display game squares
    for s in squares:
        pygame.draw.rect(WIN,GREEN,s)
    #prints the marks of x's and o's
    for m in marks:
        pass    #still working on printing the marks
    pygame.display.update()

#use the mouse methods in order to retrive location in which 
#mouse is at and see if it collides with a given square
def mouse_movement(squares,mouse_presses,marks,turn):
    for s in squares:
        if s.collidepoint(pygame.mouse.get_pos()) and mouse_presses[pygame.MOUSEBUTTONDOWN]:
            if turn % 2 == 1:
                marks[squares.index(s)] = 'X'
            elif turn % 2 == 0:
                marks[squares.index(s)] = 'O'
            else:
                raise TypeError("Neither condition is being met")
            
    

def main():
    turn = 1
    x, y = 0,0
    squares = []
    for c in range(3):
        for r in range(3):
            squares.append(pygame.Rect((x,y),(WIDTH//3-BORDER_THICKNESS//2,WIDTH//3-BORDER_THICKNESS//2)))
            x += WIDTH//3 - BORDER_THICKNESS//2 + BORDER_THICKNESS
        x = 0
        y += WIDTH//3 - BORDER_THICKNESS//2 + BORDER_THICKNESS
    
    marks = ['','','','','','','','','']
            
        
    
    game_going = True
    while game_going:
        clock.tick(FPS)

        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                game_going = False
                pygame.quit()
            

        print(pygame.mouse.get_pos())    
        mouse_presses = pygame.mouse.get_pressed()
        marks = mouse_movement(squares,mouse_presses,marks,turn)
        display_window(squares,marks,turn)


if __name__ == '__main__':
    main()
            

I searched the pygame website to make sure that the 'pygame.mouse.get_pos()' method returns a tuple and it does.我搜索了 pygame 网站以确保“pygame.mouse.get_pos()”方法返回一个元组并且确实如此。 I'm not quite sure where to go from here.我不太确定 go 从这里到哪里。

You get the out of range exception because of mouse_presses[pygame.MOUSEBUTTONDOWN] .由于mouse_presses[pygame.MOUSEBUTTONDOWN]你得到了超出范围的异常。

pygame.mouse.get_pressed() returns a list of Boolean values that represent the state ( True or False ) of all mouse buttons. pygame.mouse.get_pressed()返回 Boolean 个值的列表,代表所有鼠标按钮的 state( TrueFalse )。 The state of a button is True as long as a button is held down.只要按下按钮,按钮的 state 就是True When multiple buttons are pressed, multiple items in the list are True .当按下多个按钮时,列表中的多个项目为True The 1st, 2nd and 3rd elements in the list represent the left, middle and right mouse buttons.列表中的第一个、第二个和第三个元素代表鼠标左键、中键和右键。

If you want to test if the left mouse button is pressed it is:如果你想测试鼠标左键是否被按下,它是:

if mouse_presses[0]:

If you want to test if any button is pressed it is:如果你想测试是否有任何按钮被按下,它是:

if any(mouse_presses):

However, this is not how the MOUSEBUTTONDOWN event works.但是,这不是MOUSEBUTTONDOWN事件的工作方式。 The MOUSEBUTTONDOWN event occurs once when you click the mouse button and the MOUSEBUTTONUP event occurs once when the mouse button is released. MOUSEBUTTONDOWN事件在您单击鼠标按钮时发生一次, MOUSEBUTTONUP事件在您释放鼠标按钮时发生一次。 The pygame.event.Event() object has two attributes that provide information about the mouse event. pygame.event.Event() object 有两个属性提供有关鼠标事件的信息。 pos is a tuple that stores the position that was clicked. pos是一个元组,存储被点击的 position。 button stores the button that was clicked. button存储被点击的按钮。 Each mouse button is associated a value.每个鼠标按钮都关联一个值。 For instance the value of the attributes is 1, 2, 3, 4, 5 for the left mouse button, middle mouse button, right mouse button, mouse wheel up respectively mouse wheel down.例如鼠标左键、鼠标中键、鼠标右键、鼠标滚轮向上分别为鼠标滚轮向下的属性值为 1、2、3、4、5。 When multiple keys are pressed, multiple mouse button events occur.当按下多个键时,会发生多个鼠标按钮事件。 Further explanations can be found in the documentation of the module pygame.event .进一步的解释可以在模块pygame.event的文档中找到。

eg:例如:

game_going = True
while game_going:
    # [...]

    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            game_going = False
                
        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1: # left button
                print(event.pos)  # mouse position
                # [...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM