【问题标题】:I'm trying to detect the collision between two moving Rects in pygame [duplicate]我正在尝试检测pygame中两个移动的矩形之间的碰撞[重复]
【发布时间】:2021-11-30 11:19:52
【问题描述】:

所以我一直尝试在 pygame 碰撞中检测两个不同的移动矩形,但它只是不断地记录碰撞。正如在控制台中不断打印输球,但敌人甚至没有接触到球。我想要的效果只是一个简单的游戏,球必须避开大头钉,这样球才不会弹出。

import pygame
import sys
import math
import random
pygame.init()



size = width, height = 800, 600
white = 255, 255, 255
red = 255, 0, 0
clock = pygame.time.Clock()

screen = pygame.display.set_mode(size)

character = pygame.image.load("intro_ball.gif")
charrect = character.get_rect()
x = 340
y = 480
enemy = pygame.image.load("ho.png")
enrect = enemy.get_rect()
ex = random.randint(0, 690)
ey = 0
lose = False




while 1:
     clock.tick(60)
     for event in pygame.event.get():
          if event.type == pygame.QUIT: sys.exit()


     if ey >= 600 and lose != True:
          ey = 0
          ex = random.randint(0, 690)

     collide = pygame.Rect.colliderect(charrect, enrect)


     if event.type == pygame.KEYDOWN:
          if event.key == pygame.K_RIGHT and x < 690:
          x += 4
     if event.key == pygame.K_LEFT and x > 0:
        x -= 4

     if collide:
        lose = True
     else: lose = False

     if lose == True: print("lose")




    ey += 2        
    screen.fill(white)
    screen.blit(enemy, (ex, ey))
    screen.blit(character, (x, y))
    pygame.display.update()

【问题讨论】:

    标签: python pygame collision-detection


    【解决方案1】:

    pygame.Surface.get_rect.get_rect() 返回一个具有 Surface 对象大小的矩形,该矩形始终从 (0, 0) 开始,因为 Surface 对象没有位置。 Surfaceblit 在屏幕上的某个位置。矩形的位置可以由关键字参数指定。例如,可以使用关键字参数topleft 指定矩形的左上角。这些关键字参数在返回之前应用于pygame.Rect 的属性(有关关键字参数的完整列表,请参见pygame.Rect):

    while 1:
        # [...]
    
        charrect = character.get_rect(topleft = (x, y))
        enrect = enemy.get_rect(topleft = (ex, ey))
        collide = pygame.Rect.colliderect(charrect, enrect)
    
        # [...]
    

    另一方面,您根本不需要变量xyexey。使用pygame.Rect 对象的功能。
    另请阅读How can I make a sprite move when key is held down

    小例子:

    import pygame, sys, math, random
    pygame.init()
    
    size = width, height = 800, 600
    white = 255, 255, 255
    red = 255, 0, 0
    clock = pygame.time.Clock()
    screen = pygame.display.set_mode(size)
    
    #character = pygame.image.load("intro_ball.gif")
    character = pygame.Surface((20, 20))
    character.fill((0, 255, 0))
    charrect = character.get_rect(topleft = (340, 480))
    #enemy = pygame.image.load("ho.png")
    enemy = pygame.Surface((20, 20))
    enemy.fill((255, 0, 0))
    enrect = enemy.get_rect(topleft = (random.randint(0, 690), 0))
    lose = False
    
    run = True
    while run:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
        
        keys = pygame.key.get_pressed()
        charrect.x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * 4
        charrect.clamp_ip(screen.get_rect())
    
        enrect.y += 2     
        if enrect.y >= 600 and lose != True:
            enrect.y = 0
            enrect.x = random.randint(0, 690)
    
        collide = pygame.Rect.colliderect(charrect, enrect)
        if collide:
            lose = True
        else: 
            lose = False
        if lose == True: 
            print("lose")
       
        screen.fill(white)
        screen.blit(enemy, enrect)
        screen.blit(character, charrect)
        pygame.display.update()
    
    pygame.quit()
    exit()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-13
      • 2017-10-30
      相关资源
      最近更新 更多