Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygame矩形和鼠标点不工作_Python_Python 2.7_Pygame - Fatal编程技术网

Python Pygame矩形和鼠标点不工作

Python Pygame矩形和鼠标点不工作,python,python-2.7,pygame,Python,Python 2.7,Pygame,我正在做一个基本的游戏,我有一个表面,每次我点击表面,它就会向右移动5个像素。没有checkCollide(event)函数,程序运行正常,但当我设置该条件时,它不会移动。怎么了 到目前为止,我的代码是 import pygame, sys from pygame.locals import * pygame.init() DISPLAYSURF = pygame.display.set_mode((300,300)) def checkCollide(event): k = 0

我正在做一个基本的游戏,我有一个表面,每次我点击表面,它就会向右移动5个像素。没有checkCollide(event)函数,程序运行正常,但当我设置该条件时,它不会移动。怎么了

到目前为止,我的代码是

import pygame, sys
from pygame.locals import *

pygame.init()

DISPLAYSURF = pygame.display.set_mode((300,300))

def checkCollide(event):
    k = 0
    a,b = event.pos
    x = P1[0].get_rect()
    if x.collidepoint(a,b):
        return True
    return False

CP1 = [(150, 150)
      ,(155, 150)
      ,(160, 150)
      ,(165, 150)
      ,(170, 150)
      ,(175, 150)
      ,(180, 150)
      ,(185, 150)
      ,(190, 150)]

statp1_1 = 0

WHITE = (255,255,255)
DISPLAYSURF.fill(WHITE)

while True: # the main game loop
    P1 = [pygame.image.load('PAzul.png'),CP1[statp1_1],statp1_1]
    DISPLAYSURF.blit(P1[0], P1[1])
    e = pygame.event.get()
    for event in e:
        if event.type == MOUSEBUTTONUP:
            a = checkCollide(event)
            if a:
                DISPLAYSURF.fill(WHITE)
                statp1_1 +=1

        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    pygame.display.update()

谢谢你

检查一下你函数中的逻辑:

x = P1[0][0].get_rect()
if x.collidepoint(a,b):
    return True
return False
您的代码取决于这一点:

a = checkCollide(event)
if a:
    DISPLAYSURF.fill(WHITE)

因此,您永远不会认为这是真的。

请检查您函数中以下几行的逻辑:

x = P1[0][0].get_rect()
if x.collidepoint(a,b):
    return True
return False
您的代码取决于这一点:

a = checkCollide(event)
if a:
    DISPLAYSURF.fill(WHITE)

所以你永远不会认为这篇文章是真的。

我才意识到错在哪里。当我执行
x=P1[0].get_rect()
时,它会创建一个左上角位于(0,0)的曲面。
我需要做的是使用
x.topleft=P1[1]

更改矩形的位置,我刚刚意识到出了什么问题。当我执行
x=P1[0].get_rect()
时,它会创建一个左上角位于(0,0)的曲面。
我需要做的是使用
x.topleft=P1[1]

更改矩形的位置。我有一些提示给你。首先将rect存储在P1列表中(在下面的示例中,它仅包含图像和rect,但是您也可以向其添加
statp1_1
索引)。现在,如果用户单击它,我们就可以移动这个rect(在示例中,我将
topleft
属性设置为下一个点)。阅读评论了解更多提示。您需要修复的一件事是防止游戏在
statp1_1
索引太大时崩溃

import sys
import pygame


pygame.init()

DISPLAYSURF = pygame.display.set_mode((300, 300))

WHITE = (255, 255, 255)
# Don't load images in your while loop, otherwise they have to
# be loaded again and again from your hard drive.
# Also, convert loaded images to improve the performance.
P1_IMAGE = pygame.image.load('PAzul.png').convert()  # or .convert_alpha()

# Look up `list comprehension` if you don't know what this is.
CP1 = [(150+x, 150) for x in range(0, 41, 5)]

statp1_1 = 0
# Now P1 just contains the image and the rect which stores the position.
P1 = [P1_IMAGE, P1_IMAGE.get_rect(topleft=CP1[statp1_1])]

clock = pygame.time.Clock()  # Use this clock to limit the frame rate.

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONUP:
            if P1[1].collidepoint(event.pos):
                print('clicked')
                statp1_1 += 1
                # Set the rect.topleft attribute to CP1[statp1_1].
                P1[1].topleft = CP1[statp1_1]

    DISPLAYSURF.fill(WHITE)
    DISPLAYSURF.blit(P1[0], P1[1])  # Blit image at rect.topleft.

    pygame.display.update()
    clock.tick(30)  # Limit frame rate to 30 fps.

我有一些建议给你。首先将rect存储在P1列表中(在下面的示例中,它仅包含图像和rect,但是您也可以向其添加
statp1_1
索引)。现在,如果用户单击它,我们就可以移动这个rect(在示例中,我将
topleft
属性设置为下一个点)。阅读评论了解更多提示。您需要修复的一件事是防止游戏在
statp1_1
索引太大时崩溃

import sys
import pygame


pygame.init()

DISPLAYSURF = pygame.display.set_mode((300, 300))

WHITE = (255, 255, 255)
# Don't load images in your while loop, otherwise they have to
# be loaded again and again from your hard drive.
# Also, convert loaded images to improve the performance.
P1_IMAGE = pygame.image.load('PAzul.png').convert()  # or .convert_alpha()

# Look up `list comprehension` if you don't know what this is.
CP1 = [(150+x, 150) for x in range(0, 41, 5)]

statp1_1 = 0
# Now P1 just contains the image and the rect which stores the position.
P1 = [P1_IMAGE, P1_IMAGE.get_rect(topleft=CP1[statp1_1])]

clock = pygame.time.Clock()  # Use this clock to limit the frame rate.

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONUP:
            if P1[1].collidepoint(event.pos):
                print('clicked')
                statp1_1 += 1
                # Set the rect.topleft attribute to CP1[statp1_1].
                P1[1].topleft = CP1[statp1_1]

    DISPLAYSURF.fill(WHITE)
    DISPLAYSURF.blit(P1[0], P1[1])  # Blit image at rect.topleft.

    pygame.display.update()
    clock.tick(30)  # Limit frame rate to 30 fps.

然后,
x.collidepoint(a,b)
似乎总是返回比较时为假的值。您可以回答自己的问题并将其标记为已解决。然后,
x.collidepoint(a,b)
似乎总是返回比较时为假的值。您可以回答自己的问题并将其标记为已解决。