【问题标题】:Pygame Rectangle and Mouse pos Collidepoint not workingPygame Rectangle 和 Mouse pos Collidepoint 不工作
【发布时间】:2017-05-11 21:15:21
【问题描述】:

我正在制作一个基本游戏,其中有一个表面,每次单击该表面时,它都会向右移动 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.collidepoint(a,b) 总是返回比较 False 的值。
  • 您可以回答自己的问题并将其标记为已解决。

标签: python python-2.7 pygame


【解决方案1】:

在你的函数的这些行中检查你的逻辑:

x = P1[0][0].get_rect()
if x.collidepoint(a,b):
    return True
return False

您的代码取决于这一点:

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

所以你永远不会评估这件作品是否真实。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      我有一些建议给你。首先将矩形存储在 P1 列表中(在以下示例中它仅包含图像和矩形,但也许您也可以向其添加 statp1_1 索引)。现在我们可以移动这个矩形,如果用户点击它(在示例中我将topleft 属性设置为下一点)。阅读 cmets 以获得更多提示。您需要解决的一件事是防止游戏在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.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多