但我是pygame的新手,仍然在研究小怪癖。我有碰撞功能不再向我抛出错误,但它无法检测到碰撞?我知道它比较了左上角,所以这意味着精灵必须具有相同的大小? 我将我的对象调整为相同大小,但它不会拾取它,但我知道肯定有一种方法可以让它移动。非常感谢任何建议!
from classyforryan import sprites
import pygame
import os
import time
pygame.init() #short for initialize does return a tuple of successful intilizaton
gameDisplay= pygame.display.set_mode((800, 600))
bg=pygame.image.load('clouds_converted.jpg')
moore=pygame.image.load("baemoore_converted.png")
jump = False
fall = False
cube = pygame.image.load("otherreferece.png")
player=sprites(moore, 50, 544)
ob = sprites(cube, 2125, 540) #changing 580 to 540
ob1 = sprites(cube, 2125, 540)
ob2 = sprites(cube, 2125, 540)
clock = pygame.time.Clock()
class sprite1(pygame.sprite.Sprite):
def __init__(self, ob, ob1, ob2):
pygame.sprite.Sprite.__init__(self)
#self.image = pygame.image.load("1136693.jpg")
self.spritesgroup = pygame.sprite.Group() #sprite group
self.spritesgroup.add(ob)
self.spritesgroup.add(ob1)
self.spritesgroup.add(ob2)
#self.spritesgroup = pygame.sprite.Group() #sprite group
pygame.display.update()
while not gameExit:
clock.tick(40)
timer = pygame.time.get_ticks()
for event in pygame.event.get():
if event.type==pygame.QUIT:
gameExit=True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if not fall:
jump = True
if event.key == pygame.K_ESCAPE:
gameExit= True
if jump:
player.recty -= 18
if player.recty <= 410:
ycoor = 500
jump = False
fall = True
if fall:
player.recty += 18
if player.recty >= 544:
ycoor = 0
fall = False
pygame.display.update()
gameDisplay.blit(bg,[0,0])
time.sleep(.05)
player.pos()
ob.pos()
ob.left()
ob1.pos()
ob2.pos()
if(timer > 1600):
ob1.left()
if(timer > 2800):
ob2.left()
score = 100
spritegroup=sprite1(ob, ob1, ob2)
blocks_hit_list = pygame.sprite.spritecollide(player, spritegroup.spritesgroup, True)
print(blocks_hit_list)
#for block in blocks_hit_list:
# score +=1
# print("yo new score is", score)
pygame.quit() #unintiliazes pygames
quit() #this will exit out of python
这是我导入的类
import pygame
import os
import time
gameDisplay= pygame.display.set_mode((800, 600))
class sprites(pygame.sprite.Sprite):
def __init__(self, image, xcoor, ycoor):
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
self.image = image
self.rect = self.image.get_rect()
self.rectx=xcoor
self.recty=ycoor
self.rect.topleft = self.rectx, self.recty
def pos(self):
gameDisplay.blit(self.image, [self.rectx, self.recty])
def left(self):
dist = 7
self.rectx = self.rectx - dist