当前位置: 首页>>代码示例>>Golang>>正文


Golang Renderer.DrawRect方法代码示例

本文整理汇总了Golang中github.com/veandco/go-sdl2/sdl.Renderer.DrawRect方法的典型用法代码示例。如果您正苦于以下问题:Golang Renderer.DrawRect方法的具体用法?Golang Renderer.DrawRect怎么用?Golang Renderer.DrawRect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/veandco/go-sdl2/sdl.Renderer的用法示例。


在下文中一共展示了Renderer.DrawRect方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: run

func run() int {
	var window *sdl.Window
	var renderer *sdl.Renderer
	var points []sdl.Point
	var rect sdl.Rect
	var rects []sdl.Rect

	window, err := sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
		winWidth, winHeight, sdl.WINDOW_SHOWN)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", err)
		return 1
	}
	defer window.Destroy()

	renderer, err = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", err)
		return 2
	}
	defer renderer.Destroy()

	renderer.Clear()

	renderer.SetDrawColor(255, 255, 255, 255)
	renderer.DrawPoint(150, 300)

	renderer.SetDrawColor(0, 0, 255, 255)
	renderer.DrawLine(0, 0, 200, 200)

	points = []sdl.Point{{0, 0}, {100, 300}, {100, 300}, {200, 0}}
	renderer.SetDrawColor(255, 255, 0, 255)
	renderer.DrawLines(points)

	rect = sdl.Rect{300, 0, 200, 200}
	renderer.SetDrawColor(255, 0, 0, 255)
	renderer.DrawRect(&rect)

	rects = []sdl.Rect{{400, 400, 100, 100}, {550, 350, 200, 200}}
	renderer.SetDrawColor(0, 255, 255, 255)
	renderer.DrawRects(rects)

	rect = sdl.Rect{250, 250, 200, 200}
	renderer.SetDrawColor(0, 255, 0, 255)
	renderer.FillRect(&rect)

	rects = []sdl.Rect{{500, 300, 100, 100}, {200, 300, 200, 200}}
	renderer.SetDrawColor(255, 0, 255, 255)
	renderer.FillRects(rects)

	renderer.Present()

	sdl.Delay(2000)

	return 0
}
开发者ID:emlai,项目名称:go-sdl2,代码行数:56,代码来源:render.go

示例2: Draw

func (d *drawable) Draw(r *sdl.Renderer) {
	// Fill
	if d.fill {
		SetDrawColor(r, d.fill_color)
		r.FillRect(&sdl.Rect{d.X, d.Y, d.W, d.H})
	}

	// Draw bounds
	SetDrawColor(r, d.border_color)
	r.DrawRect(&sdl.Rect{d.X, d.Y, d.W, d.H})
}
开发者ID:jbott,项目名称:go-mission-control,代码行数:11,代码来源:drawable.go

示例3: main

func main() {
	var window *sdl.Window
	var renderer *sdl.Renderer
	var points []sdl.Point
	var rect sdl.Rect
	var rects []sdl.Rect

	window = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
		winWidth, winHeight, sdl.WINDOW_SHOWN)
	if window == nil {
		fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", sdl.GetError())
		os.Exit(1)
	}

	renderer = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
	if renderer == nil {
		fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", sdl.GetError())
		os.Exit(2)
	}

	renderer.SetDrawColor(255, 255, 255, 255)
	renderer.DrawPoint(150, 300)

	renderer.SetDrawColor(0, 0, 255, 255)
	renderer.DrawLine(0, 0, 200, 200)

	points = []sdl.Point{{0, 0}, {100, 300}, {100, 300}, {200, 0}}
	renderer.SetDrawColor(255, 255, 0, 255)
	renderer.DrawLines(points)

	rect = sdl.Rect{300, 0, 200, 200}
	renderer.SetDrawColor(255, 0, 0, 255)
	renderer.DrawRect(&rect)

	rects = []sdl.Rect{{400, 400, 100, 100}, {550, 350, 200, 200}}
	renderer.SetDrawColor(0, 255, 255, 255)
	renderer.DrawRects(rects)

	rect = sdl.Rect{250, 250, 200, 200}
	renderer.SetDrawColor(0, 255, 0, 255)
	renderer.FillRect(&rect)

	rects = []sdl.Rect{{500, 300, 100, 100}, {200, 300, 200, 200}}
	renderer.SetDrawColor(255, 0, 255, 255)
	renderer.FillRects(rects)

	renderer.Present()

	sdl.Delay(2000)

	renderer.Destroy()
	window.Destroy()
}
开发者ID:JalfResi,项目名称:go-sdl2,代码行数:53,代码来源:render.go

