python - 如何在 pygame 中碰撞并降低玩家的生命值?

标签 python pygame

我正在使用 pygame 和 random 模块在 python 中编写一个 2D 游戏,我遇到了一些代码问题。一个问题是,两个玩家都没有受到任何伤害,每颗子弹击中每个玩家 100 点生命值应该造成 10 点伤害(子弹和玩家都是 Sprite )。第二个问题是,我想要一种让两个玩家碰撞而不是相互重叠的方法,而不是刺入函数,也许使用 pygame.sprite.collide_rect(),所以如果你能帮助解决这个问题,我可以得到去掉 stabbed 函数,可以省去第三个问题。第三个问题,我的 stabbed 函数是重复“玩家 1 获胜”和“玩家 2 获胜”,当两个玩家互相接触时。 谢谢!`

import OpenGL
import panda3d
import pygame
import pyglet
import tkinter
import time
import random

pygame.init()
display_width = 1200
display_height = 600

green = (0, 255, 0)
yellow = (255, 255, 0)
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
blue = (0, 0, 255)

person_height = 113
person_width = 150
person2_width = 150
person2_height = 113


gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Wert')
clock = pygame.time.Clock()

personImg = pygame.image.load('Untitled.png').convert_alpha()
personImg2 = pygame.image.load('Untitled2.png').convert_alpha()
bulletImg = pygame.image.load('Untitled1.png').convert_alpha()

player_health = 100
player2_health = 100
x = (0)
y = (display_height * 0.37)
ny = (display_height * 0.37)
nx = (display_width * 0.87)
#ny1 = (display_height * 0.87)
#thing_startx = display_width/2
#thing_starty = display_height/2
#thing_width = 100
#thing_height = 100





class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = personImg
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.player_health = 100
        self.x_change = 0
        self.y_change = 0


    def update(self):
        self.x_change = 0
        self.y_change = 0
        keystate = pygame.key.get_pressed()
        if keystate[pygame.K_w]:
            self.y_change = -5
        if keystate[pygame.K_s]:
            self.y_change = 5
        if keystate[pygame.K_d] and pygame.key.get_mods() and pygame.KMOD_LSHIFT:
            self.x_change = 10
        elif keystate[pygame.K_d]:
            self.x_change = 5
        if keystate[pygame.K_a]:
            self.x_change = -5
        self.rect.x += self.x_change
        self.rect.y +=self.y_change
        if self.rect.right > display_width:
            self.rect.right = display_width
        if self.rect.left < 0:
            self.rect.left = 0
        if self.rect.top < 0:
            self.rect.top = 0
        if self.rect.bottom > display_height:
            self.rect.bottom = display_height

    def shoot(self):
        bullet = Bullet1(self.rect.x, self.rect.y)
        all_sprites.add(bullet)
        bullets.add(bullet)





class Player2(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = personImg2
        self.rect = self.image.get_rect()
        self.rect.x = nx
        self.rect.y = ny
        self.player2_health = 100
        self.x_change1 = 0
        self.y_change1 = 0

    def update(self):
        self.x_change1 = 0
        self.y_change1 = 0
        keystate = pygame.key.get_pressed()
        if keystate[pygame.K_UP]:
            self.y_change1 = -5
        if keystate[pygame.K_DOWN]:
            self.y_change1 = 5
        if keystate[pygame.K_LEFT] and pygame.key.get_mods() and pygame.KMOD_RSHIFT:
            self.x_change1 = -10
        elif keystate[pygame.K_LEFT]:
            self.x_change1 = -5
        if keystate[pygame.K_RIGHT]:
            self.x_change1 = 5
        self.rect.x += self.x_change1
        self.rect.y += self.y_change1
        if self.rect.right > display_width:
            self.rect.right = display_width
        if self.rect.left < 0:
            self.rect.left = 0
        if self.rect.top < 0:
            self.rect.top = 0
        if self.rect.bottom > display_height:
            self.rect.bottom = display_height

    def shoot(self):
        bullet = Bullet(self.rect.x, self.rect.y)
        all_sprites.add(bullet)
        bullets1.add(bullet)



all_sprites = pygame.sprite.Group()
player = Player()
player2 = Player2()
bullets = pygame.sprite.Group()
bullets1 = pygame.sprite.Group()


class Bullet(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((20, 10))
        self.mask = pygame.mask.from_surface(gameDisplay)
        self.image.fill(black)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.x_change = -10


    def update(self):
        self.rect.x += self.x_change
        if self.rect.bottom > display_width:
            self.kill()
        if self.rect.bottom < 0:
            self.kill()

class Bullet1(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((20, 10))
        self.mask = pygame.mask.from_surface(gameDisplay)
        self.image.fill(black)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
        self.x_change1 = 10

    def update(self):
        self.rect.x += self.x_change1
        if self.rect.bottom > display_width:
            self.kill()
        if self.rect.bottom < 0:
            self.kill()


bullets = pygame.sprite.Group()
players1 = pygame.sprite.Group()
players2 = pygame.sprite.Group()
bullet1 = Bullet1(x, y)
players1.add(player)
players2.add(player2)
bullet = Bullet(x, y)
all_sprites.add(bullet1)
all_sprites.add(bullet)
all_sprites.add(player)
all_sprites.add(player2)


def person(x, y):
    gameDisplay.blit(personImg, (x, y))



def person2(nx,ny):
    gameDisplay.blit(personImg2, (nx, ny))


#def boundary():
    #if x > thing_startx and x < thing_startx + thing_width or x + person_width > thing_startx and x + person_width < thing_startx + thing_width:
    #if x > display_width - person_width or x < 0:

def button(msg,x,y,w,h,ic,ac,action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    if x+w > mouse[0] > x and y+h > mouse[1] > y:
        pygame.draw.rect(gameDisplay, ac,(x,y,w,h))

        if click[0] == 1 and action != None:
            action()
    else:
        pygame.draw.rect(gameDisplay, ic,(x,y,w,h))

    smallText = pygame.font.SysFont("timesnewromanms",20)
    textSurf, textRect = text_objects(msg, smallText)
    textRect.center = ( (x+(w/2)), (y+(h/2)) )
    gameDisplay.blit(textSurf, textRect)

def died():

    GameOver = True

    while GameOver:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        gameDisplay.fill(white)
        largeText = pygame.font.SysFont("timesnewromanms", 115)
        TextSurf, TextRect = text_objects("Player 2 wins", largeText)
        TextRect.center = ((display_width / 2), (display_height / 2))
        gameDisplay.blit(TextSurf, TextRect)

        button("Play Again", 450, 450, 100, 50, blue, blue, game_loop)
        button("Quit", 650, 450, 100, 50, red, red, quitgame)

        pygame.display.update()
        clock.tick(15)

def died1():


    GameOver = True

    while GameOver:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        gameDisplay.fill(white)
        largeText = pygame.font.SysFont("timesnewromanms", 115)
        TextSurf, TextRect = text_objects("Player 1 wins", largeText)
        TextRect.center = ((display_width / 2), (display_height / 2))
        gameDisplay.blit(TextSurf, TextRect)

        button("Play Again", 450, 450, 100, 50, blue, blue, game_loop)
        button("Quit", 650, 450, 100, 50, red, red, quitgame)

        pygame.display.update()
        clock.tick(15)



def health_bars(player_health, player2_health):

    if player_health > 75:
        player_health_color = green
    elif player_health > 50:
        player_health_color = yellow
    else:
        player_health_color = red
    if player2_health > 75:
        player2_health_color = green
    elif player2_health > 50:
        player2_health_color = yellow
    else:
        player2_health_color = red
    pygame.draw.rect(gameDisplay, player_health_color, (1080, 25, player2_health, 25))
    pygame.draw.rect(gameDisplay, player2_health_color, (20, 25, player_health, 25))





def redrawgame():
    player.rect.x = x
    player.rect.y = y
    player2.rect.x = nx
    player2.rect.y = ny

def stabbed ():
    stabbed = True
    a = ["Player 1 wins", "Player 2 wins"]
    b = []

    while stabbed:
        for event in pygame.event.get():
            a = ["Player 1 wins", "Player 2 wins"]
            b = []
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        if player_health > player2_health:
            b.clear()
            b.insert(0, "Player 1 wins")
        elif player2_health > player_health:
            b.clear()
            b.insert(0, "Player 2 wins")
        elif player_health == player2_health:
            b.clear()
            b.insert(0, random.choice(a))
            redrawgame()
        gameDisplay.fill(white)
        redrawgame()
        largeText = pygame.font.SysFont("timesnewromanms", 115)
        TextSurf, TextRect = text_objects(b[0], largeText)
        TextRect.center = ((display_width / 2), (display_height / 2))
        gameDisplay.blit(TextSurf, TextRect)

        button("Play Again", 450, 450, 100, 50, blue, blue, game_loop)
        button("Quit", 650, 450, 100, 50, red, red, quitgame)


        pygame.display.update()
        clock.tick(15)
def game_intro():
    intro = True

    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        gameDisplay.fill(white)
        largeText = pygame.font.SysFont("timesnewromanms", 115)
        TextSurf, TextRect = text_objects("Wert", largeText)
        TextRect.center = ((display_width / 2), (display_height / 2))
        gameDisplay.blit(TextSurf, TextRect)

        button("Play", 450, 450, 100, 50, blue, blue, game_loop)
        button("Quit", 650, 450, 100, 50, red, red, quitgame)

        pygame.display.update()
        clock.tick(15)
def quitgame():
    pygame.quit()
    quit()
def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()


def paused():
    largeText = pygame.font.SysFont("timesnewromanms", 115)
    TextSurf, TextRect = text_objects("Paused", largeText)
    TextRect.center = ((display_width / 2), (display_height / 2))
    gameDisplay.blit(TextSurf, TextRect)

    pause = True
    while pause:
        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                pygame.quit()
                quit()



        button("Continue", 450, 450, 100, 50, blue, blue, game_loop)
        button("Quit", 650, 450, 100, 50, red, red, quitgame)

        pygame.display.update()
        clock.tick(15)





def game_loop():
    x = (0)
    y = (display_height * 0.37)
    ny = (display_height * 0.37)
    nx = (display_width * 0.87)
    y_change = 0
    x_change = 0
    x_change1 = 0
    y_change1 = 0
    #thing_startx = display_width/2
    #thing_starty = display_height/2

    gameExit = False

    while not gameExit:

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE:
                        player.shoot()
                if event.type == pygame.MOUSEBUTTONDOWN:
                    player2.shoot()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_p:
                        paused()



            y += y_change
            x += x_change
            ny += y_change1
            nx += x_change1


            player_health = 100
            player2_health = 100
            gameDisplay.fill(white)
            #pygame.draw.rect(gameDisplay, black, (thing_startx, thing_starty, thing_width, thing_height))

            player
            player2


            #if x > thing_startx and x < thing_startx + thing_width or x + person_width > thing_startx and x + person_width < thing_startx + thing_width:
                #hit()

            #if thing_starty > display_height:
                #thing_starty = 0 - thing_height
                #thing_startx = random.randrange(0, display_width)

            if pygame.sprite.collide_rect(player, player2):
                stabbed()


            player_health = 100
            player2_health = 100

            if pygame.sprite.collide_rect(bullet, player):
                bullet1.kill()
                player_health -= 10
                #return player_health
            if player_health == 0:
                died()


            if pygame.sprite.collide_rect(bullet1, player2):
                bullet.kill()
                player2_health -= 10
                #return player2_health
            if player2_health == 0:
                died1()



            #print(player_health)
            #print(player2_health)

            all_sprites.update()

            health_bars(player_health, player2_health)
            all_sprites.draw(gameDisplay)
            pygame.display.flip()

            pygame.display.update()
            clock.tick(60)


game_intro()
game_loop()
pygame.quit()
quit()

最佳答案

在游戏循环中,玩家的生命值迅速初始化为 100。在循环之前初始化玩家的健康一次。清除子弹组,这在游戏重新开始时很重要:

player_health = 100
player2_health = 100

for bullet in bullets:
    bullet.kill()
for bullet in bullets1:
    bullet.kill()

while not gameExit:

    # [...]

检查容器 bullets1 中的子弹是否击中 player 并检查容器 bullets 中的子弹是否击中 player2 :

for bullet in bullets1:
    if pygame.sprite.collide_rect(bullet, player):
        bullet.kill()
        player_health -= 10
        break
if player_health == 0:
    died()

for bullet in bullets:
    if pygame.sprite.collide_rect(bullet, player2):
        bullet.kill()
        player2_health -= 10
        break
if player2_health == 0:
    died1()

跳过将单个独立项目符号添加到 all_sprites 组:

all_sprites.add(bullet1)
all_sprites.add(子弹)


两个玩家的条形颜色混淆了:

pygame.draw.rect(gameDisplay, player_health_color, (1080, 25, player_health, 25))
pygame.draw.rect(gameDisplay, player2_health_color, (20, 25, player2_health, 25))
pygame.draw.rect(gameDisplay, player2_health_color, (1080, 25, player2_health, 25))
pygame.draw.rect(gameDisplay, player_health_color, (20, 25, player_health, 25))


BulletBullet2 类的update 方法存在问题。你必须分别测试 self.rect.left self.rect.right,而不是 self.rect.bottom:

def update(self):
    self.rect.x += self.x_change
    if self.rect.left > display_width: # self.rect.left instead of self.rect.bottom
        self.kill()
    if self.rect.right < 0:            # self.rect.right instead of self.rect.bottom
        self.kill()

关于python - 如何在 pygame 中碰撞并降低玩家的生命值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54273814/

相关文章:

python - pygame 项目无法运行 : "pygame.error: No available video device"

python - 在 Intellij IDEA 中导入 pygame

python - 多项式回归失败

python - 如何使用 python 使 html 解析更具性能

python - 是否可以在开发过程中阻止 Django 创建 .pyc 文件?

python - Django:如何检测翻译是否被激活?

python - 在 pygame 中使用计时器来触发某些东西

python - Pygame 移动物体/图像重复自身

python - pygame Sprite 和绘制方法未按预期工作

python - 在 NumPy 中旋转晶体