[英]Trying to make a quick video game
以下代碼應該采用輸入和循環; 根據您從 Weather_values 獲得的天氣(范圍為 2 的隨機數),在內部向上或向下計數“player_startPoint”的值。 你達到三分就贏得了比賽,但是,我的代碼永遠不會離開 while 循環。
編碼為 Python
# import random and set the range of values between 1 & 2
# remember that zero is the default lowest number when i+1
import pygame
import random
i = int
weather_values = random.randint(1,2)
# These values are used later to define the game state the user will encounter, if sunny move forward +1; if rainy move backwards -1
# These are most of the variables in general
Sunny = int = 1
Rainy = int = 2
player_startPoint = 0
# These values will later be replaced once I figure out how to negate case sensitive
yes = str
Yes = str
yes == Yes
no = str
No = str
No == no
# This prompt starts the game
print("This is the weather game")
Ask_string = input("Ask about the weather? (Yes/No): ")
weather_values == Sunny == 1
weather_values == Rainy == 2
if weather_values == Sunny:
print("Sunny")
player_startPoint + 1
else :
weather_values == Rainy
print("Rainy")
player_startPoint - 1
#The value Ask_string prompts the program to run the mathRand command to get a value of 1 or 2 that print out the weather assigned to either number
if Ask_string == "yes" or "Yes":
#Here we intitute the mathRand function to give the player insight and give them
# An input choice to proceed or stay put based on the forcast
# Printing the variable random abides by the range set and will give you a desired output,
# However, I am not sure if random and weather_values will indentify to the same value going forward if just random is used.
print (weather_values)
else:
print("Maybe later then")
# the sunny and rainy variables allow the program associate a value with the object
if weather_values == Sunny :
player_startPoint =+ 1
else :
player_startPoint =- 1
#for weather_values in range(1) :
#player_startPoint + 1
#else :
# player_startPoint - 1'
while player_startPoint < 3 :
Ask_string = input("Ask about the weather? (Yes/No): ")
if player_startPoint == 3 :
print(" Congrats! You've weathered the storm! ")
您的 while 循環只是一遍又一遍地要求玩家輸入並且什么都不做。 您應該定義一個 function 並在用戶輸入后的 while 循環中調用該 function。 您的語法有錯誤,您在player_startPoint
中添加或減去一個,請參閱代碼注釋。
這將使您的循環按預期工作,但您的游戲仍然卡在無限循環中,因為您在每個循環中添加 1 然后從player_startPoint
中減去 1
這段代碼不是很好,你可以做一些事情來讓它變得更好。 研究函數開始。
# import random and set the range of values between 1 & 2
# remember that zero is the default lowest number when i+1
import pygame
import random
i = int
weather_values = random.randint(1,2)
# These values are used later to define the game state the user will encounter, if sunny move forward +1; if rainy move backwards -1
# These are most of the variables in general
Sunny = int = 1
Rainy = int = 2
player_startPoint = 0
# These values will later be replaced once I figure out how to negate case sensitive
yes = str
Yes = str
yes == Yes
no = str
No = str
No == no
# This prompt starts the game
print("This is the weather game")
# this input is redundent you only ned to call it in the loop
# Ask_string = input("Ask about the weather? (Yes/No): ")
weather_values == Sunny == 1
weather_values == Rainy == 2
#define a function to call when the user inputs an answer
def checkWeather():
global player_startPoint #make player_startPoint global so it can be accessed from within the function
if weather_values == Sunny:
print("Sunny")
player_startPoint + 1
else :
weather_values == Rainy
print("Rainy")
player_startPoint - 1
#The value Ask_string prompts the program to run the mathRand command to get a value of 1 or 2 that print out the weather assigned to either number
if Ask_string == "yes" or "Yes":
#Here we intitute the mathRand function to give the player insight and give them
# An input choice to proceed or stay put based on the forcast
# Printing the variable random abides by the range set and will give you a desired output,
# However, I am not sure if random and weather_values will indentify to the same value going forward if just random is used.
print (weather_values)
else:
print("Maybe later then")
# the sunny and rainy variables allow the program associate a value with the object
if weather_values == Sunny :
player_startPoint += 1 # =+ is an invalid syntax it should be +=
else :
player_startPoint -= 1 # =+ is an invalid syntax it should be +=
#for weather_values in range(1) :
#player_startPoint + 1
#else :
# player_startPoint - 1'
while player_startPoint < 3 :
Ask_string = input("Ask about the weather? (Yes/No): ")
checkWeather() # call checkWeather function after user input
if player_startPoint == 3 :
print(" Congrats! You've weathered the storm! ")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.