Пример #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()
	}
}
Пример #2
0
func loop(screen *sdl.Surface) {
	for {
		screen.Flip()
		event := sdl.PollEvent()
		if event != nil {
			if _, ok := event.(*sdl.QuitEvent); ok {
				log.Println("Caught quit event. Quiting SDL.")
				break
			}
		}
	}
}
Пример #3
0
func playGame(screen *sdl.Surface, bg *glh.Texture, font *gltext.Font) (int, bool) {
	movingUp := false
	b := bird.NewBird(240, 380)
	score := 0
	var pipes []*pipe.Pipe
	ticker := 0
	for {
		gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
		quit := manageEvents(screen, &movingUp)
		if quit {
			return score, true
		}

		renderBackground(screen, bg)
		ticker++
		if ticker > 100 {
			pipes = append(pipes, pipe.NewPipe(int(screen.W), int(screen.H)))
			ticker = 0
		}

		if movingUp {
			b.MoveUp()
		} else {
			b.MoveDown()
		}

		for _, p := range pipes {
			if p.X < 0 {
				p.Destroy()
				pipes = pipes[1:]
			}
			if p.CollidesWith(b.X, b.Y, b.Width, b.Height) {
				return score, false
			}
			if p.GonePast(b.X, b.Y) {
				score++
			}
			p.Tick()
			p.Render()
		}
		b.Render()

		font.Printf(240, 50, strconv.Itoa(score))
		screen.Flip()
		time.Sleep((1 / 30) * time.Second)
		sdl.GL_SwapBuffers()
	}
}