Tipp 0233 Würfel
Autor/Einsender:
Datum:
  Detlev Schubert
10.05.2002
Entwicklungsumgebung:   VB 5
Dieser Tipp zeigt, wie man für ein Würfelspiel wie z.B. Kniffel optisch ansprechende Würfel realisieren kann, die unabhängig voneinander zufällige Zahlen würfeln. Als Grundlage dient hier eine Grafik, die die Würfeloberflächen enthält, und mit der API-Funktion BitBlt entsprechend dargestellt werden.
Code im Codebereich des Moduls
 
Option Explicit

Global Const SRCCOPY = &HCC0020

Public Declare Function BitBlt Lib "gdi32" (ByVal _
      hDestDC As Long, ByVal x As Long, ByVal y As Long, _
      ByVal nWidth As Long, ByVal nHeight As Long, _
      ByVal hSrcDC As Long, ByVal xSrc As Long, _
      ByVal ySrc As Long, ByVal dwRop As Long) As Long

Public Sub Wuerfeln(WPic As PictureBox, Color As Integer, _
      W1 As PictureBox, W2 As PictureBox, W3 As PictureBox, _
      W4 As PictureBox, W5 As PictureBox)

  Dim A As Long
  Dim B As Long
  Dim C As Long
  Dim D As Long
  Dim E As Long

  Randomize
  A = Int(6 * Rnd + 1)

  Randomize
  B = Int(6 * Rnd + 1)

  Randomize
  C = Int(6 * Rnd + 1)

  Randomize
  D = Int(6 * Rnd + 1)

  Randomize
  E = Int(6 * Rnd + 1)

  ZeigeWuerfel WPic, W1, A, Color
  ZeigeWuerfel WPic, W2, B, Color
  ZeigeWuerfel WPic, W3, C, Color
  ZeigeWuerfel WPic, W4, D, Color
  ZeigeWuerfel WPic, W5, E, Color
End Sub

Public Sub ZeigeWuerfel(Source As PictureBox, _
      Ziel As PictureBox, Digit As Long, Color As Integer)

  BitBlt Ziel.hDC, 0, 0, 32, 32, Source.hDC, 32 * (Color - 1), _
        32 * (Digit - 1), SRCCOPY
  Ziel.Refresh
End Sub
 
Code im Codebereich der Form
 
Option Explicit

Dim WColor As Integer

Private Sub Form_Load()
  Wuerfeln WPic, 1, W1, W2, W3, W4, W5
End Sub

Private Sub Command1_Click()
  Dim I As Integer

  If optWR.Value = True Then
    WColor = 3
  ElseIf optWG.Value = True Then
    WColor = 2
  Else
    WColor = 1
  End If

  For I = 1 To 6
    Wuerfeln WPic, WColor, W1, W2, W3, W4, W5
  Next I
End Sub
 

Windows-Version
95
98/SE
ME
NT
2000
XP
Vista
Win 7
VB-Version
VBA 5
VBA 6
VB 4/16
VB 4/32
VB 5
VB 6


Download  (4,9 kB) Downloads bisher: [ 3773 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Projekte | Tutorials | API-Referenz | VB-/VBA-Tipps | Komponenten | Bücherecke | VB/VBA-Forum | VB.Net-Forum | DirectX-Forum | Foren-Archiv | DirectX | VB.Net-Tipps | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Samstag, 3. September 2011