Add bubble image for bubbler shots

master
Elis Axelsson 2017-07-28 12:16:38 +02:00
parent 9e8c739353
commit e69abdf5fc
5 changed files with 76 additions and 12 deletions

View File

@ -1,5 +1,7 @@
class Settings:
def __init__(self):
self.cacheDir = 'etu-pygame-td'
self.size = 1280, 720
self.gridSize = self.size[1] / 12
self.enemyStart = (0, 1.5 * self.gridSize)

54
classes/svg.py Normal file
View File

@ -0,0 +1,54 @@
import os
import hashlib
import cairosvg
from classes.settings import Settings
class Svg(object):
def __init__(self):
settings = Settings()
# Build cachedir based on environment veraibles and settings of name of
# dir to put cached files in
self.cacheDir = os.environ.get(
'XDG_CACHE_HOME',
os.environ.get('HOME', '~') + '/.cache'
) + '/' + settings.cacheDir
# Make cache dir
if not os.path.exists(self.cacheDir):
os.mkdir(self.cacheDir)
def svgAsPng(self, svgName, size):
# Open SVG File and read it
svg = open(svgName).read()
# Hash the SVG
svgHash = hashlib.sha1(svg.encode('utf-8')).hexdigest()
# Generate a PNG Filename
pngFileName = "{}/{}_{}x{}_{}.png".format(
self.cacheDir,
svgName.replace('/', '-').replace('.', '-'),
size[0],
size[1],
svgHash,
)
# If cached file already exists, just return the filename
if os.path.exists(pngFileName):
return pngFileName
# Convert SVG to PNG
pngData = cairosvg.svg2png(
url=svgName,
parent_width=size[0],
parent_height=size[1],
)
# Write file to disk
file = open(pngFileName, 'wb')
file.write(pngData)
# Return filename
return pngFileName

View File

@ -1,22 +1,15 @@
import pygame
from classes.svg import Svg
from classes.towers.projectiles.projectile import Projectile
class Bubble(Projectile):
def __init__(self, startingPosition, angle, speed, damage, range):
# Make a surface
surface = pygame.Surface(
(5, 5),
pygame.SRCALPHA
)
# Convert SVG to PNG and return filename
pngFile = Svg().svgAsPng('images/bubble.svg', (10, 10))
# And fill it with a square the same size
pygame.draw.rect(
surface,
(255, 0, 0),
surface.get_rect(),
0
)
# Import bubble image
surface = pygame.image.load(pngFile)
super(self.__class__, self).__init__(
startingPosition=startingPosition,

View File

@ -6,6 +6,7 @@ stdenv.mkDerivation rec {
python3
python36Packages.pip
python36Packages.pygame
python36Packages.cairosvg
python36Packages.virtualenv
];
}

14
images/bubble.svg Normal file
View File

@ -0,0 +1,14 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 -0.5 10 10" shape-rendering="crispEdges">
<metadata>Made with Pixels to Svg https://codepen.io/shshaw/pen/XbxvNj</metadata>
<path stroke="rgba(0,158,254,0.9372549019607843)" d="M3 0h1M0 3h1" />
<path stroke="rgba(0,221,254,0.9568627450980393)" d="M4 0h1M9 5h1" />
<path stroke="#00c2ff" d="M5 0h1M9 4h1M4 9h2" />
<path stroke="rgba(0,197,254,0.9372549019607843)" d="M6 0h1M3 9h1M6 9h1" />
<path stroke="rgba(0,167,254,0.796078431372549)" d="M1 1h1" />
<path stroke="#0090ff" d="M2 1h2M1 2h1M1 3h1M2 4h1M2 5h2M1 6h1" />
<path stroke="#00c1ff" d="M6 1h2M2 2h2M6 2h1M3 3h2M6 3h1M3 4h3M7 4h1M5 5h2M4 6h3M4 7h2M3 8h1M5 8h2" />
<path stroke="rgba(0,220,254,0.5490196078431373)" d="M8 1h1M1 8h1M8 8h1" />
<path stroke="#00deff" d="M8 2h1M8 3h1M8 6h1M1 7h1M8 7h1M2 8h1M7 8h1" />
<path stroke="rgba(0,221,255,0.7490196078431373)" d="M9 3h1M0 6h1M9 6h1" />
<path stroke="#0093ff" d="M0 4h1M0 5h1" />
</svg>

After

Width:  |  Height:  |  Size: 972 B