This question already has answers here:
How can I force division to be floating point? Division keeps rounding down to 0?
(11个答案)
2年前关闭。
我正在尝试在pygame中制作一个Healthbar,并且应该以百分比显示。我使用的公式是
游戏开始时,玩家以
现在,当玩家拥有
当玩家拥有
game.py-功能名称为draw_health_bar
player.py(对象名称是game.py中的Troll)
(11个答案)
2年前关闭。
我正在尝试在pygame中制作一个Healthbar,并且应该以百分比显示。我使用的公式是
(lives/mlives)*100
,mlives
代表max lives
。游戏开始时,玩家以
3 lives
开始。每次shielding
降到0
或更低时,都会从-1
中删除lives
。因此,在第一次运行时,一切正常。但是,如果从lives
中删除-1,则健康栏显示0,而我尝试在终端上打印公式,而不用100乘以查看lives / mlives
时显示的内容。现在,当玩家拥有
3 lives
时,公式将给出1.0
,即100%
当玩家拥有
2 lives
时,公式将给出0.0
而不是0.66
,即66%
player has 3 lives
player has 2 lives
game.py-功能名称为draw_health_bar
#!/usr/bin/python
import os, sys, player, enemy, config, random
from os import path
try:
import pygame
from pygame.locals import *
except ImportError, err:
print 'Could not load module %s' % (err)
sys.exit(2)
# main variables
FPS, BGIMG = 30, 'FlappyTrollbg.jpg'
# initialize game
pygame.init()
screen = pygame.display.set_mode((config.WIDTH,config.HEIGHT))
pygame.display.set_caption("FlappyTroll - Python2.7")
# background
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((255,255,255))
img_dir = path.join(path.dirname(__file__), 'img')
class Background(pygame.sprite.Sprite):
def __init__(self, image_file, location):
pygame.sprite.Sprite.__init__(self)
self.width = config.WIDTH
self.height = config.HEIGHT
self.image = pygame.image.load(path.join(img_dir,image_file)).convert_alpha()
self.image = pygame.transform.scale(self.image,(self.width,self.height))
self.rect = self.image.get_rect()
#self.rect.left, self.rect.top = location
self.rect.x, self.rect.y = location
self.speedx = 5
def update(self):
self.rect.x -= self.speedx
if self.rect.x <= -config.WIDTH:
self.rect.x = config.WIDTH
def draw_shield_bar(surf, x, y, percent):
if percent <= 0:
percent = 0
bar_length = 100
bar_height = 10
fill = (percent / 100) * bar_length
bar_outline = pygame.Rect(x,y,bar_length,bar_height)
bar_filled = pygame.Rect(x,y, fill, bar_height)
pygame.draw.rect(surf, config.green, bar_filled)
pygame.draw.rect(surf, config.white, bar_outline, 2)
def draw_health_bar(surf, x, y, percent):
bar_length = 100
bar_height = 10
fill = float(percent) * bar_length
bar_outline = pygame.Rect(x,y,bar_length,bar_height)
bar_filled = pygame.Rect(x,y, fill, bar_height)
pygame.draw.rect(surf, config.red, bar_filled)
pygame.draw.rect(surf, config.white, bar_outline, 2)
def draw_text(surf, text, size, x, y):
font_ = pygame.font.SysFont("Arial", size)
show_kills = font_.render(text, True, config.white)
surf.blit(show_kills, (x, y))
# blitting
screen.blit(background,(0,0))
pygame.display.flip()
# clock for FPS settings
clock = pygame.time.Clock()
def newSprite(group, obj):
group.add(obj)
def main():
all_sprites = pygame.sprite.Group()
bgs = pygame.sprite.Group()
creature = pygame.sprite.Group()
attack = pygame.sprite.Group()
eattack = pygame.sprite.Group()
bgs.add(Background(BGIMG, [0,0]))
bgs.add(Background(BGIMG, [config.WIDTH,0]))
troll = player.FlappyTroll()
creature.add(troll)
for i in range(0,4):
newSprite(all_sprites, enemy.TrollEnemy())
# variable for main loop
running = True
# init umbrella
# event loop
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
for e in all_sprites:
if (pygame.time.get_ticks() - e.starttime) >= e.delay:
newEAtk = enemy.EnemyAttack(e.rect.x, e.rect.y)
eattack.add(newEAtk)
e.starttime = pygame.time.get_ticks()
keys = pygame.key.get_pressed()
if (keys[pygame.K_SPACE]) and (pygame.time.get_ticks() - troll.starttime) >= troll.delay:
newAtk = player.FlappyAttack(troll.rect.x, troll.rect.y)
attack.add(newAtk)
troll.starttime = pygame.time.get_ticks()
b_gets_hit = pygame.sprite.groupcollide(eattack, attack, True, True)
p_gets_hit_eatk = pygame.sprite.groupcollide(eattack, creature, True, False)
gets_hit = pygame.sprite.groupcollide(all_sprites, attack, True, True)
p_gets_hit = pygame.sprite.groupcollide(all_sprites, creature, True, False)
if gets_hit or p_gets_hit:
newEnemy = enemy.TrollEnemy()
newSprite(all_sprites, newEnemy)
for p in creature:
if p_gets_hit or p_gets_hit_eatk:
troll.shield -= random.randint(1,5)*1.5
if troll.shield <= 0:
troll.lives -= 1
troll.shield = 100
if troll.lives == 0:
print "#--- GAME OVER ---#"
break
screen.fill([255, 255, 255])
# update
bgs.update()
all_sprites.update()
creature.update()
attack.update()
eattack.update()
# draw
bgs.draw(screen)
all_sprites.draw(screen)
creature.draw(screen)
attack.draw(screen)
eattack.draw(screen)
draw_shield_bar(screen, 5, 5, troll.shield)
draw_health_bar(screen, 5, 20, (troll.lives/troll.mlives))
draw_text(screen, ("Lives: "+str(troll.lives)), 20, (config.WIDTH / 2), 0)
print float(troll.lives/troll.mlives)
# flip the table
pygame.display.flip()
pygame.quit()
if __name__ == '__main__':
main()
player.py(对象名称是game.py中的Troll)
import pygame, config
from pygame.locals import *
from os import path
from random import randint
img_dir = path.join(path.dirname(__file__), 'img')
class FlappyTroll(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.width = 64
self.height = 64
self.image = pygame.image.load(path.join(img_dir,"flappytroll.png")).convert_alpha()
self.image = pygame.transform.scale(self.image,(self.width,self.height))
self.rect = self.image.get_rect()
self.rect.x = self.width*2
self.rect.y = config.HEIGHT/2-self.height
self.speedy = 5
self.starttime = pygame.time.get_ticks()
self.delay = 500
self.shield = 100
self.lives = 3
self.mlives = 3
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
self.rect.y -= self.speedy*2
elif self.rect.y < config.HEIGHT-self.height*1.5:
self.rect.y += self.speedy
最佳答案
尝试除以3.0而不是仅除以3。希望这会有所帮助:-) P.S.如果您还要进行加法,减法,除法或乘法运算,请确保将.0放在末尾,除非您想要另一个D.P.。例如2 * 7.0。
关于python - Pygame的健康/maxhealth = 0.0而不是0.66,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43560340/
10-11 09:30