コード例 #1
0
ファイル: pong.go プロジェクト: gophercoders/pong
func initialiseBallDirection() {
	// pick some random numbers to determine if the ball will move up or down
	// and left or right initially.
	var n int
	n = random.GetRandomNumberInRange(1, 10)
	var up bool
	if isOddNumber(n) {
		up = true // we want the ball to move up - decreasing Y coordinate
	} else {
		up = false // we want the ball to move down - increasing y coordinate
	}

	n = random.GetRandomNumberInRange(1, 10)
	var left bool
	if isOddNumber(n) {
		left = true // we want the ball to move left - decreasing X coordiiate
	} else {
		left = false //we want the ball to move right - increasing X coordinate
	}
	// pick two random mumbers for the initial direction
	ballDirX = float64(random.GetRandomNumberInRange(1, 10))
	ballDirY = float64(random.GetRandomNumberInRange(1, 10))
	// are we moving left?
	if left {
		ballDirX = ballDirX * -1
	} // otherwise the ball is moving right so ballDirX should be positive
	// are we moving up?
	if up {
		// yes - make the number negative
		ballDirY = ballDirY * -1
	} // otherwise the ball is moving dwon so ballDirY should be positive
	// the vector now needs to be normalised
	setBallDirection(ballDirX, ballDirY)
}
コード例 #2
0
ファイル: timesquiz.go プロジェクト: gophercoders/codeclub
func main() {
	// declare some varaibels.
	// a and b are the variabels we are going to use to hold the question in
	var a int
	var b int
	// answer is the variable we are going to use for your answer
	var answer int

	// Tell the user what the program does.
	fmt.Println("The timesquiz shows you how to use loops.")
	fmt.Println("Can you remember your times tables?")
	fmt.Println("")

	// use random numbers again!
	// The program will pick a number between 1 and 12, including 1 and 12
	// and assign it to the variables a and b
	// If you run the program again, the value of a will be different because
	// the computer will have picked a different number.
	a = random.GetRandomNumberInRange(1, 12)
	b = random.GetRandomNumberInRange(1, 12)

	// Now we want to print the question
	// What is a * b?
	// but with the values of a and b
	fmt.Print("What is ")
	fmt.Print(a)
	fmt.Print(" * ")
	fmt.Print(b)
	fmt.Println("?")

	// now you have to read the users answer from the keyboard
	// using the input using pattern

	// now you need a loop!
	// You have to keep looping around while the users answer is
	// not equal to a * b
	// You need to use the loop pattern for this
	for {
		// your loop statement block starts here!
		// Now you need to use an if statement
		// if the users answer was to large print out
		// Sorry, your gueds was to big
		// otherwise the users guess is too small so print out
		// Sorry your guess was to small

		// now tell the user to try again

		// now you have to use the input pattern again to assign
		// the users next guess to the answer variable

	}
	// Now we are outside the loop! So the user must have guessed
	// correctly. Now you need to print Congratulations.

}
コード例 #3
0
func main() {
	// declare some varaibels.
	// a and b are the variabels we are going to use to hold the question in
	var a int
	var b int
	// answer is the variable we are going to use for your answer
	var answer int

	// Tell the user what the program does.
	fmt.Println("The timesquestion program shows you how to use if and else.")
	fmt.Println("Can you remember your times tables?")
	fmt.Println("")

	// This new!
	// a and b are both set to a random number.
	// The program will pick a number between 1 and 12, including 1 and 12
	// and assign it to the variable a
	// If you run the program again, the value of a will be different because
	// the computer will have picked a different number.
	a = random.GetRandomNumberInRange(1, 12)
	// b is also picked randomly!
	b = random.GetRandomNumberInRange(1, 12)

	// Now we want to print the question
	// What is a * b?
	// but with the values of a and b
	fmt.Print("What is ")
	fmt.Print(a)
	fmt.Print(" * ")
	fmt.Print(b)
	fmt.Println("?")

	// now you have to read the users answer from the keyboard
	// using the input using pattern

	// Now you have to fill in the rest!
	// Use the selection if else if pattern...

	// if the users gues is exacly a*b then you have to print
	// congratulations

	// else if the users guess is to large print out
	// Sorry you guess was to big and then print out the correct answer
	// else the users guess must have been too small so print out
	// Sorry your guess was too small and then print out the correct
	// answer

	// this sould be the last line!
	// Run the program again and you'll be asked a different question!
	fmt.Println("Run the program again to try another question.")
}