我有一个游戏,敌人士兵从页面下来,你必须射击他们,然后你得到一个总分。我有一张血迹的图片,我想在敌人被射击时出现。我该怎么做?我没有尝试任何东西,除了定义血咒并试图在我射击敌人时调用它,但我不确定如何做到这一点。
代码,如果有人有兴趣:
您可能需要查看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()
答案 0 :(得分:1)
你可以实现一个Player
方法Hit
(或任何你喜欢的BulletHit),它会将你的玩家精灵的啪啪声加入。这样可以确保您拥有玩家的位置详细信息以便正确地进行操作。
此外,您只需while running:
而不是while running == True
。