iT邦幫忙

0

pygame碰撞問題

  • 分享至 

  • xImage

目前在練習pygame,而在判斷玩家跟敵人碰撞的時候遇到TypeError: 'Player' object is not iterable,想問一下該怎麼解決這個問題。

import pygame
import random

FPS = 60
WIDTH = 600
HEIGHT = 600
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)

#遊戲初始化 創建視窗
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("game")
clock = pygame.time.Clock()


class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((10, 10))
        self.image.fill(GREEN)
        self.rect = self.image.get_rect()
        self.rect.center = (WIDTH/2, HEIGHT/2)
        self.speedx = 3
        self.speedy = 3
        
    def update(self):
        key_pressed = pygame.key.get_pressed()
        
        #上下左右
        if key_pressed[pygame.K_RIGHT]:
            self.rect.x += self.speedx
        if key_pressed[pygame.K_LEFT]:
            self.rect.x -= self.speedx
        if key_pressed[pygame.K_UP]:
            self.rect.y -= self.speedy
        if key_pressed[pygame.K_DOWN]:
            self.rect.y += self.speedy
        
        #控制邊界
        if self.rect.right > WIDTH:
            self.rect.right = WIDTH
        if self.rect.left < 0:
            self.rect.left = 0
        if self.rect.bottom > HEIGHT:
            self.rect.bottom = HEIGHT
        if self.rect.top < 0:
            self.rect.top = 0
            
    def attack(self):
        bullet = Attack(self.rect.centerx, self.rect.top)
        all_sprites.add(bullet)
        bullets.add(bullet)
        
class Soldier(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((15, 15))
        self.image.fill(RED)
        self.rect = self.image.get_rect()
        
        #球由上到下    
        self.rect.x = random.randrange(0, WIDTH - self.rect.width)
        self.rect.y = random.randrange(0, 40)
        self.speedx = random.randrange(-3, 3)
        self.speedy = random.randrange(2, 6)
        
  
    def update(self):
        self.rect.x += self.speedx  
        self.rect.y += self.speedy
        
        #碰到邊界重製
        if self.rect.right > WIDTH or self.rect.left < 0 :
            self.speedx *= -1
            
        if self.rect.bottom > HEIGHT or self.rect.top < 0 :
            self.speedy *= -1

class Attack(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((8, 8))
        self.image.fill(YELLOW)
        self.rect = self.image.get_rect()
        
        self.rect.centerx = x
        self.rect.bottom = y
        self.speedy = -10
        
  
    def update(self):
        self.rect.y += self.speedy
        
        #碰到邊界重製
        if self.rect.top < 0 :
            self.kill()
            
#動作儲存            
all_sprites = pygame.sprite.Group()
soldiers = pygame.sprite.Group()
bullets = pygame.sprite.Group()
player = Player()
all_sprites.add(player)

#增加敵人
for j in range (15):
    soldier = Soldier()
    all_sprites.add(soldier)
    soldiers.add(soldier)



#遊戲迴圈
running = True
while running:
    #1秒內更新幾次
    clock.tick(FPS)
    
    #取得輸入
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                player.attack()
    #更新遊戲
    all_sprites.update()
    
    #判斷敵人跟子彈是否碰撞
    hits = pygame.sprite.groupcollide(soldiers, bullets, True, True)
    for hit in hits:
        soldier = Soldier()
        all_sprites.add(soldier)
    
    #判斷敵人與玩家是否碰撞    
    hit = pygame.sprite.groupcollide(player, soldiers, False, False)
    if hit :
        running = False
    
    #畫面顯示
    screen.fill(WHITE)
    all_sprites.draw(screen)
    pygame.display.update()

pygame.quit()

bill0704 iT邦新手 5 級 ‧ 2022-05-02 21:23:34 檢舉
沒用過pygame
不過改hit = pygame.sprite.groupcollide([player], soldiers, False, False) 試試看行不行?不行就要把Player丟到一個新的pygame group才能支持她想要的iterable.還有一種怪招是複寫你的Player class的__iter__魔術方法來 return self (自己)
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
akonpk12
iT邦見習生 ‧ 2023-03-28 14:52:26

I THINK this is not true because I am Apply it on https://apkomex.com/critical-action-mod-apk/ and https://apkomex.com/mortal-kombat-11-mod-apk/ but not responded.Please help me.

我要發表回答

立即登入回答