【问题标题】:Pygame not drawing rectangle while clickingPygame在单击时不绘制矩形
【发布时间】:2021-01-25 12:21:42
【问题描述】:

我是 pygame 的新手,我正在制作一个简单的游戏。在这段代码中,我试图在鼠标位于某个位置时绘制一个矩形,但是当我打印任何东西时它没有绘制它工作

import pygame

pygame.init()

res = (600,600)

screen = pygame.display.set_mode(res)

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

background = (255,150,150)

color_light = (170,170,170)

color_dark = (100,100,100)

hover_color = (255, 204, 203)

width = screen.get_width()

height = screen.get_height()

def draw_line():

    pygame.draw.rect(screen, line_color, (190,10,10,580))
    pygame.draw.rect(screen, line_color, (390, 10, 10, 580))
    pygame.draw.rect(screen, line_color, (10, 200, 580, 10))
    pygame.draw.rect(screen, line_color, (10, 390, 580, 10))



def highlight():

    if 10 <= mouse[0] <= 10+180 and 10 <= mouse[1] <= 10+190:
        pygame.draw.rect(screen, hover_color, (10, 10, 180, 190))  # X,Y,WIDTH,HEIGHT

    if 205 <= mouse[0] <= 205+180 and 10 <= mouse[1] <= 10+190:
        pygame.draw.rect(screen, hover_color, (200, 10, 190, 190))  # X,Y,WIDTH,HEIGHT

    if 400 <= mouse[0] <= 400+190 and 10 <= mouse[1] <= 10+190:
        pygame.draw.rect(screen, hover_color, (400, 10, 190, 190))  # X,Y,WIDTH,HEIGHT

    if 10 <= mouse[0] <= 10+180 and 210 <= mouse[1] <= 210+180:
        pygame.draw.rect(screen, hover_color, (10, 210, 180, 180))  # X,Y,WIDTH,HEIGHT

    if 200 <= mouse[0] <= 200+180 and 210 <= mouse[1] <= 210+180:
        pygame.draw.rect(screen, hover_color, (200, 210, 200, 180))  # X,Y,WIDTH,HEIGHT

    if 400 <= mouse[0] <= 400+190 and 210 <= mouse[1] <= 210+180:
        pygame.draw.rect(screen, hover_color, (400, 210, 190, 180))  # X,Y,WIDTH,HEIGHT

    if 10 <= mouse[0] <= 10+180 and 400 <= mouse[1] <= 400+200:
        pygame.draw.rect(screen, hover_color, (10, 400, 180, 190))  # X,Y,WIDTH,HEIGHT

    if 200 <= mouse[0] <= 200+190 and 400 <= mouse[1] <= 400+200:
        pygame.draw.rect(screen, hover_color, (200, 400, 190, 190))  # X,Y,WIDTH,HEIGHT

    if 400 <= mouse[0] <= 400+190 and 400 <= mouse[1] <= 400+190:
        pygame.draw.rect(screen, hover_color, (400, 400, 190, 190))  # X,Y,WIDTH,HEIGHT



while True:

    screen.fill(background)

    mouse = pygame.mouse.get_pos()
    for ev in pygame.event.get():
        if ev.type == pygame.QUIT:
            pygame.quit()
        if ev.type == pygame.MOUSEBUTTONDOWN:
            if 10 <= mouse[0] <= 10 + 180 and 10 <= mouse[1] <= 10 + 190:
                pygame.draw.rect(screen,background,(10,10,180,190))
                pygame.display.update()
    line_color = (212, 212, 255)
    draw_line()
    highlight()
    pygame.display.update()

