2013-11-24 23 views
1

我有一個遊戲,敵人士兵在頁面下來,你必須射擊它們,然後你在最後得到總分。我有一張當敵人被擊中時想出現的血跡圖像。我將如何做到這一點?我沒有嘗試過任何東西,除了定義血跡並在射擊敵人時試圖稱呼它,但我不確定如何確切地做到這一點。當子彈擊中敵人時如何使血跡出現

代碼,如果任何人的興趣: 你可能想看看在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() 

回答

1

您可以實現Player方法Hit(或BulletHit,任何你喜歡的),這將位塊傳輸的飛濺到你的玩家精靈。這可以確保您擁有玩家的位置詳細信息,以便正確地使用它們。

此外,您可以簡單地執行while running:而不是while running == True

+0

啊,是的!我會在敵人的碰撞中稱它爲?謝謝。哈哈!我沒有想到:p – MafiaCure

+0

是的,在'enemyCollision'循環中調用它。您的歡迎:)順便說一句,偉大的項目,我很樂意玩! –

+1

Aww謝謝! :)乾杯:D – MafiaCure

相關問題