Guten Abend
Ich habe zu Lernzwecken ein Snake-Remake in bisher 8 unterschiedlichen Programmiersprachen (AutoIt, PureBasic, C#, VisualBasic .NET, JavaScript, Java, Python, Basic4Android) programmiert.
Das Spiel ist in den unterschiedlichen Programmiersprachen im Design und in der Steuerung (außer bei der Android Version) immer identisch.
Vielleicht kann ja der ein oder andere damit etwas Anfangen oder mir noch Tipps geben
[Blockierte Grafik: https://image.ibb.co/jYSvoR/Clipboard03.jpg]
AutoIt Quellcode:
AutoIt
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=icon.ico
#AutoIt3Wrapper_Outfile=Snake_AutoIt.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GUIConstants.au3>
#include <GDIPlus.au3>
#include <Misc.au3>
#include <Sound.au3>
Global Const $window_title = "Snake"
Global Const $window_size = 500
Global Const $block_size = 20
Global Const $game_size = $window_size / $block_size
Global Const $key_right = 27
Global Const $key_left = 25
Global Const $key_up = 26
Global Const $key_down = 28
Global Const $key_space = 20
Global Enum $snake_x, $snake_y, $snake_length
Global Enum $direction_right, $direction_left, $direction_up, $direction_down
Global Enum $game_start, $game_play, $game_pause, $game_restart
Global $window
Global $graphics, $bitmap, $buffer
Global $user_dll
Global $snake_direction
Global $snake_current_direction
Global $snake[1][$snake_length]
Global $gift_x, $gift_y, $gift_collision
Global $game_state = $game_start
Global $gift_sound[4]
Global $collision_sound
Global $collision
Global $effect
Global $effect_timer
Global $move_timer
Global $fps_timer = TimerInit()
Global $fps_counter = 0
Global $fps = 0
init()
Func init()
$user_dll = DllOpen("user32.dll")
$window = GUICreate($window_title, $window_size, $window_size)
_GDIPlus_Startup()
$graphics = _GDIPlus_GraphicsCreateFromHWND($window)
$bitmap = _GDIPlus_BitmapCreateFromGraphics($window_size, $window_size, $graphics)
$buffer = _GDIPlus_ImageGetGraphicsContext($bitmap)
For $i = 0 To UBound($gift_sound) - 1
$gift_sound[$i] = _SoundOpen("gift" & String($i + 1) & ".wav")
Next
$collision_sound = _SoundOpen("collision.wav")
GUISetState()
restart()
main()
EndFunc ;==>init
Func main()
While True
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
ExitLoop
EndSwitch
event()
draw()
WEnd
close()
EndFunc ;==>main
Func close()
DllClose($user_dll)
_SoundClose($collision_sound)
For $i = 0 To UBound($gift_sound) - 1
_SoundClose($gift_sound[$i])
Next
_GDIPlus_GraphicsDispose($buffer)
_GDIPlus_BitmapDispose($bitmap)
_GDIPlus_GraphicsDispose($graphics)
_GDIPlus_Shutdown()
GUIDelete($window)
EndFunc ;==>close
Func fps()
$fps_counter += 1
If TimerDiff($fps_timer) >= 1000 Then
$fps = $fps_counter
$fps_counter = 0
$fps_timer = TimerInit()
EndIf
EndFunc ;==>fps
Func restart()
ReDim $snake[1][$snake_length]
$snake[0][$snake_x] = Random(3, $game_size - 4, 1)
$snake[0][$snake_y] = Random(3, $game_size - 4, 1)
gift()
$move_timer = TimerInit()
EndFunc ;==>restart
Func gift()
Local $find
Do
$gift_x = Random(1, $game_size - 2, 1)
$gift_y = Random(1, $game_size - 2, 1)
$find = False
For $i = 0 To UBound($snake) - 1
If $gift_x = $snake[$i][$snake_x] And $gift_y = $snake[$i][$snake_y] Then
$find = True
ExitLoop
EndIf
Next
Until Not $find
EndFunc ;==>gift
Func event()
Local $restart
Switch $game_state
Case $game_start, $game_restart
;if start or restart game
$restart = False
If _IsPressed($key_left, $user_dll) Then
$snake_direction = $direction_left
$restart = True
ElseIf _IsPressed($key_right, $user_dll) Then
$snake_direction = $direction_right
$restart = True
ElseIf _IsPressed($key_up, $user_dll) Then
$snake_direction = $direction_up
$restart = True
ElseIf _IsPressed($key_down, $user_dll) Then
$snake_direction = $direction_down
$restart = True
EndIf
If $restart Then
If $game_state = $game_restart Then
restart()
EndIf
$game_state = $game_play
EndIf
Case $game_play
;if play and click space
If _IsPressed($key_space, $user_dll) Then
Do
Until Not _IsPressed($key_space, $user_dll)
$game_state = $game_pause
EndIf
;if key left
If _IsPressed($key_left, $user_dll) Then
If Not (UBound($snake) > 1 And $snake_current_direction = $direction_right) Then
$snake_direction = $direction_left
EndIf
;if key right
ElseIf _IsPressed($key_right, $user_dll) Then
If Not (UBound($snake) > 1 And $snake_current_direction = $direction_left) Then
$snake_direction = $direction_right
EndIf
EndIf
;if key up
If _IsPressed($key_up, $user_dll) Then
If Not (UBound($snake) > 1 And $snake_current_direction = $direction_down) Then
$snake_direction = $direction_up
EndIf
;if key down
ElseIf _IsPressed($key_down, $user_dll) Then
If Not (UBound($snake) > 1 And $snake_current_direction = $direction_up) Then
$snake_direction = $direction_down
EndIf
EndIf
move()
Case $game_pause
;if pause and click space
If _IsPressed($key_space, $user_dll) Then
Do
Until Not _IsPressed($key_space, $user_dll)
$game_state = $game_play
EndIf
EndSwitch
EndFunc ;==>event
Func move()
Local $next_x, $next_y, $x_next, $y_next
If TimerDiff($move_timer) >= 100 Then
$snake_current_direction = $snake_direction
$next_x = $snake[0][$snake_x]
$next_y = $snake[0][$snake_y]
Switch $snake_current_direction
Case $direction_right
$next_x += 1
Case $direction_left
$next_x -= 1
Case $direction_up
$next_y -= 1
Case $direction_down
$next_y += 1
EndSwitch
$collision = False
For $i = 0 To UBound($snake) - 2
If $next_x = $snake[$i][$snake_x] And $next_y = $snake[$i][$snake_y] Then
$collision = True
ExitLoop
EndIf
Next
If $collision Or $next_x < 1 Or $next_x > $game_size - 2 Or $next_y < 1 Or $next_y > $game_size - 2 Then
$collision = True
_SoundPlay($collision_sound)
$game_state = $game_restart
Return
EndIf
If $next_x = $gift_x And $next_y = $gift_y Then
_SoundPlay($gift_sound[Random(0, UBound($gift_sound) - 1, 1)])
$gift_collision = True
$i = UBound($snake)
ReDim $snake[$i + 1][$snake_length]
gift()
EndIf
For $i = 0 To UBound($snake) - 1
$x_next = $next_x
$y_next = $next_y
$next_x = $snake[$i][$snake_x]
$next_y = $snake[$i][$snake_y]
$snake[$i][$snake_x] = $x_next
$snake[$i][$snake_y] = $y_next
Next
$move_timer = TimerInit()
EndIf
EndFunc ;==>move
Func draw()
Local $text = ""
Local $color = 0xff99cb99
If $gift_collision Then
$gift_collision = False
$effect = 1
$effect_timer = TimerInit()
ElseIf $collision Then
$collision = False
$effect = 2
$effect_timer = TimerInit()
EndIf
If $effect = 1 Then
$color = 0xffffffff
ElseIf $effect = 2 Then
$color = 0xffFEC6BF
EndIf
If $effect > 0 And TimerDiff($effect_timer) >= 30 Then
$effect = 0
EndIf
_GDIPlus_GraphicsClear($buffer, $color)
;draw bound
_GDIPlus_GraphicsDrawRect($buffer, $block_size, $block_size, $window_size - $block_size * 2, $window_size - $block_size * 2)
;draw text, score and fps
_GDIPlus_GraphicsDrawString($buffer, "SCORE: " & String((UBound($snake) - 1) * 10), $block_size, $window_size - $block_size + 3, "Lucida Console", 10)
_GDIPlus_GraphicsDrawString($buffer, "FPS: " & String($fps), $window_size - $block_size * 5, $window_size - $block_size + 3, "Lucida Console", 10)
Switch $game_state
Case $game_start
$text = "CLICK ARROW KEYS TO START"
Case $game_pause
$text = "PAUSE, CLICK SPACE TO PLAY"
Case $game_restart
$text = "LOST, CLICK ARROW KEYS TO RESTART"
Case $game_play
$text = "HAVE FUN"
EndSwitch
_GDIPlus_GraphicsDrawString($buffer, $text, $block_size, 3, "Lucida Console", 10)
;draw snake
For $i = 0 To UBound($snake) - 1
_GDIPlus_GraphicsFillRect($buffer, $snake[$i][$snake_x] * $block_size + 1, $snake[$i][$snake_y] * $block_size + 1, $block_size - 2, $block_size - 2)
Next
;draw gift
_GDIPlus_GraphicsFillRect($buffer, $gift_x * $block_size, $gift_y * $block_size + $block_size / 3, $block_size / 3, $block_size / 3)
_GDIPlus_GraphicsFillRect($buffer, $gift_x * $block_size + $block_size / 3 * 2, $gift_y * $block_size + $block_size / 3, $block_size / 3, $block_size / 3)
_GDIPlus_GraphicsFillRect($buffer, $gift_x * $block_size + $block_size / 3, $gift_y * $block_size, $block_size / 3, $block_size / 3)
_GDIPlus_GraphicsFillRect($buffer, $gift_x * $block_size + $block_size / 3, $gift_y * $block_size + $block_size / 3 * 2, $block_size / 3, $block_size / 3)
_GDIPlus_GraphicsDrawImage($graphics, $bitmap, 0, 0)
fps()
EndFunc ;==>draw
Alles anzeigen
In der ZIP Datei sind alle Quellcodes, ausführbare Dateien, Sounds und Icons enthalten
DOWNLOAD: https://drive.google.com/uc?id=1FDYzRgJ…export=download
DOWNLOAD mit Android Version: https://drive.google.com/open?id=1Rcoj3…ONG53TPVYzVc-bV