PythonPygame移动图片当我

2024-04-27 02:27:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创造一个游戏。我想要一个图片跟随鼠标光标,但只有在我点击。我试着给它编程,但画面只出现了,然后又消失了。但我希望它跟随鼠标光标直到我再次单击。下面是代码的一部分:

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

如果需要,这里有一个完整的代码:

# -*- 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()

Tags: pathbottlegtkifoswidthscreenstart
2条回答

谢谢你,我修好了:

# -*- 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()

你内心的while循环似乎是错的。你知道吗

如果使用blit()但从不使用flip(),屏幕将永远不会实际更新。你知道吗

所以删除你的第二个while,取消它的内容,并可能以不同的方式处理“clicked”变量。你知道吗

编辑: 另外,如果您使用所描述的事件here会更好

相关问题 更多 >