示例4: Draw

func Draw(r *sdl.Renderer, g *grid.Grid) {
	g.EachRoom(func(room *grid.Room) {
		x := int32(room.X) * pixelSize * roomSize
		y := int32(room.Y) * pixelSize * roomSize
		width := roomSize * pixelSize
		height := roomSize * pixelSize
		rect := &sdl.Rect{x, y, width, height}

		if room.Blocked {
			r.FillRect(rect)
		} else {
			r.DrawRect(rect)
		}
	})
}
开发者ID:paytonrules,项目名称:pathfinding,代码行数:15,代码来源:gameloop.go

示例5: Render

func (view *gameView) Render(renderer *sdl.Renderer, events *Events) {
	view.player.calculateMovement(events)

	// start each new frame by filling the frame black
	renderer.SetDrawColor(0, 0, 0, 0)
	renderer.Clear()

	// draw the player
	renderer.SetDrawColor(255, 0, 0, 255)
	playerSDLRect := view.player.rect.GetSDLRect()
	renderer.DrawRect(playerSDLRect)
	renderer.FillRect(playerSDLRect)

	// draw the players spinning weapon
	playerWeaponSDLRect := view.player.weapon.rect.GetSDLRect()
	renderer.DrawRect(playerWeaponSDLRect)
	renderer.FillRect(playerWeaponSDLRect)

	// destroyedMeteors is used to remove destroyed meteors outside
	// of looping through view.meteors to avoid out of range errors
	var destroyedMeteors []int

	// loop through the frames active meteors
	for i, meteor := range view.meteors {
		// move and draw and then reinsert the moved meteor
		meteor.move()
		enemySDLRect := meteor.rect.GetSDLRect()
		renderer.SetDrawColor(0, 255, 0, 255)
		renderer.DrawRect(enemySDLRect)
		renderer.FillRect(enemySDLRect)
		view.meteors[i] = meteor

		// if the player and meteor collide, end the game
		if view.player.rect.Colision(meteor.rect) {
			renderer.Present()
			log.Println("You died!")
			log.Println("Score:", view.score)
			sdl.Delay(1000)
			os.Exit(0)
		}

		// if the weapon and meteor collide, destroy it and update score
		if meteor.rect.Colision(view.player.weapon.rect) {
			destroyedMeteors = append(destroyedMeteors, i)
			view.score += 10
		}

		// if the meteor is off screen, destroy it and update score
		if meteor.isOffScreen() {
			destroyedMeteors = append(destroyedMeteors, i)
			view.score++
		}
	}

	// for each destroyed meteor, remove it from the meteors slice
	for _, i := range destroyedMeteors {
		view.meteors = append(view.meteors[:i], view.meteors[i+1:]...)
	}

	// present the updated frame to the player
	renderer.Present()

	// add new meteors depending on the difficulty and ticks since
	// last meteor was added
	if view.meteorTimer > 100 {
		newMeteor := spawnMeteor()
		view.meteors = append(view.meteors, newMeteor)
		view.meteorTimer = rand.Intn(30) + view.difficulty
		if view.difficulty < 60 {
			view.difficulty += 3
		}
	}
	view.meteorTimer++

	return
}
开发者ID:Charlesworth,项目名称:goGame,代码行数:76,代码来源:gameView.go

示例6: run