【问题讨论】:

    标签: python pygame rect


    【解决方案1】:

    要使矩形永久化,您需要在应用程序循环中绘制矩形。该事件在单个帧中仅发生一次。添加变量clicked = False。在事件发生时设置变量。并根据变量的状态绘制矩形:

    clicked = False
    while True:
    
        screen.fill(background)
    
        mouse = pygame.mouse.get_pos()
        for ev in pygame.event.get():
            if ev.type == pygame.QUIT:
                pygame.quit()
            if ev.type == pygame.MOUSEBUTTONDOWN:
                if 10 <= mouse[0] <= 10 + 180 and 10 <= mouse[1] <= 10 + 190:
                    clicked = True
        line_color = (212, 212, 255)
        draw_line()
        highlight()
        if clicked:
            pygame.draw.rect(screen,background,(10,10,180,190))
        pygame.display.update()
    

    简化您的代码:

    • 为矩形创建一个pygame.Rect 对象列表

      rect_list = [pygame.Rect(10, 10, 180, 190), ...]
      
    • 为指定的字段创建一个列表

      clicked_list = [0 for _ in rect_list]
      
    • 使用collideponit 测试鼠标是否在矩形上:

      for rect in rect_list:
          if rect.collidepoint(mouse):
              pygame.draw.rect(screen, hover_color, rect)
      
    • 使用collidepoint来判断一个字段是否被点击并改变该字段的状态

      if ev.type == pygame.MOUSEBUTTONDOWN:
          for i, rect in enumerate(rect_list):
              if rect.collidepoint(ev.pos):
                  clicked_list[i] = 1
      
    • 根据状态循环绘制字段:

      for i, rect in enumerate(rect_list):
          if clicked_list[i]:
              pygame.draw.rect(screen,background,rect)
      

    完整代码:

    import pygame
    pygame.init()
    
    res = (600,600)
    screen = pygame.display.set_mode(res)
    pygame.display.set_caption("Tic Tac Toe")
    
    background = (255,150,150)
    color_light = (170,170,170)
    color_dark = (100,100,100)
    hover_color = (255, 204, 203)
    width = screen.get_width()
    height = screen.get_height()
    
    def draw_line():
    
        pygame.draw.rect(screen, line_color, (190,10,10,580))
        pygame.draw.rect(screen, line_color, (390, 10, 10, 580))
        pygame.draw.rect(screen, line_color, (10, 200, 580, 10))
        pygame.draw.rect(screen, line_color, (10, 390, 580, 10))
    
    rect_list = [
        pygame.Rect(10, 10, 180, 190),
        pygame.Rect(200, 10, 180, 190),
        pygame.Rect(400, 10, 180, 190),
        pygame.Rect(10, 210, 180, 190),
        pygame.Rect(200, 210, 180, 190),
        pygame.Rect(400, 210, 180, 190),
        pygame.Rect(10, 400, 180, 190),
        pygame.Rect(200, 400, 180, 190),
        pygame.Rect(400, 400, 180, 190)]
    clicked_list = [0 for _ in rect_list]
    
    def highlight():
        for rect in rect_list:
            if rect.collidepoint(mouse):
                pygame.draw.rect(screen, hover_color, rect)
    
    while True:
        mouse = pygame.mouse.get_pos()
        for ev in pygame.event.get():
            if ev.type == pygame.QUIT:
                pygame.quit()
            if ev.type == pygame.MOUSEBUTTONDOWN:
                for i, rect in enumerate(rect_list):
                    if rect.collidepoint(ev.pos):
                        clicked_list[i] = 1
        
        line_color = (212, 212, 255)
        screen.fill(background)
        draw_line()
        highlight()
        for i, rect in enumerate(rect_list):
            if clicked_list[i] == 1:
                pygame.draw.rect(screen,background,rect)
        pygame.display.update()
    

    【讨论】:

    • 什么是 ev.pos in line rect.collidepoint(ev.pos)
    • 我试过它是说如果 clicked_list[i] == 1 中没有定义我:它也说 rect 可以在 pygame.draw.rect(screen,background,rect跨度>
    • ev 是事件。 ev.type 是事件的类型 ev.pos 是与 MOUSEBUTTONDOWN 事件关联的鼠标位置。见pygame.event。按下的按钮存储在ev.button
    • @MoeezAli 你只需要复制代码。答案底部的示例代码已经过测试并且工作正常。很可能忘记了clicked_list = [0 for _ in rect_list]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-08
    相关资源
    最近更新 更多