python 我正在检测pygame中两个移动矩形之间的碰撞

slmsl1lt  于 2023-01-16  发布在  Python
关注(0)|答案(1)|浏览(138)

所以我一直试图在pygame中检测两个不同的移动rects碰撞,但它只是不断地注册碰撞。在控制台不断打印丢失,但敌人甚至没有接触球。我想要的效果只是一个简单的游戏,球必须避免大头钉,使球不会弹出。

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

ruoxqz4g1#

pygame.Surface.get_rect.get_rect()返回一个矩形,其大小与 Surface 对象相同,始终从(0,0),因为 Surface 对象没有位置。Surface 是屏幕上某个位置的blit。矩形的位置可以由关键字参数指定。例如,可以使用关键字参数topleft指定矩形的顶部lft。在返回pygame.Rect之前,这些关键字参数将应用于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()

相关问题