func run() int {
	var window *sdl.Window
	var renderer *sdl.Renderer
	var points []sdl.Point
	var rect sdl.Rect
	var rects []sdl.Rect
	var err error

	sdl.Do(func() {
		window, err = sdl.CreateWindow(WindowTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, WindowWidth, WindowHeight, sdl.WINDOW_SHOWN)
	})
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", err)
		return 1
	}
	defer func() {
		sdl.Do(func() {
			window.Destroy()
		})
	}()

	sdl.Do(func() {
		renderer, err = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
	})
	if err != nil {
		fmt.Fprint(os.Stderr, "Failed to create renderer: %s\n", err)
		return 2
	}

	sdl.Do(func() {
		renderer.Clear()
	})
	defer func() {
		sdl.Do(func() {
			renderer.Destroy()
		})
	}()

	go func() {
		println("goroutine: A")
	}()

	sdl.Do(func() {
		renderer.SetDrawColor(255, 255, 255, 255)
		renderer.DrawPoint(150, 300)
		println("queue: A")
	})

	go func() {
		println("goroutine: B")
	}()

	sdl.Do(func() {
		renderer.SetDrawColor(0, 0, 255, 255)
		renderer.DrawLine(0, 0, 200, 200)
		println("queue: B")
	})

	go func() {
		println("goroutine: C")
	}()

	sdl.Do(func() {
		points = []sdl.Point{{0, 0}, {100, 300}, {100, 300}, {200, 0}}
		renderer.SetDrawColor(255, 255, 0, 255)
		renderer.DrawLines(points)
		println("queue: C")
	})

	go func() {
		println("goroutine: D")
	}()

	sdl.Do(func() {
		rect = sdl.Rect{300, 0, 200, 200}
		renderer.SetDrawColor(255, 0, 0, 255)
		renderer.DrawRect(&rect)
		println("queue: D")
	})

	go func() {
		println("goroutine: E")
	}()

	sdl.Do(func() {
		rects = []sdl.Rect{{400, 400, 100, 100}, {550, 350, 200, 200}}
		renderer.SetDrawColor(0, 255, 255, 255)
		renderer.DrawRects(rects)
		println("queue: E")
	})

	go func() {
		println("goroutine: F")
	}()

	sdl.Do(func() {
		rect = sdl.Rect{250, 250, 200, 200}
		renderer.SetDrawColor(0, 255, 0, 255)
		renderer.FillRect(&rect)
		println("queue: F")
//.........这里部分代码省略.........
开发者ID:veandco,项目名称:go-sdl2,代码行数:101,代码来源:render_queue.go

示例7: run

func run() int {
	var window *sdl.Window
	var renderer *sdl.Renderer
	var err error

	sdl.Do(func() {
		window, err = sdl.CreateWindow(WindowTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, WindowWidth, WindowHeight, sdl.WINDOW_OPENGL)
	})
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", err)
		return 1
	}
	defer func() {
		sdl.Do(func() {
			window.Destroy()
		})
	}()

	sdl.Do(func() {
		renderer, err = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
	})
	if err != nil {
		fmt.Fprint(os.Stderr, "Failed to create renderer: %s\n", err)
		return 2
	}
	defer func() {
		sdl.Do(func() {
			renderer.Destroy()
		})
	}()

	sdl.Do(func() {
		renderer.Clear()
	})

	for i := range rects {
		rects[i] = sdl.Rect{
			X: int32(rand.Int() % WindowWidth),
			Y: int32(i * WindowHeight / len(rects)),
			W: RectWidth,
			H: RectHeight,
		}
	}

	running := true
	for running {
		sdl.Do(func() {
			for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
				switch event.(type) {
				case *sdl.QuitEvent:
					runningMutex.Lock()
					running = false
					runningMutex.Unlock()
				}
			}

			renderer.Clear()
			renderer.SetDrawColor(0, 0, 0, 0x20)
			renderer.FillRect(&sdl.Rect{0, 0, WindowWidth, WindowHeight})
		})

		// Do expensive stuff using goroutines
		wg := sync.WaitGroup{}
		for i := range rects {
			wg.Add(1)
			go func(i int) {
				rects[i].X = (rects[i].X + 10) % WindowWidth
				sdl.Do(func() {
					renderer.SetDrawColor(0xff, 0xff, 0xff, 0xff)
					renderer.DrawRect(&rects[i])
				})
				wg.Done()
			}(i)
		}
		wg.Wait()

		sdl.Do(func() {
			renderer.Present()
			sdl.Delay(1000 / FrameRate)
		})
	}

	return 0
}
开发者ID:veandco,项目名称:go-sdl2,代码行数:84,代码来源:render_goroutines.go


注:本文中的github.com/veandco/go-sdl2/sdl.Renderer.DrawRect方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。