Exemple #1
0
func gameloop(screen *sdl.Surface) {
	gc := &GameContext{screen, 320, 16}

	for {
		e := sdl.WaitEvent()

		screen.FillRect(nil, 0x0)

		gc.drawFloor()
		gc.drawPlayer()
		gc.drawObjects()

		switch re := e.(type) {
		case *sdl.QuitEvent:
			return

		case *sdl.MouseMotionEvent:

			screen.FillRect(&sdl.Rect{
				int16(re.X),
				int16(re.Y),
				50, 50}, 0xffffff)

			screen.Blit(&sdl.Rect{
				int16(re.X),
				int16(re.Y),
				0, 0}, playerTexture, nil)

			fmt.Println(re.X, re.Y)

		case *sdl.KeyboardEvent:
			if re.Type == sdl.KEYDOWN {
				keyname := sdl.GetKeyName(sdl.Key(re.Keysym.Sym))
				fmt.Println("pressed:", keyname)

				switch keyname {
				case "right":
					gc.moveRight()
				case "left":
					gc.moveLeft()
				case "q":
					return
				}
			} else if re.Type == sdl.KEYUP {
				gc.resetPlayerSpeed()
			}
		default:
			//fmt.Println("What the heck?!")
		}

		gc.Dump()

		screen.Flip()
	}
}
Exemple #2
0
func main() {
	sdl.Init(sdl.INIT_VIDEO)
	ttf.Init()
	screen := sdl.SetVideoMode(480, 560, 16, sdl.OPENGL|sdl.RESIZABLE)
	sdl.WM_SetCaption("Flappy Bird", "")
	bg := utils.TextureFromFile("./bg.png")
	font := utils.LoadFont("/usr/share/fonts/truetype/DroidSans.ttf", 32)
	reshape(int(screen.W), int(screen.H))

	renderBackground(screen, bg)
	font.Printf(110, 50, "Click to play")
	sdl.GL_SwapBuffers()
	for {
	OuterLoop:
		for {
			e := sdl.WaitEvent()
			switch e.(type) {
			case *sdl.MouseButtonEvent:
				if e.(*sdl.MouseButtonEvent).Type == sdl.MOUSEBUTTONUP {
					break OuterLoop
				}
			}
		}

		score, quit := playGame(screen, bg, font)
		if quit {
			break
		}

		quit = gameOverScreen(screen, strconv.Itoa(score), bg, font)
		if quit {
			break
		}
	}

	screen.Free()
	ttf.Quit()
	sdl.Quit()
	return
}