![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Programming Guru
![]() |
A more complicated question.
I need to set an image to transparent, but I don't want to make an entire class out of it. Here's my entire program. See if you can work it out. I'll bold the line of code that I want to contain the transparency value. It's in the "character_move.py"
main.py from title_screen import * from area_2 import * from level_flow import * lvl = 5 maxhp = 30 curhp = 30 maxmp = 5 curmp = 5 str = 12 defn = 8 agi = 7 luck = 9 weapon = "Rusty Sword" armor = "Rusty Armor" turk = [lvl, maxhp, curhp, maxmp, curmp, str, defn, agi, luck, weapon, armor] party = [turk] title_screen() title_screen.py from game_settings import *
from level_flow import *
class logo_jdi(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('logo_jdi.bmp')
centerx = 800/2
centery = 700/2
self.rect.topleft = centerx-(585/2), centery-(533/2)
class scr_title(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('scr_title.bmp')
centerx = 800/2
centery = 700/2
self.rect.topleft = centerx-(497/2), centery-(384/2)
class scr_cursor1(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('cursor_set_1.bmp')
self.rect.topleft = 404, 425
class scr_cursor2(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('cursor_set_2.bmp')
self.rect.topleft = 403, 425
class scr_cursor3(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('cursor_set_3.bmp')
self.rect.topleft = 403, 425
class scr_cursor4(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('cursor_set_4.bmp')
self.rect.topleft = 403, 425
class game_info(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('game_info.bmp')
centerx = 800/2
centery = 700/2
self.rect.topleft = centerx-(497/2), centery-(384/2)
def load_scr_logo():
screen = video_settings()
time.sleep(2)
sound1 = load_sound('JDI.wav')
sound1.play()
layer1 = logo_jdi()
layers = pygame.sprite.RenderPlain((layer1))
screen = video_settings()
background = pygame.Surface(screen.get_size())
screen.blit(background, (0, 0))
layers.draw(screen)
pygame.display.flip()
time.sleep(3)
returnvalue = 0
while returnvalue == 0:
for event in pygame.event.get():
if event.type == KEYDOWN:
returnvalue = 1
elif event.type == MOUSEBUTTONDOWN:
returnvalue = 1
def load_scr_title(party):
screen = video_settings()
background = pygame.Surface(screen.get_size())
screen.blit(background, (0, 0))
time.sleep(3)
layer1 = scr_title()
layer2 = scr_cursor1()
layers = pygame.sprite.RenderPlain((layer1, layer2))
background = pygame.Surface(screen.get_size())
time.sleep(1)
sound1 = load_sound('Soulodine.wav')
sound1.play(99)
returnvalue = 0
cursorvalue = 1
while returnvalue == 0:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_UP:
if cursorvalue == 1:
cursorvalue = 1
layer2 = scr_cursor1()
elif cursorvalue == 2:
cursorvalue = 1
layer2 = scr_cursor1()
elif cursorvalue == 3:
cursorvalue = 2
layer2 = scr_cursor2()
elif cursorvalue == 4:
cursorvalue = 3
layer2 = scr_cursor3()
if event.key == K_DOWN:
if cursorvalue == 1:
cursorvalue = 2
layer2 = scr_cursor2()
elif cursorvalue == 2:
cursorvalue = 3
layer2 = scr_cursor3()
elif cursorvalue == 3:
cursorvalue = 4
layer2 = scr_cursor4()
elif cursorvalue == 4:
cursorvalue =4
layer2 = scr_cursor4()
if event.key == K_RETURN:
if cursorvalue == 1:
cursorvalue = 1
level_flow(party)
elif cursorvalue == 2:
cursorvalue = 2
elif cursorvalue == 3:
returnvalue_2 = 0
while returnvalue_2 == 0:
layer3 = game_info()
layers.update()
screen.blit(background, (0, 0))
layers.draw(screen)
pygame.display.flip()
layers = pygame.sprite.RenderPlain((layer1, layer2, layer3))
time.sleep(1)
for event in pygame.event.get():
if event.type == KEYDOWN or event.type == MOUSEBUTTONDOWN:
pygame.sprite.Sprite.kill(layer3)
time.sleep(1)
returnvalue_2 = 1
elif cursorvalue == 4:
sound1.fadeout(3500)
quit_game()
return
background = pygame.Surface(screen.get_size())
screen.blit(background, (0, 0))
layers.update()
screen.blit(background, (0, 0))
layers.draw(screen)
pygame.display.flip()
layers = pygame.sprite.RenderPlain((layer1, layer2))
def title_screen(party):
load_scr_logo()
load_scr_title(party)game_settings.py import os, pygame, time
from pygame.locals import *
from quit_game import *
if not pygame.font: print 'Warning, fonts disabled'
if not pygame.mixer: print 'Warning, sound disabled'
def load_image(name, colorkey=None):
fullname = os.path.join('', name)
try:
image = pygame.image.load(fullname)
except pygame.error, message:
print 'Cannot load image:', fullname
raise SystemExit, message
image = image.convert()
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0,0))
image.set_colorkey(colorkey, RLEACCEL)
return image, image.get_rect()
def load_sound(name):
class NoneSound:
def play(self): pass
if not pygame.mixer or not pygame.mixer.get_init():
return NoneSound()
fullname = os.path.join('sound', name)
try:
sound = pygame.mixer.Sound(fullname)
except pygame.error, message:
print 'Cannot load sound:', fullname
raise SystemExit, message
return sound
def video_settings():
pygame.init()
screen = pygame.display.set_mode((800, 700))
pygame.display.set_caption('Soulodine v 0.2')
pygame.mouse.set_visible(1)
return screenlevel_flow.py from game_settings import*
from area_2 import *
def level_flow(party):
party = area_2(party)area_2.py from character_move import *
from game_settings import *
from battle_screen import *
def average_enemy_stats(e_lvl, e_hp, e_mp, e_str, e_defn, e_agi, e_luck):
enemy = [e_lvl, e_hp, e_mp, e_str, e_defn, e_agi, e_luck]
return enemy
def area_2(party):
character_location = [50, 50]
collision_1 = ["rectangle",100, 200, 150, 250]
collision_2 = ["point", 600, 600]
collision_3 = ['','']
collision_4 = ['','']
collision_5 = ['','']
collisions = [collision_1, collision_2, collision_3, collision_4, collision_5]
screen = video_settings()
pygame.draw.rect(screen, (255, 255, 255), ( (250, 250), (550, 450) ), 0)
character_location = character_move(screen, "area_map_2.bmp", character_location)
incr = 10
step = "nostep"
pos = "front"
character_draw(screen, step, pos)
area = area_2
e_lvl = (random.randrange(1,3))
enemy = average_enemy_stats( (random.randrange(1,3)),
(((e_lvl + 5) * 2) + random.randrange(0, 3)),
(0),
((e_lvl) * 2),
((e_lvl) * 2) + random.randrange(-2, 2),
((e_lvl) * 2) + random.randrange(-2, 4),
((e_lvl) * 2) + random.randrange(-3, 1) )
while 1:
for event in pygame.event.get():
if event.type == KEYDOWN:
c_o_l_l_i_s_i_o_n = check_collision(collisions, character_location, 0)
rand_battle_a = random.randrange(1, 100)
rand_battle_b = random.randrange(1, 100)
def rand_battle(party):
if rand_battle_a == rand_battle_b:
party = battle(party, enemy, area_2)
if c_o_l_l_i_s_i_o_n == 0:
if event.key == K_UP:
character_location[1] = character_location[1] + incr
rand_battle(party)
event_key = event.key
elif event.key == K_DOWN:
character_location[1] = character_location[1] - incr
rand_battle(party)
event_key = event.key
elif event.key == K_LEFT:
character_location[0] = character_location[0] + incr
rand_battle(party)
event_key = event.key
elif event.key == K_RIGHT:
character_location[0] = character_location[0] - incr
rand_battle(party)
event_key = event.key
c_o_l_l_i_s_i_o_n = check_collision(collisions, character_location, 0)
if c_o_l_l_i_s_i_o_n == 1:
if event.key == K_UP:
character_location[1] = character_location[1] - incr
elif event.key == K_DOWN:
character_location[1] = character_location[1] + incr
elif event.key == K_LEFT:
character_location[0] = character_location[0] - incr
elif event.key == K_RIGHT:
character_location[0] = character_location[0] + incr
character_location = character_move(screen, "area_map_2.bmp", character_location)
step, pos = character_change(step, pos, event_key)
character_draw(screen, step, pos)character_move.py from game_settings import *
def check_collision(collisions, character_location, c_o_l_l_i_s_i_o_n):
for collision in range( 0, len(collisions) ):
if collisions[collision][0] == "rectangle":
if character_location[0] > (collisions[collision][1]):
if character_location[0] < (collisions[collision][3]):
if character_location[1] > (collisions[collision][2]):
if character_location[1] < (collisions[collision][4]):
c_o_l_l_i_s_i_o_n = 1
return c_o_l_l_i_s_i_o_n
elif collisions[collision][0] == "point":
if character_location[0] > (collisions[collision][1]-5):
if character_location[0] < (collisions[collision][1]+5):
if character_location[1] > (collisions[collision][2]-5):
if character_location[1] < (collisions[collision][2]+5):
c_o_l_l_i_s_i_o_n = 1
return c_o_l_l_i_s_i_o_n
else:
if collision == len(collisions)-1:
c_o_l_l_i_s_i_o_n = 0
return c_o_l_l_i_s_i_o_n
def character_change(step, pos, event_key):
def step_change(step):
if step == "nostep":
step = "step_1"
elif step == "step_1":
step = "step_2"
elif step == "step_2":
step = "step_1"
return step
if event_key == K_UP:
pos = "back"
step = step_change(step)
elif event_key == K_DOWN:
pos = "front"
step = step_change(step)
elif event_key == K_LEFT:
pos = "side1"
step = step_change(step)
elif event_key == K_RIGHT:
pos = "side2"
step = step_change(step)
return step, pos
def character_draw(screen, step, pos):
pic_load = "main_character_" + pos + "_" + step + ".bmp"
char_draw = pygame.image.load(pic_load)
screen.blit(char_draw, (350, 350) )
pygame.display.flip()
def character_move(screen, area_map, character_location):
map_draw = pygame.image.load(area_map)
screen.blit(map_draw, character_location)
pygame.draw.rect(screen, 0, ( (0, 0), (250, 700) ), 0)
pygame.draw.rect(screen, 0, ( (250, 0), (800, 250) ), 0)
pygame.draw.rect(screen, 0, ( (550, 250), (800, 700) ), 0)
pygame.draw.rect(screen, 0, ( (250, 450), (550, 700) ), 0)
pygame.display.flip()
clock = pygame.time.Clock()
clock.tick(60)
return character_locationbattle_screen.py def check_status(party, enemy):
if party[0][2] < 1:
status = "dead"
elif enemy[1] < 1:
status = "e_dead"
else:
status = "0"
return status
def battle(party, enemy, area):
##create screen
##create background based on area variable
##load music
if party[0][7] < (enemy[5]+1):
turn = "enemy"
else:
turn = "you"
battle_return = 0
while battle_return == 0:
if turn == "enemy":
##computer AI
turn = "you"
status = check_status(party, enemy)
if status != "0":
battle_return = 1
if turn == "you":
##choice of commands
##results of choice
turn = "enemy"
status = check_status(party, enemy)
if status != "0":
battle_return = 1
#print enemy
#print party
#print status
##
##status = "0"
##return party variableThe code will not work along, unless you have all my pictures, sounds, and save all my modules correctly...and have pygame. |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Wow, don't think you needed to post all of that.
Could you clarify: You want the image to be 'transparent'. By that do you mean that you want the image to have transparent parts (so in parts you can see through to the background), or do you mean you want the whole image to be fully transparent (so.. no image)? |
|
|
|
|
|
#3 |
|
Programming Guru
![]() |
lol, if I wanted the function of no image, I'd just add an argument to disclude the image. I want white to be transparent. And I posted the whole thing so you could see when the functions come from.
It's able to be done if you make it into a class, then make the def __init(self): function yield a load image pygame function in which you can add the argument for a transparent color code. I tried adding an extra argument for color code in the one I have now, but it doesn't accept it. I just want to know if there is an easier way then making a game object out of it. ![]() Last edited by Sane; Apr 6th, 2005 at 3:49 PM. |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Well uh.. IIRC pygame already does the whole transparency thing for you if you use a PNG?
Just load the transparent/semi-transparent PNG and blit it straight on. And also... you say you can make this work if you "make a class out of it" - you haven't clarified on _why_ exactly. If i'm way off just say so ![]() |
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
What's IIRC pygame?
And look at the title_screen.py up in the first post. There are like 4 classes that all share similar code right off the bat. self.image, self.rect = load_image('logo_jdi.bmp') Can be transparent by: self.image, self.rect = load_image('logo_jdi.bmp', -1) But I try that it when it's not inside a class: char_draw = pygame.image.load(pic_load) --> char_draw = pygame.image.load(pic_load, -1) And it doesn't recognize the argument, because it's a whole different function meant to just display a picture, and not use it as a game object. |
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
IIRC == If I Recall Correctly.
I see. You can load the image and make all white regions transparent by passing -1 to this `load_image` function, which returns the image and a rectangle representing the size, but you don't want that. You want whatever is returned by pygame.image.load. Like I said, I think it will "just work" if you use a PNG instead of a BMP - PNG's have native support for transparency and semi-transparency. Open up the bmp file in your favourite image editor with support for drawing transparent pixels (the GIMP, Photoshop, Kolourpaint) and replace all the white pixels with transparent pixels. Just save it as a png file and then load that. Good luck! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|