python - 我的过滤器无法工作,尽管它们应该工作相同

标签 python pygame

我正在尝试制作西蒙说的游戏。我有 4 个不同颜色的方 block ,但为了美观,我在每个框周围留出了 10 个像素的边距。现在我正在尝试获取鼠标位置并查看当您单击哪个框时是否会进入,但它不起作用。

主要内容是这样的:

def findColor(mouse):
    if mouse[0] >=margin and mouse[0]<= (width/2-(margin*2)+margin):
        #first column
        if mouse[1] >= margin and mouse[0] <= (height/2-(margin*2)+margin):
            print("red")

我正在测试左上角的方 block ,但它也检测到底部的方 block 计数为红色。(其绿色)。这种情况不应该发生,因为用于查找正方形的 x 过滤器有效,但 y 过滤器不起作用。这是其余的代码,以防万一我是个白痴,但提前谢谢你们!

simon says game

import pygame
import time
import sys
pygame.init()

score = 0

#(width/2-(margin*2)),(height/2-(margin*2))
def findColor(mouse):
    if mouse[0] >=margin and mouse[0]<= (width/2-(margin*2)+margin):
        #first column
        if mouse[1] >= margin and mouse[0] <= (height/2-(margin*2)+margin):
            print("red")

width,height = 690,690
window = pygame.display.set_mode((width,height))
pygame.display.set_caption("Score: "+str(score)) 

width = 700
height = 700 
def drawRect(color,x,y,width,height):
    pygame.draw.rect(window, color, pygame.Rect(x, y, width, height))

margin = 10


boxSizeX = (width/2-(margin*2))
boxSizeY = (height/2-(margin*2))

red=(255,0,0)
black =(0,0,0)
blue = (0,0,255)
green = (0,255,0)
yellow = (255,255,0)
##redBox = {"x":boxSizeX,"y":boxSizeY,"color":red}
#blueBox = {"x":boxSizeX,"y":boxSizeY,"color":blue}
# keep game running till running is true
run = True
while run:
   
   
    for event in pygame.event.get():
         
        # if event is of type quit then set
        # running bool to false
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            
            mouse = pygame.mouse.get_pos()
            findColor(mouse)
            score+=1
            pygame.display.set_caption("Score: "+str(score))
            print(mouse[0]," ",mouse[1])
     









    
    window.fill(black)
     
    #red
    drawRect(red,margin,margin,(width/2-(margin*2)),(height/2-(margin*2)))
    #blue
    drawRect(blue,350,margin,(width/2-(margin*2)),(height/2-(margin*2)))
    #green
    drawRect(green,margin,350,(width/2-(margin*2)),(height/2-(margin*2)))
    #yellow
    drawRect(yellow,350,350,(width/2-(margin*2)),(height/2-(margin*2)))





    # Update our window
    pygame.display.flip()
     
pygame.quit()

最佳答案

您的代码中有一种类型。 y 坐标 mouse[1] ,不是鼠标[0]:

if mouse[1] >= margin and mouse[0] <= (height/2-(margin*2)+margin):/s>

if mouse[1] >= margin and mouse[1] <= (height/2-(margin*2)+margin):

但是,我建议简化您的代码。使用 pygame.Rect 对象和 collidepoint

import pygame
pygame.init()

score = 0

#(width/2-(margin*2)),(height/2-(margin*2))
def findColor(mouse, rect):
    if rect.collidepoint(mouse):
            print("red")

width,height = 690,690
window = pygame.display.set_mode((width,height))
pygame.display.set_caption("Score: "+str(score)) 

width = 700
height = 700 
def drawRect(color, rect):
    pygame.draw.rect(window, color, rect)

margin = 10


boxSizeX = (width/2-(margin*2))
boxSizeY = (height/2-(margin*2))

red=(255,0,0)
black =(0,0,0)
blue = (0,0,255)
green = (0,255,0)
yellow = (255,255,0)

red_rect = pygame.Rect(margin,margin,(width/2-(margin*2)),(height/2-(margin*2)))
blue_rect = pygame.Rect(350,margin,(width/2-(margin*2)),(height/2-(margin*2)))
green_rect = pygame.Rect(margin,350,(width/2-(margin*2)),(height/2-(margin*2)))
yellow_rect = pygame.Rect(350,350,(width/2-(margin*2)),(height/2-(margin*2)))

# keep game running till running is true
run = True
while run:
   
   
    for event in pygame.event.get():
         
        # if event is of type quit then set
        # running bool to false
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            findColor(event.pos, red_rect)
            score+=1
            pygame.display.set_caption("Score: "+str(score))
            print(event.pos[0]," ",event.pos[1])
         
    window.fill(black)
     
    #red
    drawRect(red, red_rect)
    #blue
    drawRect(blue, blue_rect)
    #green
    drawRect(green, green_rect)
    #yellow
    drawRect(yellow, yellow_rect)

    # Update our window
    pygame.display.flip()
     
pygame.quit()

关于python - 我的过滤器无法工作,尽管它们应该工作相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73040637/

相关文章:

python - "Deterministic"伪随机数生成

python - 用于 Android 和 utf-16-be 编码的 Pygame 子集

python - Pygame 文本未更新

python - pygame 一直陷入困境

python - 运行代码时 Pygame 没有响应

python-3.x - 在 pygame 中渲染抗锯齿透明文本

python - 按字母顺序对数据框排序

python - django admin禁用特定用户的密码更改

python - 当 python "requests-cache"命中缓存时,有什么方法可以记录吗?

python - 在写入后调用查找和/或读取操作时,Python 是否会自动刷新其缓冲区?