如何使子弹击中敌人时血溅

2024-06-05 23:48:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个游戏,敌人的士兵从页面下来,你必须射击他们,然后你得到一个总分在最后。我有一张血溅的照片,我想在敌人被枪杀时出现。我该怎么做?我什么都没试过,除了给“血溅”下个定义,当我向敌人开枪时,我试图称之为“血溅”,但我不知道该怎么做。在

如果有人感兴趣的话: 您可能想看看for bullet in bulletList部分

import pygame
from pygame.locals import * 
import random

# define some colours
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

class Player(pygame.sprite.Sprite):

    def __init__(self):
        # call the parents (sprite) constructor
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.image.load('Cartoon army guy 2.gif').convert_alpha()
        self.rect = self.image.get_rect() 

    def update(self):

        pos = pygame.mouse.get_pos()

        self.rect.x = pos[0] # set the player position to the mouse on the screen

class Bullet(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.image.load('bullet 2.gif').convert_alpha()
        self.rect = self.image.get_rect()


    def update(self):
        # this is for moving the bullet up the screen
        self.rect.y -= 5

class Enemy(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.image.load('enemy soldier 2.gif').convert_alpha()
        self.rect = self.image.get_rect()

    def update(self):
        self.rect.y += float(1.5)



pygame.init() # initialise pygame

# set the window width and height for the screen
windowWidth = 600
windowHeight = 600

# make the screen
thescreen = pygame.display.set_mode((windowWidth, windowHeight))
# create the caption
pygame.display.set_caption('Shooter!')

font = pygame.font.Font(None, 20)

background = pygame.image.load('dry-desert-wasteland.jpeg').convert()

# set the score to 0
score = 0

# sets how fast the game updates the screen
clock = pygame.time.Clock()

allSpritesList = pygame.sprite.Group() # make a group with all sprites in it

enemySpriteList = pygame.sprite.Group() # make a group of sprites with just enemies in it

bulletList = pygame.sprite.Group() # make a group of sprites for just bullets

player = Player() 
allSpritesList.add(player) # add player to the group of all sprites


player.rect.y = 540 # set the player y value to 540 (can't move up or down)



# create the enemies --------------------
for i in range(200):
    enemy = Enemy() 

    enemy.rect.x = random.randrange(0, windowWidth)
    enemy.rect.y = random.randrange(windowHeight - 7000, windowHeight - 650)

    enemySpriteList.add(enemy)
    allSpritesList.add(enemy)

    if enemy.rect.y > 610: # if enemy goes beyond 610 pixels of the screen, remove it
        enemySpriteList.remove(enemy)
        allSpritesList.remove(enemy)

# ------------MAIN LOOP--------------
running = True
while running == True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        elif event.type == pygame.MOUSEBUTTONDOWN: # if person presses mouse down key
            bullet = Bullet() 


            # set the bullet rect.x and rect.y equal to that of the player
            bullet.rect.x = player.rect.x 
            bullet.rect.y = player.rect.y


            allSpritesList.add(bullet) # add bullet to all sprites list
            bulletList.add(bullet) # add bullet to bullet group

    allSpritesList.update() # update the sprite list so everything works


    for bullet in bulletList:
        # collision between enemies and bullets
        enemyCollision = pygame.sprite.spritecollide(bullet, enemySpriteList, True) 
        for enemy in enemyCollision:
            bulletList.remove(bullet) # remove bullet if it hits enemy
            allSpritesList.remove(bullet) # remove bullet from allspriteslist if hits enemy
            score += 1 # add 1 to the score
            print( score )




        if bullet.rect.y < -5: # if bullet goes beyond 5 pixels of the screen, remove it
            bulletList.remove(bullet)
            allSpritesList.remove(bullet)

    thescreen.blit(background, (0, 0))

    allSpritesList.update() # update all sprites (instead of having to update player, enemy
    # and bullets
    allSpritesList.draw(thescreen) # draw all sprites to the screen

    pygame.display.update() # update the screen with what we've provided

    clock.tick(60) # set FPS(frames per second) to 60 
pygame.quit()

Tags: thetorectimageselfaddupdatescreen
1条回答
网友
1楼 · 发布于 2024-06-05 23:48:48

你可以实现一个Player方法Hit(或者bulletthit,随便你喜欢什么),它会将飞溅物blit到你的播放器精灵上。这确保你有球员的位置细节,以正确地闪电他们。在

另外,您可以简单地执行while running:,而不是{}。在

相关问题 更多 >