I know this topic was opened so many times before.
I looked at all the examples, but I just cant make it work!
I have 3 dimensions, date is in the first dimension.
All works fine expect one array, and I just dont understand why..
Here is the test link: click here
Sub DataSorter(arrArray)
Dim row, j, StartingKeyValue, StartingOtherValue, _
NewStartingKey, NewStartingOther, _
swap_pos
For row = 0 To UBound(arrArray)-1
StartingKeyValue = arrArray(row, 0)
StartingOtherValue = arrArray(row, 0)
StartingVenue = arrArray(row, 1)
StartingVenueOther = arrArray(row, 1)
swap_pos = row
For j = row + 1 to UBound(arrArray)
If DateDiff("s", arrArray(j, 0), NewStartingKey) < 0 Then
swap_pos = j
NewStartingKey = arrArray(j, 0)
NewStartingOther = arrArray(j, 0)
NewStartingVenue = arrArray(j, 1)
NewStartingVenueOther = arrArray(j, 1)
NewStartingCountry = arrArray(j, 2)
NewStartingCountryOther = arrArray(j, 2)
End If
Next
If swap_pos <> row Then
arrArray (swap_pos, 0) = StartingKeyValue
arrArray (swap_pos, 0) = StartingOtherValue
arrArray (row, 0) = NewStartingKey
arrArray (row, 0) = NewStartingOther
arrArray (swap_pos, 1) = StartingVenue
arrArray (swap_pos, 1) = StartingVenueOther
arrArray (row, 1) = NewStartingVenue
arrArray (row, 1) = NewStartingVenueOther
arrArray (swap_pos, 2) = StartingCountry
arrArray (swap_pos, 2) = StartingCountryOther
arrArray (row, 2) = NewStartingCountry
arrArray (row, 2) = NewStartingCountryOther
End If
Next
End Sub
Very strange code. Try this:
Sub DataSorter(arrArray)
Dim row, i, tmp
For row = 0 To UBound(arrArray) - 1
For j = row + 1 to UBound(arrArray)
If DateDiff("s", arrArray(j, 0), arrArray(row, 0)) < 0 Then
For i = 0 To 2
tmp = arrArray(j, i)
arrArray(j, i) = arrArray(row, i)
arrArray(row, i) = tmp
Next
End If
Next
Next
End Sub
Related
idk what is wrong but it writes this into the file when it saves the value. If u need more information or have a question please ask me! Thanks for your time and hopefully u your solution!
SebyGHG
My discord: S̸̽̚e̵̓̓b̸̿̕y̴͆͐#4638
Here is a screenshot of the time and the value in the file:
Value in file
Time in game
Code:
name = "Timer"
description = "Just a normal Timer."
positionX = 0
positionY = 0
sizeX = 24
sizeY = 10
scale = 1
START_STOP_KEY = 0x55 --or 'U'
RESET_KEY = 0x4A --or 'J'
--
--[[
Timer Module Script by SebyGHG original script by Onix64(Stopwatch)
if you wish to change the key you can take the key code from here
https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
]] -------------script-code-------------
state = 1
stopTime = 0
startTime = 0
f = io.input("timesave.txt")
result = f :read()
f :close()
stopTime = result
state = 2
function keyboard(key, isDown)
if (isDown == true) then
if (key == RESET_KEY) then
state = 0
elseif (key == START_STOP_KEY) then
if (state == 0) then
state = 1
startTime = os.time()
elseif (state == 1) then
state = 2
io.output("timesave.txt")
timesave= (io.open("timesave.txt","w"))
io.write(stopTime)
io.close(timesave)
stopTime = os.time()
elseif (state == 2) then
state = 1
startTime =startTime + os.time() - stopTime
end
end
end
end
TimerText = "00:00"
TextColor = {r = 30, g = 255, b = 30, a = 255}
function doubleDigit(number)
if (number < 10) then
return "0" .. math.floor(number)
else
return math.floor(number)
end
end
function timeText(time)
local result = ""
local days = 0
while (time > 86399) do
days = days + 1
time = time - 86400
end
local hours = 0
while (time > 3599) do
hours = hours + 1
time = time - 86400
end
local minutes = 0
while (time > 59) do
minutes = minutes + 1
time = time - 60
end
if (days == 0) then
if (hours == 0) then
return doubleDigit(minutes) .. ":" .. doubleDigit(time)
else
return math.floor(hours) .. " : " .. doubleDigit(minutes) .. ":" .. doubleDigit(time)
end
else
return math.floor(days) ..
" : " .. doubleDigit(hours) .. " : " .. doubleDigit(minutes) .. ":" .. doubleDigit(time)
end
end
function update()
if (state == 0) then
TextColor = {r = 255, g = 0, b = 0, a = 255}
TimerText = "00:00"
elseif (state == 1) then
TimerText = timeText(os.time() - startTime)
TextColor = {r = 0, g = 255, b = 255, a = 255}
elseif (state == 2) then
TimerText = timeText(stopTime - startTime)
TextColor = {r = 255, g = 255, b = 0, a = 255}
end
end
function render()
local font = gui.font()
local tw = font.width(TimerText)
gfx.color(0, 0, 0, 0)
gfx.rect(0, 0, tw + 4, 10)
gfx.color(TextColor.r, TextColor.g, TextColor.b, TextColor.a)
gfx.text(2, 1, TimerText)
end
One error I spot is this part
while (time > 3599) do
hours = hours + 1
time = time - 86400
end
this is most likely supposed to be
while (time > 3599) do
hours = hours + 1
time = time - 3600
end
Private Sub Arr(ByVal arrRad() As RadioButton, ByVal arrLbl() As Label)
Dim result As Integer = 0
Dim temp As Integer = 0
'arrRad is my control array Radiobutton, arrLbl is my control array Label
'I have 15 Radiobutton and 3 Label
For i As Integer = 0 To arrRad.Length - 1 Step 5
If arrRad(i).Checked = True Then
temp = arrRad(i).Text.Substring(0, 1)
ElseIf arrRad(i + 1).Checked = True Then
temp = arrRad(i + 1).Text.Substring(0, 1)
ElseIf arrRad(i + 2).Checked = True Then
temp = arrRad(i + 2).Text.Substring(0, 1)
ElseIf arrRad(i + 3).Checked = True Then
temp = arrRad(i + 3).Text.Substring(0, 1)
ElseIf arrRad(i + 4).Checked = True Then
temp = arrRad(i + 4).Text.Substring(0, 1)
Else
temp = 0
End If
result += temp
Next
txtKetQua.Text = result.ToString()
End Sub
I want to show lblError if that row radiobutton is unchecked, but I don't know how to show it
P/s: Sorry for my bad English
You can do it in a very shorter way using LInQ Extension .Any():
lblError.Visible = Not arrRad.Any(Function(rb) rb.Ckecked)
Yesterday, I ran the following script in excel and it worked, but now it throws an error:
(run-time error '5' )
in the line:
Distance = Mid(Distance, InStr(1, Distance, ">") + 1, InStr(1, Distance, " ") - InStr(1, Distance, ">") - 1)
Sub Поиск_ближайшего_СБС()
Set Points = Worksheets("Точки")
Set SBS = Worksheets("СБС")
Dim Cordinate11, Cordinate12, Cordinate21, Cordinate22 As String
i = 2
Do While SBS.Cells(i, 1) <> Empty
Rows_of_SBS = i
i = i + 1
Loop
i = 2
Do While Cells(i, 1) <> Empty
Dist = 1000
Cordinate21 = Points.Cells(i + 1, 3)
Cordinate22 = Points.Cells(i + 1, 4)
For j = 2 To Rows_of_SBS
Cordinate11 = SBS.Cells(j, 2)
Cordinate12 = SBS.Cells(j, 3)
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False
With IE
.Navigate "http://maps.googleapis.com/maps/api/distancematrix/xml?origins=" _
& Cordinate11 & ",+" & Cordinate12 _
& "&destinations=" _
& Cordinate21 & ",+" & Cordinate22 _
& "&mode=driving&language=ru&sensor=false"
Do While .Busy
DoEvents
Loop
For Each MyTable In .Document.getElementsByTagName("text")
Distance = MyTable.innertext
Next
Distance = Mid(Distance, InStr(1, Distance, ">") + 1, InStr(1, Distance, " ") - InStr(1, Distance, ">") - 1)
If CDbl(Distance) < Dist Then
Dist = CDbl(Distance) s = j
End If
End With
IE.Quit
Set EI = Nothing
Next j
Points.Cells(i + 1, 5) = Dist
Points.Cells(i + 1, 6) = SBS.Cells(s, 1)
i = i + 1
Loop
End Sub
'
So I have a 2d array that I want to Sort. I can sort it easily when one dimensional.
I Hope you can help me guys.
This is my Data.
top5(0,0) = Greeting
top5(0,1) = 2
top5(1,0) = VerifyingInformation
top5(1,1) = 5
top5(2,0) = Calibration
top5(2,1) = 4
I can sort It no problem when one dimensional.
I'm using this code for one dimensional.
For i = LBound(top5) to UBound(top5)
For j = LBound(top5) to UBound(top5) - 1
If top5(j,1) < top5(j + 1,1) Then
TempValue = top5(j + 1,1)
top5(j + 1,1) = top5(j,1)
top5(j,1) = TempValue
End If
next
Next
The result I want to have is this.
VerifyingInformation 5
Calibration 4
Greeting 2
THIS WORKS FOR ME
function sort_arr_mult(byref ArrTmp, ordPlace)
' ordPlace - the place of the order value in the array
' create the new array
Redim arrRet (Ubound(ArrTmp, 1), Ubound(ArrTmp, 2))
for j = 0 to Ubound(ArrTmp, 2)
orderVal = ArrTmp(ordPlace, j)
if j = 0 then ' first enter insert to first column
for i = 0 to Ubound(ArrTmp, 1)
arrRet(i, j) = ArrTmp(i, j)
next
else
' check the first value if smaller or equal
' move the columnbs one field up
' at the end insert to currenct column
for k = 0 to Ubound(arrRet, 2)
if isEmp(arrRet(0, k)) then ' if empty fied the column
for i = 0 to Ubound(arrRet, 1)
arrRet(i, k) = ArrTmp(i, j)
next
exit for
else
if orderVal<=arrRet(ordPlace, k) then
for x = Ubound(arrRet, 2) to k+1 step -1
for i = 0 to Ubound(arrRet, 1)
arrRet(i, x) = arrRet(i, x-1)
next
next
for i = 0 to Ubound(arrRet, 1)
arrRet(i, k) = ArrTmp(i, j)
next
exit for
end if
end if
next ' for k = 0 to Ubound(arrRet, 2)
end if
next
sort_arr_mult = arrRet
end function
It looks like you are actually performing a one-dimensional sort of the numeric value with an associated text string just along for the ride.
Your example code is close but you will need 2 temp values to represent the array values you will be shifting around.
For i = LBound(top5) to UBound(top5)
For j = LBound(top5) to UBound(top5) - 1
If top5(j,1) < top5(j + 1,1) Then
TempValue = top5(j + 1,1)
TempText = top5(j + 1,0)
top5(j + 1,1) = top5(j,1)
top5(j + 1,0) = top5(j,0)
top5(j,1) = TempValue
top5(j,0) = TempText
End If
Next
Next
Extending raam's answer with 3rd parameter as sorting direction "ASC" or "DESC"
Function sortArrayMulti(byref ArrTmp, ordPlace, so)
''so: sortorder "ASC" or "DESC"
Dim j, i, k, orderVal, x
Redim arrRet(Ubound(ArrTmp, 1), Ubound(ArrTmp, 2))
for j = 0 To Ubound(ArrTmp, 2)
orderVal = ArrTmp(ordPlace, j)
if j = 0 Then
for i = 0 to Ubound(ArrTmp, 1)
arrRet(i, j) = ArrTmp(i, j)
next
else
for k = 0 to Ubound(arrRet, 2)
if isEmpty(arrRet(0, k)) then
for i = 0 to Ubound(arrRet, 1)
arrRet(i, k) = ArrTmp(i, j)
next
exit for
else
if so = "ASC" then
if orderVal <= arrRet(ordPlace, k) then
for x = Ubound(arrRet, 2) to k + 1 step -1
for i = 0 to Ubound(arrRet, 1)
arrRet(i, x) = arrRet(i, x - 1)
next
next
for i = 0 to Ubound(arrRet, 1)
arrRet(i, k) = ArrTmp(i, j)
next
exit for
end if
else
if orderVal >= arrRet(ordPlace, k) then
for x = Ubound(arrRet, 2) to k + 1 step -1
for i = Ubound(arrRet, 1) to 0 step -1
arrRet(i, x) = arrRet(i, x - 1)
next
next
for i = 0 to Ubound(arrRet, 1)
arrRet(i, k) = ArrTmp(i, j)
next
exit for
end if
end if
end if
next
end if
next
sortArrayMulti = arrRet
End Function
I'm trying to duplicate the behavior of some grid viewports (like 3ds Max's Orthometric view)
or map viewers like (GoogleMaps) where we have the map or grid, which is a whole lot bigger
than the screen, and we navigate by clicking somewhere in the viewport and dragging.
So far, i've managed to create a pretty big grid, draw it and make the viewport show only the tiles it should.
Here is my code so far:
import pygame, sys, math
from pygame.locals import *
FPS = 30
WINDOWWIDTH = 640
WINDOWHEIGHT = 480
GRIDWIDTH = 256
GRIDHEIGHT = 256
GRIDSIZE = 256
TILESIZE = 40
BGCOLOR = (128, 128, 128)
FGCOLOR = (64, 64, 64)
GRID = []
FPSCLOCK = pygame.time.Clock()
indexX = None
indexY = None
minVPcoordX = 0
minVPcoordY = 0
maxVPcoordX = (TILESIZE*GRIDSIZE)-WINDOWWIDTH
maxVPcoordY = (TILESIZE*GRIDSIZE)-WINDOWHEIGHT
viewportOffset = (0, 0)
vpStartXTile = 0
vpStartYTile = 0
viewportCoord = (0, 0)
coordX = 0
coordY = 0
movedDistanceX = 0
movedDistanceY = 0
speed = 4
def main():
global FPSCLOCK, DISPLAYSURF
global coordX, coordY
global offsetX, offsetY, negativeOffsetX, negativeOffsetY
global movedDistanceX, movedDistanceY
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
mouseX = 0
mouseY = 0
generateGrid(GRIDSIZE, GRIDSIZE)
LeftButton = False
mousePos = (0, 0)
dragStart = (0,0)
dragEnd = (0,0)
pygame.font.init()
arialFnt = pygame.font.SysFont('Arial', 16)
while True:
DISPLAYSURF.fill(BGCOLOR)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
#X
if coordX < maxVPcoordX:
coordX += speed
elif coordX < minVPcoordX:
coordX = 0
else:
coordX = maxVPcoordX
#Y
if coordY < maxVPcoordY:
coordY += speed
elif coordY < minVPcoordY:
coordY = 0
else:
coordY = maxVPcoordY
#-------------
viewportCoord = (coordX, coordY)
print(coordX, coordY)
vpStartXTile = math.floor(float(viewportCoord[0]/TILESIZE))
vpStartYTile = math.floor(float(viewportCoord[1]/TILESIZE))
GRIDstartTile = GRID[vpStartXTile][vpStartYTile]
negativeOffsetX = viewportCoord[0] - GRID[vpStartXTile][vpStartYTile][0]
negativeOffsetY = viewportCoord[1] - GRID[vpStartXTile][vpStartYTile][1]
offsetX = TILESIZE - negativeOffsetX
offsetY = TILESIZE - negativeOffsetY
repeatX = math.floor(WINDOWWIDTH/TILESIZE)
repeatY = math.floor(WINDOWHEIGHT/TILESIZE)
drawGrid(repeatX, repeatY)
outputLabel = arialFnt.render('(Top-Left)Coordinates: x%s - y%s' % (coordX, coordY), 1, (255,255,255))
DISPLAYSURF.blit(outputLabel, (10, 10))
# frame draw
pygame.display.set_caption("Memory Game - FPS: %.0f" % FPSCLOCK.get_fps())
pygame.display.flip()
pygame.display.update()
FPSCLOCK.tick(FPS)
def generateGrid(xTiles=None, yTiles=None):
global GRID
GRID = []
for i in range(xTiles):
GRID.append([None] * yTiles)
ix = 0
iy = 0
posX = -40
for x in range(len(GRID[ix])):
posX += TILESIZE
posY = -40
iy = 0
for y in range(xTiles):
posY += TILESIZE
position = (posX, posY)
GRID[ix][iy] = position
iy += 1
if ix < xTiles:
ix += 1
else:
return
def drawGrid(x=None, y=None):
lineWidth = 1
xPos = 0
yPos = 0
for i in range(x):
xStart = (xPos + offsetX, 0)
xEnd = (xPos + offsetX, WINDOWHEIGHT + negativeOffsetY)
pygame.draw.line(DISPLAYSURF, FGCOLOR, xStart, xEnd, lineWidth)
xPos += TILESIZE
for i in range(y):
yStart = (0, yPos + offsetY)
yEnd = (WINDOWWIDTH + negativeOffsetX, yPos + offsetY)
pygame.draw.line(DISPLAYSURF, FGCOLOR, yStart, yEnd, lineWidth)
yPos += TILESIZE
def moveGrid():
pass
def zoomIn():
pass
def zoomOut():
pass
main()
As you can see, it works as expected (i haven't implemented any form of click&drag
in this sample).
It seems that pygame doesn't have an event for this, so it must be a combination of
MOUSEBUTTONDOWN and MOUSEMOTION.
I've tried storing the positions of the previous frame with the get_pos() and subtracting the current frame's positions, but i can't figure out the next.
It accelerates way too fast..
I've also tried this with the get_rel() method of the mouse, with no success.
(Though i'm pretty sure i wasn't supposed to ++ the mouse position to the screen position)
I researched around to see if anyone else did this but i found only how to drag around something
on a fixed screen. I need the opposite - drag the screen around on a fixed grid.
So, if anyone has any idea or advice on how to make this mechanic, or any link that i could study about it, i would be grateful!
ps: I found something similar but it's written in JS and it's a pain to translate)
I got it working!
It still has some issues on the zoomIn/zoomOut additions but the main problem of dragging
the grid around is fixed.
import pygame, sys, math
from pygame.locals import *
FPS = 30
WINDOWWIDTH = 640
WINDOWHEIGHT = 480
GRIDSIZE = 256
TILESIZE = 40
BGCOLOR = (128, 128, 128)
FGCOLOR = (64, 64, 64)
GRID = []
FPSCLOCK = pygame.time.Clock()
indexX = None
indexY = None
minVPcoordX = 0
minVPcoordY = 0
maxVPcoordX = (TILESIZE*GRIDSIZE)-WINDOWWIDTH
maxVPcoordY = (TILESIZE*GRIDSIZE)-WINDOWHEIGHT
viewportOffset = (0, 0)
vpStartXTile = 0
vpStartYTile = 0
viewportCoord = (0, 0)
coordX = 0
coordY = 0
def main():
global FPSCLOCK, DISPLAYSURF
global coordX, coordY
global offsetX, offsetY, negativeOffsetX, negativeOffsetY
global movedDistanceX, movedDistanceY
global isDragging
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
mouseX = 0
mouseY = 0
generateGrid(GRIDSIZE, GRIDSIZE)
isDragging = False
mousePos = (0, 0)
dragStart = (0,0)
dragEnd = (0,0)
pygame.font.init()
arialFnt = pygame.font.SysFont('Arial', 16)
while True:
DISPLAYSURF.fill(BGCOLOR)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == MOUSEBUTTONDOWN:
if event.button == 2:
isDragging = True
elif event.button == 4:
zoomIn()
elif event.button == 5:
zoomOut()
elif event.type == MOUSEMOTION:
mouseRel = pygame.mouse.get_rel()
moveGrid(mouseRel)
elif event.type == MOUSEBUTTONUP:
isDragging = False
viewportCoord = (coordX, coordY)
vpStartXTile = math.floor(float(viewportCoord[0]/TILESIZE))
vpStartYTile = math.floor(float(viewportCoord[1]/TILESIZE))
GRIDstartTile = GRID[vpStartXTile][vpStartYTile]
negativeOffsetX = viewportCoord[0] - GRID[vpStartXTile][vpStartYTile][0]
negativeOffsetY = viewportCoord[1] - GRID[vpStartXTile][vpStartYTile][1]
offsetX = TILESIZE - negativeOffsetX
offsetY = TILESIZE - negativeOffsetY
repeatX = math.floor(WINDOWWIDTH/TILESIZE)
repeatY = math.floor(WINDOWHEIGHT/TILESIZE)
drawGrid(repeatX, repeatY)
outputLabel = arialFnt.render('(Top-Left)Coordinates: x%s - y%s' % (coordX, coordY), 1, (255,255,255))
DISPLAYSURF.blit(outputLabel, (10, 10))
# frame draw
pygame.display.set_caption("Memory Game - FPS: %.0f" % FPSCLOCK.get_fps())
pygame.display.flip()
pygame.display.update()
FPSCLOCK.tick(FPS)
def generateGrid(xTiles=None, yTiles=None):
global GRID
GRID = []
for i in range(xTiles):
GRID.append([None] * yTiles)
ix = 0
iy = 0
posX = -40
for x in range(len(GRID[ix])):
posX += TILESIZE
posY = -40
iy = 0
for y in range(xTiles):
posY += TILESIZE
position = (posX, posY)
GRID[ix][iy] = position
iy += 1
if ix < xTiles:
ix += 1
else:
return
def drawGrid(x=None, y=None):
lineWidth = 1
xPos = 0
yPos = 0
for i in range(x):
xStart = (xPos + offsetX, 0)
xEnd = (xPos + offsetX, WINDOWHEIGHT + negativeOffsetY)
pygame.draw.line(DISPLAYSURF, FGCOLOR, xStart, xEnd, lineWidth)
xPos += TILESIZE
for i in range(y):
yStart = (0, yPos + offsetY)
yEnd = (WINDOWWIDTH + negativeOffsetX, yPos + offsetY)
pygame.draw.line(DISPLAYSURF, FGCOLOR, yStart, yEnd, lineWidth)
yPos += TILESIZE
def moveGrid(rel):
global coordX, coordY, isDragging
if isDragging == True:
#X
if coordX <= maxVPcoordX and coordX >= minVPcoordX:
coordX = coordX - rel[0]
if coordX > maxVPcoordX:
coordX = maxVPcoordX
if coordX < minVPcoordX:
coordX = 0
#Y
if coordY <= maxVPcoordY and coordY >= minVPcoordY:
coordY = coordY - rel[1]
if coordY > maxVPcoordY:
coordY = maxVPcoordY
elif coordY < minVPcoordY:
coordY = 0
#-------------
def zoomIn():
global TILESIZE, maxVPcoordX, maxVPcoordY
TILESIZE += 4
print("Tile size: ", TILESIZE)
generateGrid(GRIDSIZE, GRIDSIZE)
maxVPcoordX = (TILESIZE*GRIDSIZE)-WINDOWWIDTH
maxVPcoordY = (TILESIZE*GRIDSIZE)-WINDOWHEIGHT
def zoomOut():
global TILESIZE, maxVPcoordX, maxVPcoordY
TILESIZE -= 4
if TILESIZE <= 0:
TILESIZE = 4
print("Tile size: ", TILESIZE)
generateGrid(GRIDSIZE, GRIDSIZE)
maxVPcoordX = (TILESIZE*GRIDSIZE)-WINDOWWIDTH
maxVPcoordY = (TILESIZE*GRIDSIZE)-WINDOWHEIGHT
main()