【问题标题】:Simple Tic-Tac_Toe Game Pygame简单的井字游戏 Pygame
【发布时间】:2014-12-09 21:39:46
【问题描述】:

我正在 pygame 中编写一个简单的井字游戏,但找不到我需要的答案。我希望在某个坐标平面内单击鼠标时出现“X”。我现在拥有的代码只会在按住鼠标按钮时显示一个 X。谢谢

import pygame
import sys

red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
darkBlue = (0,0,128)
white = (255,255,255)
black = (0,0,0)
pink = (255,200,200)

#iconChoice = input("Would you like to be X's or O's?(X/O)?:")

iconChoice = "X"

# initialize game engine
pygame.init()
pygame.font.init()
font = pygame.font.SysFont("Century Schoolbook",12)
# set screen width/height and caption
size = [500,500]
screen = pygame.display.set_mode(size)
pygame.display.set_caption('My Game')
# initialize clock. used later in the loop.
clock = pygame.time.Clock()

# Loop until the user clicks close button
done = False
while done == False:
    # write event handlers here
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
    # write game logic here

    sys_font = pygame.font.SysFont("None",60)
    rendered = sys_font.render(iconChoice, 0, black)
    mousexpos, mouseypos = pygame.mouse.get_pos()
    pygame.event.get()

    '''                
    elif pygame.mouse.get_pressed()[0] == True and mousexpos > 166 and mousexpos < 322 and  mouseypos < 156:
        print("2")

    elif pygame.mouse.get_pressed()[0] == True and mousexpos > 332 and  mouseypos < 156:
        print("3")
        done = True

    elif pygame.mouse.get_pressed()[0] == True and mousexpos < 156 and  mouseypos > 166 and mouseypos < 322:
        print("4")
        done= True

    elif pygame.mouse.get_pressed()[0] == True and mousexpos > 166 and mousexpos < 322 and  mouseypos > 166 and mouseypos < 322:
        print("5")
        done= True

    elif pygame.mouse.get_pressed()[0] == True and mousexpos > 332 and  mouseypos > 166 and mouseypos < 322:
        print("6")
        done = True

    elif pygame.mouse.get_pressed()[0] == True and mousexpos < 156 and  mouseypos > 332:
        print("7")
        done= True

    elif pygame.mouse.get_pressed()[0] == True and mousexpos > 166 and mousexpos < 322 and  mouseypos > 332:
        print("8")
        done= True

    elif pygame.mouse.get_pressed()[0] == True and mousexpos > 332 and  mouseypos > 332:
        print("9")
        done = True

    '''

    # clear the screen before drawing
    screen.fill((255, 255, 255))


    # draw
    pygame.draw.rect(screen, black, (10,156,480,15), 0)
    pygame.draw.rect(screen, black, (10,322,480,15), 0)
    pygame.draw.rect(screen, black, (156,10,15,480), 0)
    pygame.draw.rect(screen, black, (322,10,15,480), 0)
    pygame.display.flip()

    if pygame.mouse.get_pressed()[0] == True and mousexpos < 156 and mouseypos < 156:
        print("1")
        screen.blit(rendered, (20,15))
        pygame.display.update(10,10,166,166)


    # display what’s drawn. this might change.
    pygame.display.update()

    # run at 20 fps
    clock.tick(20)

# close the window and quit    
pygame.quit()

【问题讨论】:

  • 在我看来,您应该将 pygame.mouse.get_pressed() 的值存储在一个变量中,并使用该变量进行后续评估。
  • 感谢您的回复。这难道不是一回事吗,因为只有当键被按下时,变量才会为真?

标签: python pygame python-3.4


【解决方案1】:

这是您需要的: 鼠标位置:

mouse_pos = mouse.get_pos()

还有一个矩形:

rect1 = Rect(top_left_corner_x, top_left_corner_y, width, height)

然后你检查鼠标是否在这个矩形中被点击:

if event.type == MOUSEBUTTONDOWN and rect1.collidepoint(mouse_pos):
    #draw X

您可以将它放在您的事件循环中或外部(但最好在内部)。

【讨论】:

  • 感谢您的回复,但是当我编辑我的代码时,结果是一样的。 X 仅在按住鼠标时出现
  • 试着把它放在你的事件循环中。这应该可以解决问题。
猜你喜欢
  • 2019-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多