0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pygameでRPGをつくってみる9

Posted at

記事全体の目次へ移動

ダメージアニメ

ricky-senseiの記事にあったコードを短くしてみました。
Zキーが押されたとき、数字が上昇して消えます。

ここをクリックしてください
import pygame
from pygame.locals import *
import sys
import time

pygame.init()  
screen = pygame.display.set_mode((160, 160))  
pygame.display.set_caption("damage_anime")  
font = pygame.font.Font(None, 50)
# 色
white = (255, 255, 255)    
damage = 0
# ダメージ量表示のY座標    
damageY = 0  
while (1):
    # 画面を黒色に塗りつぶし
    screen.fill((0, 0, 0))  
    # attackのアニメーション表示
    text_damage = font.render(str(damage), True, white)
    if damageY > 50:
        screen.blit(text_damage, (55,damageY))
        damageY -= 1
        time.sleep(0.01)
    pygame.display.update()  
    # イベント処理
    for event in pygame.event.get():
        if event.type == QUIT: 
            pygame.quit()  
            sys.exit()
        elif event.type == KEYDOWN:
            # Zーキーが押されたとき
             if event.key == K_z:            
                # ダメージ
                damage = 10                     
                damageY = 120


HPゲージ

Zキーが押された時、HPゲージが減っていきます。

ここをクリックしてください

import pygame
import sys
from pygame.locals import *
import time

pygame.init()
screen = pygame.display.set_mode((160, 160))
pygame.display.set_caption('HP_gauge')
player_HP = 8
deffence = 0
monster_power = 6
gauge_rectx = 80
gauge_rectx2 = 80
monster_HP =  10                            
player_damage = 0


while (1):
    screen.fill((0, 0, 0))
    #HPゲージの表示    
    if gauge_rectx > gauge_rectx2:
        gauge_rectx -= 1
        time.sleep(0.005)
        
    pygame.draw.rect(screen,(255,0,0),(40,79,80,3))            
    pygame.draw.rect(screen,(0,255,0),(40,79,gauge_rectx,3))
    pygame.display.update()
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            if event.key == K_z:
                 if monster_HP > 0:                            
                     player_damage = monster_power // 2 - deffence // 4
                     if 0 != player_HP:
                         gauge_rectx2 = int(gauge_rectx - player_damage / player_HP * gauge_rectx)
                     elif 0 >= player_HP:
                         cage_rectx2 = 0
                     if player_damage <= 0:
                         player_damage = 1
                     player_HP -= player_damage


ここをクリックしてください


ここをクリックしてください


ここをクリックしてください


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?