如何在pygame中碰撞并减少玩家的健康状况?

时间:2019-01-20 05:21:31

标签: python pygame

我正在用pygame和random模块在python中编写2D游戏,并且代码有问题。一个问题是,没有一个玩家受到任何伤害,这对每个击中100个生命值的子弹来说应该是10点伤害(子弹和玩家都是精灵)。第二个问题是,我希望使用一种pygame.sprite.collide_rect()来使两个播放器发生碰撞,而不是彼此重叠,而不是使用刺入式函数,因此,如果您可以解决这个问题,我可以摆脱被刺伤的功能,可以省去第三个问题。第三个问题是我的刺伤功能是当两个玩家彼此接触时重复“玩家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()

1 个答案:

答案 0 :(得分:1)

在游戏循环内,玩家的生命值被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(bullet)


2名球员的杠铃颜色令人困惑:

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))


updateBullet的{​​{1}}方法中存在问题。您必须分别测试Bullet2 self.rect.left,而不是self.rect.right

self.rect.bottom