Dobrý den. Programuji hru a potřebuji, aby se mi hýbaly nové články hada, ale nehýbou. Hra je v XNA 4.
Public Class Game1
Inherits Microsoft.Xna.Framework.Game
Private WithEvents graphics As GraphicsDeviceManager
Private WithEvents spriteBatch As SpriteBatch
Dim snake As New List(Of Sprite)
Dim sTexture, jTexture As Texture2D
Dim jidlo As Sprite
'1: nahoru, 2: dolu, 3: doleva, 4:doprava
Dim smer As Integer
Dim stsmer As Integer
Public Sub New()
graphics = New GraphicsDeviceManager(Me)
graphics.PreferredBackBufferWidth = 150
graphics.PreferredBackBufferHeight = 150
Content.RootDirectory = "Content"
End Sub
''' <summary>
''' Allows the game to perform any initialization it needs to before starting to run.
''' This is where it can query for any required services and load any non-graphic
''' related content. Calling MyBase.Initialize will enumerate through any components
''' and initialize them as well.
''' </summary>
Protected Overrides Sub Initialize()
' TODO: Add your initialization logic here
MyBase.Initialize()
End Sub
''' <summary>
''' LoadContent will be called once per game and is the place to load
''' all of your content.
''' </summary>
Protected Overrides Sub LoadContent()
' Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = New SpriteBatch(GraphicsDevice)
sTexture = Content.Load(Of Texture2D)("telo")
jTexture = Content.Load(Of Texture2D)("jidlo")
' TODO: use Me.Content to load your game content here
snake.Add(New Sprite(sTexture, New Rectangle(1, 1, sTexture.Width, sTexture.Height)))
jidlo = New Sprite(jTexture, New Rectangle(rnd.Next(1, graphics.PreferredBackBufferWidth), rnd.Next(1, graphics.PreferredBackBufferHeight),
jTexture.Width, jTexture.Height))
End Sub
''' <summary>
''' UnloadContent will be called once per game and is the place to unload
''' all content.
''' </summary>
Protected Overrides Sub UnloadContent()
' TODO: Unload any non ContentManager content here
End Sub
Dim jinysmer As Integer = 0
''' <summary>
''' Allows the game to run logic such as updating the world,
''' checking for collisions, gathering input, and playing audio.
''' </summary>
''' <param name="gameTime">Provides a snapshot of timing values.</param>
Protected Overrides Sub Update(ByVal gameTime As GameTime)
' Allows the game to exit
If GamePad.GetState(PlayerIndex.One).Buttons.Back = ButtonState.Pressed Then
Me.Exit()
End If
stsmer = smer
Dim kState As KeyboardState = Keyboard.GetState
If kState.GetPressedKeys.Count > 0 Then
Select Case kState.GetPressedKeys(0)
Case Keys.Up
smer = 1
Case Keys.Down
smer = 2
Case Keys.Left
smer = 3
Case Keys.Right
smer = 4
End Select
End If
If Not stsmer = smer Then
Select Case smer
Case 1
snake(jinysmer).Position.Y -= CSng(0.005 * gameTime.ElapsedGameTime.TotalMilliseconds * sTexture.Width)
For i As Integer = jinysmer + 1 To snake.Count - 1
Select Case stsmer
Case 2
snake(i).Position.Y += CSng(0.005 * gameTime.ElapsedGameTime.TotalMilliseconds * sTexture.Width)
Case 3
snake(i).Position.X -= CSng(0.005 * gameTime.ElapsedGameTime.TotalMilliseconds * sTexture.Width)
Case 4
snake(i).Position.Y += CSng(0.005 * gameTime.ElapsedGameTime.TotalMilliseconds * sTexture.Width)
End Select
Next
Case 2
snake(jinysmer).Position.Y += CSng(0.005 * gameTime.ElapsedGameTime.TotalMilliseconds * sTexture.Width)
For i As Integer = jinysmer + 1 To snake.Count - 1
Select Case stsmer
Case 1
snake(i).Position.Y -= CSng(0.005 * gameTime.ElapsedGameTime.TotalMilliseconds * sTexture.Width)
Case 3
snake(i).Position.X -= CSng(0.005 * gameTime.ElapsedGameTime.TotalMilliseconds * sTexture.Width)
Case 4
snake(i).Position.Y += CSng(0.005 * gameTime.ElapsedGameTime.TotalMilliseconds * sTexture.Width)
End Select
Next
Case 3
snake(jinysmer).Position.X -= CSng(0.005 * gameTime.ElapsedGameTime.TotalMilliseconds * sTexture.Width)
For i As Integer = jinysmer + 1 To snake.Count - 1
Select Case stsmer
Case 1
snake(i).Position.Y -= CSng(0.005 * gameTime.ElapsedGameTime.TotalMilliseconds * sTexture.Width)
Case 2
snake(i).Position.X += CSng(0.005 * gameTime.ElapsedGameTime.TotalMilliseconds * sTexture.Width)
Case 4
snake(i).Position.Y += CSng(0.005 * gameTime.ElapsedGameTime.TotalMilliseconds * sTexture.Width)
End Select
Next
Case 4
snake(jinysmer).Position.X += CSng(0.005 * gameTime.ElapsedGameTime.TotalMilliseconds * sTexture.Width)
For i As Integer = jinysmer + 1 To snake.Count - 1
Select Case stsmer
Case 2
snake(i).Position.Y += CSng(0.005 * gameTime.ElapsedGameTime.TotalMilliseconds * sTexture.Width)
Case 3
snake(i).Position.X -= CSng(0.005 * gameTime.ElapsedGameTime.TotalMilliseconds * sTexture.Width)
Case 1
snake(i).Position.Y -= CSng(0.005 * gameTime.ElapsedGameTime.TotalMilliseconds * sTexture.Width)
End Select
Next
End Select
Else
For i As Integer = 0 To snake.Count - 1
Select Case smer
Case 1
snake(i).Position.Y -= CSng(0.005 * gameTime.ElapsedGameTime.TotalMilliseconds * sTexture.Width)
Case 2
snake(i).Position.Y += CSng(0.005 * gameTime.ElapsedGameTime.TotalMilliseconds * sTexture.Width)
Case 3
snake(i).Position.X -= CSng(0.005 * gameTime.ElapsedGameTime.TotalMilliseconds * sTexture.Width)
Case 4
snake(i).Position.X += CSng(0.005 * gameTime.ElapsedGameTime.TotalMilliseconds * sTexture.Width)
End Select
Next
End If
If snake(0).Rectangle.Intersects(jidlo.Rectangle) Then
snake.Add(New Sprite(sTexture, New Rectangle(snake(snake.Count - 1).Rectangle.X + sTexture.Width,
snake(snake.Count - 1).Rectangle.Y + sTexture.Height,
jTexture.Width, jTexture.Height)))
jidlo = New Sprite(jTexture, New Rectangle(rnd.Next(1, graphics.PreferredBackBufferWidth), rnd.Next(1, graphics.PreferredBackBufferHeight),
jTexture.Width, jTexture.Height))
End If
snake(0).Rectangle.X = CInt(snake(0).Position.X)
snake(0).Rectangle.Y = CInt(snake(0).Position.Y)
' TODO: Add your update logic here
MyBase.Update(gameTime)
End Sub
Dim rnd As New Random
''' <summary>
''' This is called when the game should draw itself.
''' </summary>
''' <param name="gameTime">Provides a snapshot of timing values.</param>
Protected Overrides Sub Draw(ByVal gameTime As GameTime)
GraphicsDevice.Clear(New Color With {.R = 47, .G = 86, .B = 35})
spriteBatch.Begin()
For Each fe As Sprite In snake
fe.Draw(spriteBatch)
Next
jidlo.Draw(spriteBatch)
spriteBatch.End()
' TODO: Add your drawing code here
MyBase.Draw(gameTime)
End Sub
End Class
Mohl by se na to někdo prosím kouknout a navést mě? Děkuji.
|