私はPythonの初心者で、現在pygameを使用しています。ユーザー入力が必要なゲームを作っています。私は「Typing_Questions」と呼ばれるアルゴリズムを持っており、ユーザーがミスをした場合のスコアと生活の条件があります。このアルゴリズムは正しく実行されます。pygameの下部でスクリプトを実行するため、別の画面が読み込まれません。
Typing_Questions:
Score = 0
Lives = 3
Question_4 = "Code print"
Answer = "print"
print(T_Question_4)
user_input = input("Enter: ")
if user_input == Answer:
Lives = Lives
Score = Score + 1
print(Lives)
print(Score)
print("Well Done!")
while user_input != Answer:
user_input = input()
if user_input == Answer:
Lives = Lives
Score = Score + 1
print(Lives)
print(Score)
print("Well Done!")
else:
Lives = Lives - 1
Score = Score - 1
print(Lives)
if Score == -1:
Score = 0
print(Score)
if Lives == 0:
print("Game Over")
quit()
else:
print("Try again.")
print(T_Question_4)
入力テキストボックスのアルゴリズムもあります。これにより、何でも入力して別の画面を読み込むことができます。これも正しく機能します。
テキストボックス:
import pygame as pg
def main():
screen = pg.display.set_mode((1275, 775))
font = pg.font.Font(None, 32)
clock = pg.time.Clock()
input_box = pg.Rect(400, 300, 142, 32)
color_inactive = pg.Color('lighskyblue3')
color_active = pg.Color('dodgerblue2')
color = color_inactive
active = False
text = ''
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
if event.type == pg.MOUSEBUTTONDOWN:
if input_box.collidepoint(event.pos):
active = not active
else:
active = False
color = color_active if active else color_inactive
if event.type == pg.KEYDOWN:
if active:
if event.key == pg.K_RETURN:
print(text)
text = ''
elif event.key == pg.K_BACKSPACE:
text = text[:-1]
else:
text += event.unicode
screen.fill(250, 250, 250)
txt_surface = font.render(text, True, color)
width = max(200, txt_surface.get_width()+10)
input_box.w = width
screen.blit(txt_surface, (input_box.x + 5, input_box.y + 5))
pg.draw.rect(screen, color, input_box, 2)
pg.display.flip()
clock.tick(30)
if __name__ == '__main__':
pg.init()
main()
pg.quit()
私の目的は、これら2つのモジュールをマージすることです。もう一方を自分のモジュールにインポートしようとしましたが、Typing_Questionsのみが実行され、テキストボックスモジュールの画面が読み込まれません。その逆も同様です。
ユーザーがそこから回答を入力できるように、テキストボックス画面を表示したいと思います。彼らがボックスでそれに答えるとき、私はプログラムがTyping_Questionsアルゴリズムからのスクリプトに従うことを望みます(それでそれは答えだけを受け入れるか、それは寿命を縮めるか、必要ならばプログラムを終了します)。
これを行う方法はありますか?ありがとうございました。
pygameでテキスト入力ボックスを作成する方法の回答の例を使用しますか?ssアプリケーションの基礎。この例には、テキスト入力ボックスを実装する完全なクラスが含まれています。
アプリケーションループを実装する必要があります。アプリケーションループの実装の詳細については、「PyGameアプリケーションがまったく実行されないのはなぜですか?」の回答を参照してください。。
典型的なPyGameアプリケーションループは次のことを行う必要があります。
pygame.event.pump()
またはのいずれかでイベントを処理しますpygame.event.get()
。blit
すべてのオブジェクト)を描画しますpygame.display.update()
またはのいずれかで表示を更新しますpygame.display.flip()
質問のリストと質問への回答のリストを作成します。以下のための変数を指定しscore
、lives
そしてcurrent_question
。例えば:
questions = ["question 1", "question 2", "question 3"]
answer = ["answer 1", "answer 2", "answer 3"]
current_question = 0
lives = 3
score = 0
アプリケーションループを使用して、テキスト入力フィールドから入力を取得します。その結果を評価し、ための変数を変更score
、lives
およびcurrent_question
それに応じて:
while run:
# [...]`
if lives > 0 and current_question < len(questions):
group.update(event_list)
if not text_input_box.active:
if text_input_box.text == answer[current_question]:
score += 1
current_question += 1
else:
lives -= 1
text_input_box.reset()
参照してくださいフォント&カラーでPythonの表示テキストを?テキスト出力を生成します。入力用:
game_over_surf = font100.render("Game Over", True, (255, 255, 255))
window.blit(game_over_surf, game_over_surf.get_rect(center = window.get_rect().center))
完全な例:
import pygame
class TextInputBox(pygame.sprite.Sprite):
def __init__(self, x, y, w, font):
super().__init__()
self.color = (255, 255, 255)
self.backcolor = None
self.pos = (x, y)
self.width = w
self.font = font
self.active = True
self.text = ""
self.render_text()
def render_text(self):
t_surf = self.font.render(self.text, True, self.color, self.backcolor)
self.image = pygame.Surface((max(self.width, t_surf.get_width()+10), t_surf.get_height()+10), pygame.SRCALPHA)
if self.backcolor:
self.image.fill(self.backcolor)
self.image.blit(t_surf, (5, 5))
pygame.draw.rect(self.image, self.color, self.image.get_rect().inflate(-2, -2), 2)
self.rect = self.image.get_rect(topleft = self.pos)
def reset(self):
self.text = ""
self.active = True
self.render_text()
def update(self, event_list):
for event in event_list:
if event.type == pygame.KEYDOWN and self.active:
if event.key == pygame.K_RETURN:
self.active = False
elif event.key == pygame.K_BACKSPACE:
self.text = self.text[:-1]
else:
self.text += event.unicode
self.render_text()
questions = ["question 1", "question 2", "question 3"]
answer = ["answer 1", "answer 2", "answer 3"]
current_question = 0
lives = 3
score = 0
pygame.init()
window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()
font100 = pygame.font.SysFont(None, 100)
font50 = pygame.font.SysFont(None, 50)
text_input_box = TextInputBox(20, 170, 460, font100)
group = pygame.sprite.Group(text_input_box)
run = True
while run:
clock.tick(60)
event_list = pygame.event.get()
for event in event_list:
if event.type == pygame.QUIT:
run = False
if lives > 0 and current_question < len(questions):
group.update(event_list)
if not text_input_box.active:
if text_input_box.text == answer[current_question]:
score += 1
current_question += 1
else:
lives -= 1
text_input_box.reset()
window.fill(0)
if lives == 0:
game_over_surf = font100.render("Game Over", True, (255, 255, 255))
window.blit(game_over_surf, game_over_surf.get_rect(center = window.get_rect().center))
elif current_question < len(questions):
question_surf = font100.render(questions[current_question], True, (255, 255, 255))
window.blit(question_surf, (20, 50))
group.draw(window)
else:
well_done_surf = font100.render("Well Done!", True, (255, 255, 255))
window.blit(well_done_surf, well_done_surf.get_rect(center = window.get_rect().center))
score_surf = font50.render("score: " + str(score), True, (255, 255, 255))
window.blit(score_surf, (20, 400))
lives_surf = font50.render("lives: " + str(lives), True, (255, 255, 255))
window.blit(lives_surf, (300, 400))
pygame.display.flip()
pygame.quit()
exit()
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加