Exemple #1
0
func PressKey() int {
	for ev := sdl.WaitEvent(); ev != nil; ev = sdl.WaitEvent() {
		switch e := ev.(type) {
		case *sdl.QuitEvent:
			ExitProgram()
		case *sdl.KeyboardEvent:
			if e.Type == sdl.KEYDOWN {
				return int(e.Keysym.Sym)
			}
		}
	}
	return 0
}
Exemple #2
0
func MainLoop() {
	//Initializing More stuff
	lvl := Level_x()
	lvl.CreateSurface(screen)
	But := SetButtons()
	state = SELECT
	fmt.Println("state: Select")

	for ev := sdl.WaitEvent(); ev != nil; ev = sdl.WaitEvent() {
		switch e := ev.(type) {
		case *sdl.QuitEvent:
			return
		case *sdl.MouseButtonEvent:
			if e.Type == sdl.MOUSEBUTTONDOWN {
				mouse := Rect{float64(e.X), float64(e.Y), 0, 0}

				if state != DRAGGING || state != DRAGGING|ADDWAYPOINT {
					if But.TryClick(mouse) {
						if state != PRINT {
							continue
						}
						//Print
						state = SELECT
						j := NewJSONlevel(objects)
						j.Print(lvl)
						continue
					}
				}

				switch state {
				case FLIPTILE:
					x := int(mouse.X / BOXSIZE)
					y := int(mouse.Y / BOXSIZE)
					if x >= 18 {
						break
					}

					if lvl.Grid[y][x] == 'W' {
						lvl.Grid[y][x] = '.'
					} else {
						lvl.Grid[y][x] = 'W'
					}
					lvl.CreateSurface(screen)
				case SELECT:
					old := selected
					so := objects.Select(mouse)
					if so == nil {
						break
					}
					if old == so {
						fmt.Println("State: DRAGGING")
						state = DRAGGING
					}
				case DRAGGING:
					fmt.Println("State: SELECT")
					state = SELECT

				case ADDWAYPOINT:
					so := objects.Select(mouse)
					if so == nil {
						break
					}
					fmt.Println("State: DRAGGING | ADDWAYPOINT")
					so.OldX, so.OldY = so.X, so.Y
					state = DRAGGING | ADDWAYPOINT

				case DRAGGING | ADDWAYPOINT:
					fmt.Println("State: SELECT")
					state = SELECT
					if selected != nil {
						selected.Waypoints = append(selected.Waypoints, Vector64{mouse.X, mouse.Y})
						selected.X = selected.OldX
						selected.Y = selected.OldY

						fmt.Println(selected)
					}

				case REMOVEOBJECT:
					fmt.Println("State: SELECT")
					state = SELECT
					so := objects.Select(mouse)
					if so == nil {
						break
					}
					objects = objects.RemoveValue(so)
					selected = nil

				case CLEARWAYPOINTS:
					fmt.Println("State: SELECT")
					state = SELECT
					so := objects.Select(mouse)
					if so == nil {
						break
					}
					so.Waypoints = []Vector64{}
					fmt.Println(selected)
				}
			}

		case *sdl.MouseMotionEvent:
			mouse := Rect{float64(e.X), float64(e.Y), 0, 0}
			switch state {
			case DRAGGING, DRAGGING | ADDWAYPOINT:
				if selected != nil {
					selected.X = mouse.X
					selected.Y = mouse.Y
				}
			}

		case *sdl.KeyboardEvent:
			if selected == nil {
				break
			}
			if e.Type == sdl.KEYDOWN {
				switch e.Keysym.Sym {
				case sdl.K_1:
					selected.Type = SO_PLAYER
				case sdl.K_2:
					selected.Type = SO_GHOST
				case sdl.K_3:
					selected.Type = SO_MALBORO
				case sdl.K_4:
					selected.Type = SO_DEATHSHELL
				case sdl.K_5:
					selected.Type = SO_ROBOT
				case sdl.K_6:
					selected.Type = SO_SPIKE
				case sdl.K_7:
					selected.Type = SO_TELEPORT_H
				case sdl.K_8:
					selected.Type = SO_TELEPORT_V
				}
			}
		}

		//Blitting
		lvl.Blit(screen)
		But.Blit(screen)
		objects.Blit(screen)

		screen.Flip()
	}
}
Exemple #3
0
func MainLoop() {
	outcode := -1
	option := 0
	lvl := 0

	Refresh(screen)
	screen.Blit(&sdl.Rect{0, 0, 0, 0}, IM.BG[0], nil)
	screen.Flip()

	//MU.Music[0].PlayMusic(-1)

	for running := true; running; {
		for ev := sdl.WaitEvent(); ev != nil; ev = sdl.WaitEvent() {
			Refresh(screen)
			screen.Blit(&sdl.Rect{0, 0, 0, 0}, IM.BG[0], nil)
			if option == 0 {
				screen.FillRect(&sdl.Rect{320, 212, 10, 10}, 0xFF00FF)
			} else {
				screen.FillRect(&sdl.Rect{320, 233, 10, 10}, 0xFF00FF)
			}

			switch e := ev.(type) {
			case *sdl.QuitEvent:
				running = false
				return
			case *sdl.KeyboardEvent:
				if e.Type != sdl.KEYDOWN {
					break
				}
				switch e.Keysym.Sym {
				case sdl.K_SPACE:
					if option == 0 {
						goto game
					} else {
						screen.FillRect(&sdl.Rect{100, 200, 100, 10}, 0xFFFFFF)
						screen.FillRect(&sdl.Rect{100, 220, 100, 10}, 0xFFFFFF)
						screen.FillRect(&sdl.Rect{100, 200, 10, 20}, 0xFFFFFF)
						screen.FillRect(&sdl.Rect{200, 200, 10, 30}, 0xFFFFFF)
						screen.Flip()
						var ok bool
						lvl, ok = Password()
						if ok {
							goto game
						}
					}
				case sdl.K_UP:
					option = 0
				case sdl.K_DOWN:
					option = 1
				}
			}
			screen.Flip()
		}

		continue

		//Gameing
	game:
		outcode = MainGame(lvl)

		if outcode == G_EXITPROGRAM { //Exit Program
			running = false
			return
		}

		if outcode == G_NEXTLEVEL { //NextLevel
			screen.Blit(&sdl.Rect{350, 250, 0, 0}, IM.Misc[2], nil)
			screen.Flip()
			sdl.Delay(1000)
			for PressKey() != sdl.K_SPACE {
			}
			lvl++
			if lvl%4 == 0 {
				checkpoint = lvl
			}
			goto game

		}

		if outcode == G_GAMEOVER { //Gameover
			screen.Blit(&sdl.Rect{350, 250, 0, 0}, IM.Misc[1], nil)
			screen.Flip()
			for PressKey() != sdl.K_SPACE {
			}
			player = nil
			lvl = checkpoint
			goto game

		}

		if outcode == G_ENDGAME { //Game Completed
			screen.Blit(&sdl.Rect{350, 250, 0, 0}, IM.Misc[3], nil)
			screen.Flip()
			for PressKey() != sdl.K_SPACE {
			}
			lvl = 0
		}
	}
}