I want to draw a specific curve line to Photoshop or to mspaint. This drawing action should be saved for the possibility to redraw that curve in the exact same way. How can I do it with Autoit? Is there a recording and play mechanism? As far as I read, the AU3 recorder is not available anymore.
Photoshop is just an example. I want to be able to do that kind of drawing record for different purposes and programs. Maybe also for online image editors or something.
I am not that familiar with Autoit yet. I do not expect a full code example, maybe you can give me an idea - that would be very helpful.
Currently I tried a bit with mouse functions like MouseDown, MouseMove etc. and it is quite funny, but i do not really have a concept to record and redraw these mouse actions.
If I have to clarify more please let me know - i will do my best to be precise.
I recommend using two scripts, one for recording and the second to replay recorded actions.
Code for the recording:
; declaration
Global $sFileCoordinates = #ScriptDir & '\RecordedMouseMoveCoordinates.txt'
Global $iRecordingDurationInSeconds = 10
Global $iXSave, $iYSave
; functions
Func _recordMouseMoveCoordinatesToFile()
Local $aPos = MouseGetPos()
If $aPos[0] <> $iXSave Or $aPos[1] <> $iYSave Then
FileWrite($hFile, $aPos[0] & ',' & $aPos[1] & #CRLF)
Local $aPos = MouseGetPos()
$iXSave = $aPos[0]
$iYSave = $aPos[1]
EndIf
Sleep(80)
EndFunc
; processing
Sleep(4000) ; wait 4 seconds to place your mouse to the start position
Global $hFile = FileOpen($sFileCoordinates, 1 + 256)
Global $hTimer = TimerInit()
While Round((TimerDiff($hTimer) / 1000), 1) <= $iRecordingDurationInSeconds
ToolTip(Round((TimerDiff($hTimer) / 1000), 1))
_recordMouseMoveCoordinatesToFile()
WEnd
FileClose($hFile)
Recording will start after a 4 second delay. This should allow to move your mouse to the start point of your drawing action.
Global $iRecordingDurationInSeconds = 10 means your drawing action should be finished in 10 seconds (a tooltip displays remaining seconds). And here the seconds script.
Code to redraw curve:
; declaration
Global $sFileCoordinates = #ScriptDir & '\RecordedMouseMoveCoordinates.txt'
; functions
Func _getFileContent($sFile)
Local $hFile = FileOpen($sFile, 256)
Local $sFileContent = FileRead($hFile)
FileClose($hFile)
Return $sFileContent
EndFunc
Func _drawRecordedMouseMoveCoordinatesFromFile($sContent)
Local $aFileContent = StringSplit($sContent, #CRLF, 1)
Local $iX = StringSplit($aFileContent[1], ',')[1]
Local $iY = StringSplit($aFileContent[1], ',')[2]
MouseMove($iX, $iY, 4)
MouseDown('left')
For $i = 1 To $aFileContent[0] Step 1
If $aFileContent[$i] <> '' Then
Local $iX = StringSplit($aFileContent[$i], ',')[1]
Local $iY = StringSplit($aFileContent[$i], ',')[2]
MouseMove($iX, $iY, 4)
EndIf
Next
MouseUp('left')
EndFunc
; processing
Sleep(2000) ; wait 2 seconds till start
Global $sFileContent = _getFileContent($sFileCoordinates)
_drawRecordedMouseMoveCoordinatesFromFile($sFileContent)
There is a start delay of 2 seconds. All saved coordinates will be executed in the same way recorded. It starts with MouseDown('left'), then the mouse movements to MouseUp('left').
Notice:
This approach isn't really robust because of the coordinates which aren't relative to your window. Please see Opt('MouseCoordMode', 0|1|2) in the help file for more information. If you want to draw more than just one line or curve this approach isn't the best. But as your question describes only that requirement it should be fine.
Related
I want to program my game in such a way that as your points progress, the obstacles that are approaching the player are faster and there are more of them at once. I've played around with my code and have done some research, but everything I try seems to fail. Python isn't giving me any errors, it is just that more obstacles are not appearing.
enemy = pygame.image.load('tauros1.png')
....
def things(thingx, thingy):
gameDisplay.blit(enemy, (thingx, thingy))
....
thing_starty = -100
thing_speed = 7
thing_width = 42
thing_height = 43
thing_startx = random.randrange(192, displayWidth - 192)
dodged = 0
....
things(thing_startx, thing_starty)
thing_starty += thing_speed
if thing_starty > displayHeight:
thing_starty = 0 - thing_height
thing_startx = random.randrange(192, ((displayWidth - 192) - thing_width))
dodged += 1
thing_speed += 2
These are the components of the code that make up the enemy base. I've tried implementing while loops, for loops, and embedded if statements. I can't think of anything else to try.
I can't give you a complete answer with code specific for your game, but I can give you a few tips to make the whole thing simpler, which will in turn give you more control over all the elements in your game.
First off, you should have a "container" for your sprites. You could have one for everything or, better, have them grouped in a way that makes sense. For example you might have environment sprites, enemies, allies, HUD, etc. What determines how you group your sprites is essentially what they do: similar-behaving sprites should be grouped together.
You could use an array, but since you're using pygame I'd suggest pygame.sprite.Goup - this implements methods like .update and .draw which will greatly improve the comprehensibility of your code - and therefore your control over the game.
The you should have a "game loop" (though I'm guessing you already do), which is the place where everything happens, at each frame. Something like:
# This code is only intended as a generic example. It wouldn't work on it's
# own and has many placehoders, of course. But you can adapt your
# game to have this kind of structure.
# Game clock keeps frame rate steady
theClock = pygame.time.Clock()
# Sprite groups!
allsprites = pygame.sprite.Group()
environmentSprites = pygame.sprite.Group()
enemySprites = pygame.game.Group()
# Initially populate sprite groups here, e.g.
environmentSprites.add(rockSprite)
allsprites.add(rockSprite)
# ...
# Game loop
while True:
theClock.tick(max_fps)
# Add new enemies:
spawn_enemies(enemySprites, allSprites, player_score)
# Update all sprites
environmentSprites.update()
enemySprites.update()
playerSprite.update()
# ...and draw them
allSprites.draw(background)
window.blit(background)
pygame.display.flip()
This way you can keep your game loop short (as in not many lines, externalize most of the actual work to functions and methods) and therefore have more control over what happens.
Achieving what you wanted is now much, much simpler. You have a single function which does that, and only that, which means that you can focus on just how you want the enemies to spawn in relation to the player_score. Just an example off the top of my head:
def spawn_enemies(enemyGroup, all, player_score):
# Calculate the number of enemies you want
num_enemies = player_score / 100
for i in range(num_enemies):
# Create a new enemy sprite instance and
# set it's initial x and y positions
enemy = EnemyClass()
enemy.xpos = random.randint(displayWidth, displayWidth + 100)
enemy.ypos = random.randint(0, displayHeight)
enemy.velocity = (player_score / 100) * base_velocity
# Add the sprite to the groups, so it'll automatically
# be updated and blitted in the main loop!
enemyGroup.add(enemy)
all.add(enemy)
I suggest you read up on the pygame docs and tutorials for things like pygame.sprite.Sprite, pygame.sprite.Group and pygame.time.Clock if you haven't already. I found them really useful when developing my games. The tutorialy also give a pretty good idea of how the game is best structured.
Hope this helps.
I am fairly new to coding with python but I have to learn it as part of my PhD. I want to program a task in which on a straight line (top to bottom) there are 4 possible positions (here circles) that are target zones. Later a stimulus will be presented that corresponds to one of these targets. Afterwards the subject will have to move a mouse cursor on the line to select the correct stimulus. I want to do the input via the wheel of the computer mouse. So if I scroll up the cursor on the button moves along the line and can be placed on one of the zones and then a reward is given.
To my question: How do I implement the input of the mouse wheel, bind it to the cursor and restrict the the cursor to stay on the line (disable all other mouse movements) ?
Thank you in advance.
In PsychoPy Coder --> Demos --> input --> mouse, you will see a demo script on how to react to different kinds of mouse input. Also, see the documentation. In code, you would do something like this, where points is a list of coordinates on your line.
# Your points
points = [[0, 0], [1, 0.5], [2, 1], [3, 1.5]]
# Set up mouse
from psychopy import visual, event
win = visual.Window() # A mouse must be in a window
mouse = event.Mouse() # initialize mouse object
# Begin listening
event.clearEvents('mouse') # Do this in the beginning of every trial, to discard "old" mouse events.
index = 1 # start value
while not any(mouse.getPressed()): # for this example, keep running until any mouse button is pressed
#print mouse.getWheelRel()
scroll_change = int(mouse.getWheelRel()[1]) # returns (x,y) but y is what we understand as scroll.
if scroll_change != 0:
index += scroll_change # increment / decrement index
index = min(len(points)-1, max(index, 0)) # Round down/up to 0 or number of points
print points[index] # print it to show that it works. You would probably do something else here, once you have the coordinates.
I know I can turn axes on and off in bokeh with p1.xaxis.visible = None or p1.yaxis.visible = None from Hide Axis in Bokeh. What if I have an extra y range I want to change the visibility of? I've defined my extra axis like this:
plot.extra_y_ranges = {'ORP': Range1d(start=0, end=plot_d['y_axis2_max'])}
plot.add_layout(LinearAxis(y_range_name='ORP', axis_label='ORP, mV'), 'left')
I tried plot.extra_y_ranges.visible = None but it has no effect, and I wasn't able to find anything in the documentation. Have I missed something?
You need to change the visibility of the lines, not the axis.
I've done this in a project on Github that displays temperature and humidity data (amongst other things). The humidity data is the extra y axis and I have check boxes to show/hide temperature and/or humidity. Here's the function that shows/hides the lines on the chart:
def h_t_lines_changed(self, active):
"""Helper function for h_t_tab - turns lines on and off"""
for index in range(len(self.h_t_line)):
self.h_t_line[index].visible = index in active
Here's the line definitions:
self.h_t_line[0] = self.h_t_fig.line(x='Timestamp',
y='Temperature (C)',
source=self.source,
color="blue",
legend="Temperature",
line_width=2)
self.h_t_line[1] = self.h_t_fig.line(x="Timestamp",
y="Relative humidity (%)",
source=self.source,
y_range_name="humidity",
color="green",
legend="Humidity",
line_width=2)
and here's the checkbox code, including the callback:
resp_b = [0, 1]
h_t_check_head = Div(text="Responses")
h_t_check = CheckboxGroup(labels=["Temperature", "Humidity"],
active=resp_b,
name="Lines")
h_t_check.on_click(self.h_t_lines_changed)
I'm updating my project now. If you want me to post a link to it, let me know.
The screenshot is a snip of the left side of my main frame. This frame has a toolbook at the top. I've set the background color of both the toolbar in the toolbook and the frame to 0,0,0 but, as you can see, there is a small blank line between the two. How do I get that line gone or how do I make it black, as well?
As a quick side question -- is there a way to change the color of the "selected" halo on the toolbar to something a bit more contrasted for black? You can see the very faint blue halo around that first icon, I'd like that to be a much lighter blue, if possible.
EDIT: Code added --
il = wx.ImageList(128, 128)
for tab in self.package.vars["tabs"]:
il.Add(wx.Image(self.package.vars["iconPath"].format(tab), wx.BITMAP_TYPE_PNG).ConvertToBitmap())
self.AssignImageList(il)
imageIdGenerator = self.getNextImageID(il.GetImageCount())
pages = [(wx.Panel(parent = self.parent, id = wx.ID_ANY), name.capitalize()) for name in self.package.vars["tabs"]]
imID = 0
toolbar = self.GetToolBar()
toolbar.SetBackgroundColour(self.package.vars["bgColor"])
toolbar.AddStretchableSpace()
for page, label in pages:
self.AddPage(page, label, imageId=imageIdGenerator.next())
page.SetBackgroundColour(c.COLORS["green"])
imID += 1
toolbar.AddStretchableSpace()
pages = [(wx.Panel(parent = self.parent, id = wx.ID_ANY), name.capitalize()) for name in self.package.vars["tabs"]]
maybe this wx.Panel in the list comprehension still has the default color. what happens if you apply SetBackgroundColour to these panels as well?
(edit)
How about removing that boarder space by
self.SetInternalBorder(0)
self here is Toolbook class. It seems to work on wxPython Demo Toolbook example.
Can I change the scrollregion on a Python turtle canvas? I want the drawing to move with it, not just the coordinates to shift. The appearance I'm going for is side-scroller like, where the screen's display region moves to center the turtle onscreen.
I've tried using turtle.setworldcoordinates(llx, lly, urx, ury), but, from the documentation, "This performs a screen.reset()". I've also looked at this SO question , but this involves scroll bars, will not center the turtle easily, and has a limited canvas space. What I'm looking for is something that:
Moves the display region to center the turtle
Also moves the drawing
Has an infinite scroll region
does not display scroll bars
can be called quickly with a function
My best guess would be to be able to have an infinite scrolled canvas somehow, then hide the scroll bars and set them according to turtle position.
Is this possible in Python 2.7? I don't mind if it uses tkinter as well.
EDIT: 6-3-15
I found the canvas.xview and canvas.yview functions, but they don't seem to work once I define screen = turtle.TurtleScreen(canvas), and TurtleScreen has no xview or yview functions. I can't seem to make this work.
Then I found turtle.ScrolledCanvas(). This seems ideal except it has no methods for setting scroll manually from the program. Can I set the scroll manually on a turtle.ScrolledCanvas()???
The position of a canvas can be changed without a reset using canvas.place() method. It will move the turtle and the drawings too, so the turtle needs to be relocated after every move.
The next code moves the canvas with Left and Right arrows and draws a circle with Space, while keeping the turtle in the center. No ScrolledCanvas needed, just a very large standard canvas:
import turtle
import Tkinter as tk
def keypress(event):
global xx, canvas, t, speed
ev = event.keysym
if ev == 'Left':
xx += speed
else:
xx -= speed
canvas.place(x=xx)
t.setposition((-canvas.winfo_width() / 4) - (xx + 250), 0)
return None
def drawCircle(_):
global t
t.pendown()
t.fillcolor(0, 0, 1.0)
t.fill(True)
t.circle(100)
t.fill(False)
t.fillcolor(0, 1, 0)
t.penup()
# Set the main window
window = tk.Tk()
window.geometry('500x500')
window.resizable(False, False)
# Create the canvas. Width is larger than window
canvas = turtle.Canvas(window, width=2000, height=500)
xx = -500
canvas.place(x=xx, y=0)
# Bring the turtle
t = turtle.RawTurtle(canvas)
t.shape('turtle') # nicer look
t.speed(0)
t.penup()
t.setposition((-canvas.winfo_width() / 4) - (xx + 250), 0)
# key binding
window.bind('<KeyPress-Left>', keypress)
window.bind('<KeyPress-Right>', keypress)
window.bind('<KeyPress-space>', drawCircle)
drawCircle(None)
speed = 3 # scrolling speed
window.mainloop()
Having a real infinite scrolling would require to redraw every item in the canvas every time with the required offset, instead of actually moving or scrolling the canvas. Functions like create_image() can give the illusion of movement with static backgrounds, but it resets the drawings.