簡體   English   中英

Pygame 單擊時不繪制矩形

[英]Pygame not drawing rectangle while clicking

我是 pygame 的新手,我正在制作一個簡單的游戲。 在這段代碼中,我試圖在鼠標位於某個 position 時繪制一個矩形,但是當我打印任何內容時它沒有繪制它

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()

要使矩形永久化,您需要在應用程序循環中繪制矩形。 該事件在單個幀中僅發生一次。 添加一個變量clicked = False 在事件發生時設置變量。 並根據變量的 state 繪制矩形:

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判斷一個字段是否被點擊,並改變該字段的state

     if ev.type == pygame.MOUSEBUTTONDOWN: for i, rect in enumerate(rect_list): if rect.collidepoint(ev.pos): clicked_list[i] = 1
  • 根據其 state 在循環中繪制該字段:

     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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM