Vítek Peterka
Vítek Peterka

Reputation: 99

Python-Pygame moving picture when i click

I'm trying to create a game. I want a picture to follow the mouse cursor but only after I click. I tried to program it but the picture only appears and than disappears again. But I want it to follow the mouse cursor until I click again. Here is a part of the code that should do it:

if mouse.get_pressed()[0]==1:
        clicked = "yes"
    if clicked=="yes":
        while 1:
            screen.blit(explosive_bootle_obr,(mouse_position))
            if mouse.get_pressed()[0]==1:
                break

And here is a full code if needed:

# -*- coding: utf-8 -*-


#Alchymist Lab 2015
#you can use this as you wish


#FIRST VERSION

from Tkinter import *
import base64
from pygame import *
import gtk
import os

mycolor = 0,255,0

def first_time():
    gamesave = [base64.b64encode("0"), base64.b64encode("0"), base64.b64encode("0")]
    file = open(os.path.dirname(os.path.abspath(__file__))+"/gamesave.txt", "w")
    file.write(str(gamesave))
    file.close
    hra(0,0,0)

def hra(p,s,sp):
    start_panel=gtk.gdk.screen_width()-2*gtk.gdk.screen_height()/5
    base_width=start_panel+219-1
    base_start_panel=800
    real_width=gtk.gdk.screen_width()
    add_width=real_width-base_width

    effect_bottle=os.path.dirname(os.path.abspath(__file__)) + "/.purple/smileys/purple-original/skype/emoticon-0104-surprised.gif"
    effect_bottle_obr=image.load(effect_bottle)
    water_bottle=os.path.dirname(os.path.abspath(__file__))+ "/.purple/smileys/purple-original/skype/emoticon-0104-surprised.gif"
    water_bottle_obr=image.load(water_bottle)
    explosive_bottle=os.path.dirname(os.path.abspath(__file__))+ "/.purple/smileys/purple-original/skype/emoticon-0104-surprised.gif"
    explosive_bottle_obr=image.load(explosive_bottle)

    width = gtk.gdk.screen_width()
    height = gtk.gdk.screen_height()
    while 1:
        clicked="no"
        screen = display.set_mode((width, height))
        panel = screen.subsurface (start_panel,0,219+add_width-4,gtk.gdk.screen_height())
        screen.fill([0,0,255])
        panel.fill([0,255,0])
        mouse_position=mouse.get_pos()
        panel.blit(effect_bottle_obr,(0,0))
        panel.blit(water_bottle_obr,(0,160))
        if mouse.get_pressed()[0]==1:
            clicked = "yes"
        if clicked=="yes":
            while 1:
                screen.blit(explosive_bottle_obr,(mouse_position))
                if mouse.get_pressed()[0]==1:
                    break

        display.flip()

print gtk.gdk.screen_width(), gtk.gdk.screen_height()




def continue_game():
    #my note not needed to translate
    print "nacist do 3 proměných peníze , super peníze , skóre"

okno=Tk()
okno.title("alchimist lab")

start=Button(okno, text="new game", command=first_time)
start.pack()

continue_game=Button(okno, text=" continue ", command=continue_game)
continue_game.pack()



okno.mainloop()

Upvotes: 0

Views: 130

Answers (2)

Vítek Peterka
Vítek Peterka

Reputation: 99

thanks kstenger i fixed it:

# -*- coding: utf-8 -*-


#Alchymist Lab 2015
#you can use this as you wish


#FIRST VERSION

from Tkinter import *
import base64
from pygame import *
import gtk
import os

def first_time():
    gamesave = [base64.b64encode("0"), base64.b64encode("0"), base64.b64encode("0")]
    file = open(os.path.dirname(os.path.abspath(__file__))+"/gamesave.txt", "w")
    file.write(str(gamesave))
    file.close
    hra(0,0,0)

def hra(p,s,sp):
    start_panel=gtk.gdk.screen_width()-2*gtk.gdk.screen_height()/5
    base_width=start_panel+219-1
    base_start_panel=800
    real_width=gtk.gdk.screen_width()
    add_width=real_width-base_width

    effect_bottle=os.path.dirname(os.path.abspath(__file__)) + "/efektova_lahev.GIF"
    effect_bottle_obr=image.load(effect_bottle)
    water_bottle=os.path.dirname(os.path.abspath(__file__))+ "/vodni_lahev.GIF"
    water_bottle_obr=image.load(water_bottle)
    explosive_bottle=os.path.dirname(os.path.abspath(__file__))+ "/vybusna_lahev.GIF"
    explosive_bottle_obr=image.load(explosive_bottle)

    width = gtk.gdk.screen_width()
    height = gtk.gdk.screen_height()
    while 1:
        clicked="no"
        screen = display.set_mode((width, height))
        panel = screen.subsurface (start_panel,0,219+add_width-4,gtk.gdk.screen_height())
        screen.fill([0,0,255])
        panel.fill([0,255,0])
        mouse_position=mouse.get_pos()
        panel.blit(effect_bottle_obr,(0,0))
        panel.blit(water_bottle_obr,(0,160))


        if mouse.get_pressed()[0]==1:
            clicked = "yes"
        if clicked=="yes":
            while 1:
                screen = display.set_mode((width, height))
                panel = screen.subsurface (start_panel,0,219+add_width-4,gtk.gdk.screen_height())
                screen.fill([0,0,255])
                panel.fill([0,255,0])
                mouse_position=mouse.get_pos()
                panel.blit(effect_bottle_obr,(0,0))
                panel.blit(water_bottle_obr,(0,160))
                screen.blit(explosive_bottle_obr,(mouse_position))
                display.flip()
                if mouse.get_pressed()[0]==1:
                    break

        display.flip()

print gtk.gdk.screen_width(), gtk.gdk.screen_height()




def continue_game():
    #my note not needed to translate
    print "nacist do 3 proměných peníze , super peníze , skóre"

okno=Tk()
okno.title("alchimist lab")

start=Button(okno, text="new game", command=first_time)
start.pack()

continue_game=Button(okno, text=" continue ", command=continue_game)
continue_game.pack()

end=Button(okno, text="     end     ", command=exit)
end.pack()

Upvotes: 0

kstenger
kstenger

Reputation: 374

Your inner while loop seems wrong.

If you blit() but you never flip(), the screen will never be actually updated.

So remove your second while, unindent it's contents, and maybe handle the "clicked" variable differently.

EDIT: Also it will be better if you use events as described here

Upvotes: 1

Related Questions