问题描述
我开始在隔离期间学习代码,并决定学习如何使用pygame(因为我只了解python的基础知识),因此我在youtube上观看了一个教程,“通过制作飞扬的小鸟来学习pygame”。教程非常好,但是那家伙忘记了得分在游戏中的工作方式,而是写了一个计时器,将分数加1。在过去一周左右的时间里,我一直在努力让游戏识别出鸟和烟斗x的位置是否相同,以便为得分加1分,但我一直无法弄清楚。我还没有写任何关于分数变化的信息,因此,如果有人可以伸出援手,我将非常感激。 很抱歉,如果代码有些混乱,就像我说的那样,我才刚刚开始学习。
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import TfidfVectorizer
import pandas as pd
documents = ['I went to the barbershop when my hair was long.','The barbershop was closed.']
# create set of stopwords to remove
stop_words = set(stopwords.words('italian'))
english_stop_words = set(stopwords.words('english'))
stop_words.update(english_stop_words)
# check if word in stop words
print('when' in stop_words) # True
print('il' in stop_words) # True
# else add word to the set
print('went' in stop_words) # False
stop_words.add('went')
# create tf-idf from original documents
tfidf = TfidfVectorizer(stop_words=stop_words)
x = tfidf.fit_transform(documents)
df_tfidf = pd.DataFrame(x.toarray(),columns=tfidf.get_feature_names())
print({c: s[s > 0] for c,s in zip(df_tfidf,df_tfidf.T.values)})
# {'barbershop': array([0.44943642,0.57973867]),'closed': array([0.81480247]),'hair': array([0.6316672]),'long': array([0.6316672])}
解决方法
因此,以上评论中的要点是100%正确的解决方法。在您的游戏中,您可以简化使用它,这可能会适合您的技能水平。
您的游戏正在以恒定速率连续滚动。...您似乎正在将管道向左移动每帧3个单位。因此,您的挑战更加具体。您只需要跟踪其中一根管道何时“穿过”鸟的x坐标即可。
注意:在更复杂的游戏中,您可能如上所述具有多项检查...
对于您的构建,您具有管道列表和实例变量centerx
。因此,您可以使用快速功能来检查所有管道,类似于move_pipes
函数,以查看中心是否在鸟的x坐标的1个运动范围内。用伪代码:
for pipe in pipes:
if bird.x - step_size < pipe.centerx <= bird.x:
# add to score
假设step_size
可能是移动量的全局变量,您将在其他代码中使用该变量以确保其准确性。无论您的鸟的x坐标如何,它都会起作用,这在游戏中已得到解决。
也...您会注意到,您将继续添加到管道列表中。如果您在游戏中获得 good ,这最终将导致游戏速度变慢,因为pygame仍然会尝试绘制这些内容,即使它们不在可见窗口中也是如此。 (它将进行数学运算,但是您什么也看不到)。您不必担心列表的大小,列表的大小可以毫无问题地扩大到数百万,但是您可以通过检查绘图函数中的x坐标来限制绘制它们的方式,类似于现在对y坐标所做的操作。 ..
if pipe.centerx < -10:
pass
else:
if pipe.bottom ...:
# draw stuff
else:
# draw other stuff