Pygame-如何使击打箱与敌人的移动配合使用?

时间:2019-05-24 12:16:30

标签: python multithreading pygame

我正在用Pygame制作Python游戏,目前正在研究Hitbox。每当玩家与敌人碰撞时,程序应暂停(设置play=False)。

当我注释掉所有敌人的移动(第56-64行)时,它只会“起作用”,但这显然不是最佳选择。我已经读过有关Pygame拥有自己的Hitbox碰撞系统的信息,但无法弄清楚。

游戏代码:

import pygame
import threading
from random import randint
from time import sleep

pygame.init()
window = pygame.display.set_mode((900, 900))
bg = pygame.image.load("BACKGROUND IMAGE HERE").convert()

class Entity:
    def __init__(self):
        self.W = 50
        self.H = 50
        self.X = 420
        self.Y = 400
        self.speed = 1/10
        self.hitbox = (self.X,self.Y,50,50) #Try out stuff

    def takeDamage(self):
        print("YOU'VE BEEN HIT!")

class Enemy(Entity): # inherit Entity
    def __init__(self):
        Entity.__init__(self) # get all traits of an entity

class Player(Entity): # inherit Entity
    def __init__(self):
        Entity.__init__(self) # get all traits of an entity
        self.X = 300 # overwrite specific traits
        self.Y = 300
        self.speed=1

Play=True
def Gameplay():
    global enemy_list, Entity,Play
    while True:
        window.blit(bg, [0, 0])
        keys = pygame.key.get_pressed()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        if Play:
            if keys[pygame.K_w] and player.Y >= 1:
                    player.Y-=player.speed
            if keys[pygame.K_s] and player.Y <= 900-player.H:
                    player.Y+=player.speed
            if keys[pygame.K_a] and player.X >= 1:
                    player.X-=player.speed
            if keys[pygame.K_d] and player.X <= 900-player.W:
                    player.X+=player.speed

        pygame.draw.rect(window, (93,124,249), (player.X, player.Y, player.W, player.H))
        if Play:
            for enemy in enemy_list:
                if enemy.X > player.X: 
                    enemy.X = enemy.X - enemy.speed
                else:
                    enemy.X = enemy.X + enemy.speed
                if enemy.Y > player.Y:
                    enemy.Y = enemy.Y - enemy.speed
                else:
                    enemy.Y = enemy.Y + enemy.speed

                pygame.draw.rect(window, (255, 50, 49), (enemy.X, enemy.Y, enemy.W, enemy.H))

                if enemy.Y <= player.Y and enemy.Y+enemy.W >= player.Y+player.W:
#PROBLEM HERE
                    Play=False

        pygame.display.update()

def EnemySpawn():
    global enemy_list,Play
    score= 0
    while True: # make enemies until Play == False
        if Play:
            score+=1
            print("Spawned an enemy! Score:", score-1)
            enemy_list.append(Enemy()) # make an instance of our class
            sleep(randint(1000, 5000)) #I know this is 1-5k Seconds

if __name__ == "__main__":
    player = Player() # notice the difference in capitalization!
    enemy_list = [] # to maintain records of all enemies made
    game_thread = threading.Thread(target=Gameplay)
    game_thread.start()
    enemy_spawner_thread = threading.Thread(target=EnemySpawn)
    enemy_spawner_thread.start()

您知道解决方法吗?有没有比我现在拥有的方法更好的选择了?

1 个答案:

答案 0 :(得分:2)

我建议使用pygame.Rect对象和.collidepoint()colliderect()来查找enemyplayer之间的冲突。

例如检查enemy是否与player的中心发生碰撞:

player_rect = pygame.Rect(player.X, player.Y, player.W, player.H)
enemy_rect  = pygame.Rect(enemy.X, enemy.Y, enemy.W, enemy.H)
if enemy_rect.collidepoint(player_rect.center):
    Play=False

请注意,您应该使用{{而不是.X.Y的{​​{1}},.W.Hplayer属性1}}对象。

如果要验证,如果敌人正好在玩家身上,则比较矩形的中心点就足够了:

enemy