我正在尝试制作一款游戏。我想要一张图片跟随鼠标光标,但只有在我点击之后。我试图对它进行编程,但图片只出现而不是再次消失。但我希望它跟随鼠标光标,直到我再次点击。以下是应该执行此操作的代码的一部分:
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()
答案 0 :(得分:1)
你的内在循环似乎错了。
如果你blit()但你从不翻转(),屏幕将永远不会实际更新。
所以删除你的第二个,取消它的内容,然后处理"点击"变化不同。
编辑: 如果您使用here
所述的事件,也会更好答案 1 :(得分:0)
感谢kstenger我修复了它:
# -*- 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()