gpt4 book ai didi

python - Pygame 碰撞不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 15:49:17 25 4
gpt4 key购买 nike

对于我的计算项目,我需要制作某种应用程序或游戏。我决定制作太空侵略者。我已经设法让基础工作正常工作, Sprite 会产生,我可以移动玩家,敌人从屏幕顶部掉下来,背景和音乐正常工作,但碰撞无法正常工作。当角接触或两个角之间的中点接触时,它工作正常,程序关闭。但如果角/中点不接触,则 Sprite 会直接穿过彼此。任何帮助将非常感激。这是代码:

import pygame
import random
import sys
# --- constants --- (UPPER_CASE names)

WHITE = (255, 255, 255)
BLACK = ( 0, 0, 0)
RED = (255, 0, 0)
GREEN = ( 0, 255, 0)
BLUE = ( 0, 0, 255)
ORANGE = (255, 255, 0)
YELLOW = ( 0, 255, 255)

DISPLAY_WIDTH = 720
DISPLAY_HEIGHT = 720

FPS = 60

# --- classes --- (CamelCase names)

class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("images\\user1.gif").convert()
self.image = pygame.transform.scale(self.image, (50, 50))
self.rect = self.image.get_rect()
self.speed_x = 0

def update(self):
self.speed_x = 0
keystate = pygame.key.get_pressed()
if keystate[pygame.K_LEFT]:
self.speed_x = -7
if keystate[pygame.K_RIGHT]:
self.speed_x = 7
self.rect.x += self.speed_x
if self.rect.right > DISPLAY_WIDTH:
self.rect.right = DISPLAY_WIDTH
if self.rect.left < 0:
self.rect.left = 0


class Mob(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("images\\enemy1.gif").convert()
self.image = pygame.transform.scale(self.image, (50, 50))
self.rect = self.image.get_rect()
self.speed_x = 0
self.rect.x = random.randrange(0, DISPLAY_WIDTH - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speedy = random.randrange(3, 11)

def update(self):
self.rect.y +=self.speedy
if self.rect.top > DISPLAY_HEIGHT + 10:
self.rect.x = random.randrange(0, DISPLAY_WIDTH - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speedy = random.randrange(3, 11)

# --- functions --- (lower_case names)

# empty

# --- main --- (lower_case names)

# - init -

pygame.init()
pygame.mixer.init()

display = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
display_rect = display.get_rect()

# - objects -

all_sprites = pygame.sprite.Group()
mobs = pygame.sprite.Group()

player = Player()
player.rect.center = ((DISPLAY_WIDTH / 2), DISPLAY_HEIGHT/1.2)

all_sprites.add(player)
for z in range(8):
mob = Mob()
mobs.add(mob)
all_sprites.add(mob)
background = pygame.image.load("images\\background.jpg")
background = pygame.transform.scale(background, (DISPLAY_WIDTH, DISPLAY_HEIGHT))

# - other -

pygame.mixer.music.load("audio\\soundtrack.mp3")
pygame.mixer.music.play(-1)
pygame.mixer.music.set_volume(0.4)

# - mainloop -

crashed = False
clock = pygame.time.Clock()

while not crashed:

# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True

print(event)


# - checks for a hit -
col = pygame.sprite.collide_rect(mob, player)
if col:
sys.exit()
# - updates (without draws) -

all_sprites.update()

# - draws (without updates) -

display.blit(background, (0, 0))

all_sprites.draw(display)

pygame.display.update()

# - FPS -

clock.tick(FPS)

# - end -
pygame.quit()

任何帮助都会很棒,谢谢。

最佳答案

函数pygame.sprite.collide_rect不适合直接使用。它旨在传递到“主”碰撞函数中

  1. pygame.sprite.spritecollide()
  2. pygame.sprite.groupcollide()
  3. pygame.sprite.spritecollideany()

在你的情况下你想要 pygame.sprite.spritecollideany :

col = pygame.sprite.spritecollideany(player, mobs)
if col:
sys.exit()

此函数将返回与玩家发生碰撞的小怪中的一个。

关于python - Pygame 碰撞不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41450865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com