我正在尝试使用Pyglet在Python中创建“剪刀,石头,剪刀”游戏。在运行“ on_draw”功能时,我希望它允许用户输入“ A”代表岩石,“ P”代表纸张,“ S”代表剪刀,但是我找不到解决方案。我的一个想法是,当您启动“窗口”时,它将设置一个包含文本的背景,但是也找不到任何信息。如果您有任何想法并有一些空闲时间,我将非常感谢您的帮助。
一段时间以来,我一直在试图解决这个问题,但是我对Pyglet还是陌生的。
到目前为止,这是我的代码...
import pyglet
from pyglet.window import key
import random
import time
window = pyglet.window.Window(width = 1000, height = 700, resizable = False, caption = "Rock, Paper, Scissors!")
#images saved in root of the .py file
bg = pyglet.image.load('bg.png')
sprite_bg = pyglet.sprite.Sprite(img=bg)
# all images are defined correctly and sprites(tested it and all works)
#default choice of the computer player and user
num1 = 0
num2 = 0 #num2 is the user
@window.event
#Here is where I want it to say "Rock - A, Paper - S, Scissors - D"
def on_key_press(symbol, modifiers):
global num2
if symbol == key.A:
print ("Player: Rock")
num2 = 0
on_draw2()
elif symbol == key.S:
print ("Player: Paper")
num2 = 1
on_draw2()
elif symbol == key.D:
print ("Player: Scissors")
num2 = 2
on_draw2()
def on_draw():
window.clear()
sprite_bg.draw()
num1 = random.randint(0, 2)
if num1 == 0:
sprite_r_right.draw()
elif num1 == 1:
sprite_p_right.draw()
elif num1 == 2:
sprite_s_right.draw()
if num2 == 0:
sprite_r_left.draw()
elif num2 == 1:
sprite_p_left.draw()
elif num2 == 2:
sprite_s_left.draw()
if num1 == num2:
both_win.draw()
elif num2 == 0:
if num1 == 1:
right_win.draw()
else:
left_win.draw()
elif num2 == 1:
if num1 == 2:
right_win.draw()
else:
left_win.draw()
elif num2 == 2:
if num1 == 0:
right_win.draw()
else:
left_win.draw()
if __name__ == '__main__':
pyglet.app.run()
我希望程序像这样运行: 1.摇滚-A,纸-S,剪刀-D并等待输入a,s或d 2.然后将“ num2”(用户)设置为0、1或2(0-岩石,1-纸张,2- 剪刀) 3.随机将“ num1”(计算机播放器)与0、1或2 4.在两侧显示石头,纸或剪刀的图像,并说出谁赢了 5.显示分数(尚未实施) 6.重复(尚未实施)
程序运行如下: 1.黑屏(等待输入a,s或d) 2.执行上面列出的步骤2、3、4
答案 0 :(得分:0)
我们需要解决您的代码中的几个问题。
第一个是,on_draw
除非用户理想地按下,否则不会被调用。.在框架内的特定间隔/中断处调用此函数。
我们通过以下方式做到这一点:
@window.event # <-- This is key, if you forget this - the screen won't update
def on_draw():
window.clear()
sprite_bg.draw()
后来,您的代码使用了一堆不存在的变量,例如,sprite_r_right
从未定义。我猜这是一个代表岩石或类似物体的精灵。没关系,但是为了节省时间,我在下面的代码中将其替换为Label。
另一个问题是您在每个渲染循环上执行num1 = random.randint(0, 2)
。无需检查用户是否提供了选择。理想情况下,您将执行以下操作:
if num2 != 0:
num1 = random.randint(0, 2)
*(另一个简短的说明,这些变量名令人困惑,就像f ***一样,任何人都很难真正了解它们的用途。因此,在下面的代码中,我将其更改为代表某种东西就它们的用途或用途而言,这更合乎逻辑)*
这是一个有关如何设置逻辑的建议示例:
import pyglet
from pyglet.window import key
import random
import time
window = pyglet.window.Window(width = 1000, height = 700, resizable = False, caption = "Rock, Paper, Scissors!")
#images saved in root of the .py file
bg = pyglet.image.load('bg.png')
sprite_bg = pyglet.sprite.Sprite(img=bg)
sprite_player = pyglet.text.Label("", x=(window.width/3), y=window.height/2)
sprite_computer = pyglet.text.Label("", x=window.width-(window.width/3), y=window.height/2)
sprite_result = pyglet.text.Label("", x=window.width/2, y=window.height/3, anchor_x="center")
@window.event
def on_key_press(symbol, modifiers):
if symbol == key.A:
sprite_player.text = 'Rock'
elif symbol == key.S:
sprite_player.text = 'Paper'
elif symbol == key.D:
sprite_player.text = 'Scissors'
# Once the user presses a key, randomize the computers choice
rng = random.randint(0, 2)
if rng == 0:
sprite_computer.text = 'Rock'
elif rng == 1:
sprite_computer.text = 'Paper'
elif rng == 2:
sprite_computer.text = 'Scissors'
@window.event # <-- This is key, if you forget this - the screen won't update
def on_draw():
window.clear()
sprite_bg.draw()
if sprite_player.text != "" and sprite_computer.text != "":
if sprite_player.text == sprite_computer.text:
sprite_result.text = "Draw!"
elif sprite_computer.text == "Rock":
if sprite_player.text == "Paper":
sprite_result.text = "Player wins"
else:
sprite_result.text = "Computer wins"
elif sprite_computer.text == "Paper":
if sprite_player.text == "Scissors":
sprite_result.text = "Player wins"
else:
sprite_result.text = "Computer wins"
elif sprite_computer.text == "Scissors":
if sprite_player.text == "Rock":
sprite_result.text = "Player wins"
else:
sprite_result.text = "Computer wins"
else:
sprite_result.text = "Press A for Rock, S for Paper and D for Scissors"
sprite_player.draw()
sprite_computer.draw()
sprite_result.draw()
if __name__ == '__main__':
pyglet.app.run()
这将显示一条快速的“帮助”消息,并且在游戏开始后,将立即显示结果和选择。
一种更整洁的方法是使用面向对象的编程来解决很多渲染,保留问题。由于时间原因,我将不得不返回并使用该解决方案来编辑此答案,必须去做一些工作吧。