Why does xcor and ycor not work for my border check? - python-turtle

I am using the Turtle module for Python and am making a fairly simple game. I started to code for Border checking and am following a video to do it, but it doesn't seem to work. I had used the same code in the past and it was fine but now nothing happens.
The Code:
Border Limitations
global player
if player.xcor() > 600 or player.xcor() < -600:
player.right(180)
if player.ycor() > 400 or player.ycor() < -400:
player.right(180)

I believe you should put that inside the main game-loop, instead of a function, cross out the global.

Related

How to access a layer inside UIView custom class generate with Paintcode?

I'm using Paintcode V3 and last Xcode for my project.
In Paintcode I draw a red path, kind of "clock hand" with a radius variable to be able to rotate it.
In my Xcode project, this custom UIView is added to a main view.
When the red path is rotating, I need to know when it intersects with another element of the view.
I tried:
if self.redPathView.frame.intersects(self.anotherView.frame) {
//do something...
}
but it can't work because redPathView is the entire Paintcode canvas (the square frame) and not only the red path.
I'm searching a way to access to the red path of my view.
Interesting problem.
I don't think there is a way to do exactly what you are looking for...
Like you mentioned, when we use PaintCode, our custom drawings become an entire UIView.
We can't access its specific elements (like your redPathView).
(I'm favoriting this question in case anyone knows if we actually can. I may be wrong.)
Anyway, there is a workaround. You could use PaintCode itself to tell you when "collisions" happen. Of course, then you would need to draw everything there... And implement some sort of internal control that would make sense to you.
For example, in the picture below I created this "clock hand", with functions to detect when the red thing touches other UI elements (key point there is the touchedElementID expression -- observe how it updates itself with the "ID" of the rectangles):
There are some issues you may find with this approach, for example the need to know the exact position of other elements on the screen and possibly complex ternary constructions like:
(rotationSanitized <= -352 || rotationSanitized >= -8) ?
1 :
(rotationSanitized <= -82 && rotationSanitized >= -98) ?
2 :
(rotationSanitized <= -172 && rotationSanitized >= -188) ?
3 :
(rotationSanitized <= -262 && rotationSanitized >= -278) ?
4 : 0
Also, how to "export" and consume this information along with the StyleKit in Xcode?
The trick I did was to add an empty (no Fill, no Stroke) UI element in PaintCode and associate the touchedElementID expression result to the alpha value of the element.
This way I could access the expression in my custom view, for example via StyleKit.touchedElementID. But that introduces a new problem since now I have to change the generated StyleKit every time it is exported (two lines of code, but still). That is kind of a fragile solution.
However, if you are ok with that, then it's just a matter of consuming this new information...
For that I created a protocol, so my ViewController can act as the delegate of my custom view, and perform whatever it needs to do when the most internal UI elements in PaintCode are "touched".
Here is the final result:
Finally, check this project in GitHub with all the code and samples.

Multiple light sources in a tile-based lighting system

I have made a rudimentary tile based lighting system in ROBLOX by changing color based on distance from lightsource. I want to be able to put multiple lightsources, but I've ran into some problems:
The performance goes down a bit (from full 60 to 45-50) when I try to add another light source.
It doesn't work correctly, the lightsources cut out eachother's light and such.
my code:
local sp = script.Parent
local lightBrightness
local lightPower = 1
local grid = game.ReplicatedStorage.Folder:Clone()
grid.Parent = workspace.CurrentCamera
local lightSource = game.ReplicatedStorage.LightSource:Clone()
lightSource.Parent = workspace.CurrentCamera
lightSource.Position = grid:GetChildren()[math.random(1,#grid:GetChildren())].Position
for _, v in pairs(grid:GetChildren()) do
if v:IsA("Part") then
game["Run Service"].RenderStepped:connect(function()
local lightFact = (lightSource.Position-v.Position).magnitude
lightBrightness = lightPower - (lightFact*10)/255
if lightFact < 35 then
v.SurfaceGui.Frame.BackgroundColor3 = v.SurfaceGui.Frame.BackgroundColor3.r >= 0 and Color3.new((100/255)+lightBrightness*.85,(50/255)+lightBrightness*.85,10/255+lightBrightness) or Color3.new(0,0,0)
elseif lightFact > 35 and lightFact < 40 and v.SurfaceGui.Frame.BackgroundColor3.r > 0 then
v.SurfaceGui.Frame.BackgroundColor3 = Color3.new(0,0,0)
end
end)
end
end
The reason your code is running slowly is because the RenderStepped event runs every frame render, which is (typically) every 1/60th of a second. Try using the Stepped event to improve performance, as it only fires ever 1/30th of a second.
As for your light sources cancelling each other out, it appears from looking over your code that each light source affects the tiles around it without checking what light sources were affecting the tile before.
For example, light source A has a lightFact of 37 affecting tile A, so tile A changes it's appearance. Then, light source B, which is later in the loop, has a lightFact of 32. Well, the final lightFact value for that tile is going to be 32, effectively cancelling out the initial light sources effect.
In order to fix this issue, you need to store the current lightFact variable for each tile as you progress through the loop. My recommendation (alas, maybe not the fastest) would be to create a new Instance of IntValue inside of each tile, storing the current affecting lightFact value on the tile. Then, whenever a light source is about to check the tile, check if it has a child lightFact, and if it does, add that value to the lightFact value in your code. Therefore, the lightFact value you use to apply to the tile doesn't cancel out the last light source, because it incorporates that other lightsources lightFact value in its own calculation. Make sense?

Corona SDK dynamic width objects

I'm trying to implement a game mechanics of "Hooking" objects... Here's picture of what i'm talking about:
But I have a problem with the chain/string of the hook.
I've tried create it step by step when the hook is on the fly, using timer and enterFrame events, but they both generate huge gaps between chain segments.
I've tried creating big image of whole chain and changing width dynamicly, but it only stretches and looking funny :D
Maybe anyone faced the same problem?
I would suggest you to try following.
Create whole chain
place it inside container (display.newContainer)
change width of container.
Container automatically generates masks so instead of stretching you will have just part of your chain hidden under mask.
here is link to container docs http://docs.coronalabs.com/daily/guide/graphics/container.html
This is not a complete answer - I have given you a bit of a broad answer to a broad question but I hope it can help you in creating what you need.
I created a very similar mechanic recently -
I used enterFrame to call a function, lets call it BuildHook, this would check the position of the hook and the position of the starting point and then calculate how many pieces of images would be needed to fill out the space. The graphic I had as my "hook" was partially larger then the chain pieces allowing for it to overlap and only put a new image once enough space between the last piece and hook was created.
This ment that it was the hook moving forward and backwards and then having a static "chain" between it and the starting point. Either consuming "links" as would move backwards ontop of them or created "links" as it moved away from them.
Keep in mind this is pseudo code but should give you an idea how to make it.
local arrayOfPieces = {}
local function BuildHook()
-- Bunch of variables you need to get
startX,startY,
hookX,hookY,
lastPieceX,lastPieceY,
singlePieceWidth
-- The hook pseudo code
if(hookX - lastPieceX >= singlePieceWidth) then
local newPiece = display.newImage('singlePiece.png');
newPiece.x = lastPieceX + singlePieceWidth/2
newPiece.y = lastPieceY
table.insert(arrayOfPieces,newPiece)
end
if(hookX < lastPieceX) then
lastPieceArrayPos = table.getn(arrayOfPieces)
table.remove(arrayOfPieces,lastPieceArrayPos)
lastPiece = arrayOfPieces[lastPieceArrayPos - 1]
end
end

Make Physics node move horizontal without spinning

Hello in a game I'm making using lua in Marmalade Quick,I have run into a problem with the physics.
I have a normal downward y gravity and have some notes that is affected by that.
Now I want to add some objects that "fly" horizontally on the X axis but I can not get it to work.
so one of the notes looks like this:
sky2 = director:createSprite(dw, 40, "textures/tractor.png")
physics:addNode(sky2, {type="dynamic"})
sky2.physics:setGravityScale(0)
my first thought was to
just add the following to an update listener
if(gameplaying == true) then
sky2.x = sky2.x-2.5
unfortunately this does not work after the node has got added physics
then I was looking into using
sky2.physics:applyapplyLinearImpulse or sky2.physics:applyForce
I used it like this
sky2.physics:applyapplyLinearImpulse(-10, 0, -20, 40)
The problem here is that the node then correctly moves along the axis but it is spinnig around (torque effects)..
Is there away to stop this or what am I doing wrong,,
thanks..
Found out that the Marmalade Quick Documentation was wrong, and to not input both a px and a px value but just 0 so sky2.physics:applyapplyLinearImpulse(-10, 0) this will apply the impulse at the centre of mass and make it move straight.

Image search and transparency

I need to find an image (detect text in game) using transparency (the background is changing every few seconds). My script:
#include <ImageSearch.au3>
HotKeySet("s", "Start")
$x = 0
$y = 0
Func Start()
$Search = _ImageSearchArea("*Trans0x40FF00 " & "trans_test.png",1,90,90,#DesktopWidth,#DesktopHeight,$x,$y,50)
If $Search = 1 Then
MouseMove($x, $y,10)
EndIf
EndFunc
While 1
Sleep(100)
WEnd
It doesn't work. My image to search has a bright green colour 0x40FF00 all around, representing transparency. How to make my image match?
First of all, if your game runs in DirectX mode or similar (as most fullscreen games do). This won't work at all. (A DirectX hook or something like that would be required and I don't think someone has been going through the troubles of writing the code necessary to do in from AutoIt/AHK.)
Otherwise, in Autohotkey there is the *n option which may do the job for you when adjusted correctly. It's possible this exists in AutoIt too.
(*TransN does exist in AutoHotkey too btw.)

Resources