TypeError: cannot concatenate 'str' and 'pygame.Surface' objects - image

I am currently teaching myself game programming, and I've started nice and easy with pygame. I went through a tutorial that showed me how to build a simple game, and now that I am finished with it, I am in the process of trying to reorganize the code in a manner that makes sense to me, and also to edit it and add to it.
Part of what I tried to change is that instead of loading one '.png' file for a character, I load a list of them that will be iterated through in a 'move()' function I designed to make the characters look like they are moving. However I keep running into an error and I don't know why. Near the beginning of my code (all I've done is imported necessary modules and initialized pygame and some necessary variables) I tried to do the following code:
badguyimgs = ['badguy.png', 'badguy2.png', 'badguy3.png', 'badguy4.png']
for img in badguyimgs:
badguyimgs.append(pygame.image.load("resources/images/" + img))
badguyimgs.remove(img)
I keep getting the following error:
TypeError: cannot concatenate 'str' and 'pygame.Surface' objects
So far I have tried to initialize a new variable (resource = "resources/images/" + img) and place that at the beginning of the "for" loop and then insert that into the pygame.image.load(). I've also tried using os.path.join("resource/images/" + img). I've tried using the full path name ("c:\\Users\\ . . . \\resources\\images\\" +img). But any time I try to concatenate the pathname with the file name in the list, I get the above error code. I tried looking in the pygame documentation, but didn't see anything that helped in this situation. I've tried googling the error, but get nothing in reference to this. (a lot of issues with people tring to concatenate int types to strings though. . . ) I would appreciate any help anyone could give in pointing out why I am experiencing this, and what could fix it. Thanks.

It looks like what you're doing is appending the pygame.surface object (that you loaded from a png file) to the list while you're iterating through it. You are loading the images successfully. However after your function adds the first image and removes the string, your list looks like this:
badguyimgs = ['badguy2.png', 'badguy3.png', 'badguy4.png', pygame.image]
You are still iterating through the list, so your function starts trying to concatenate the string and the pygame.surface object. I would recommend creating an empty list, and add your loaded images to that list without adding or removing anything from the original. Hope this helped!

Here's an example to go with PlatypusVenom's explanation:
file_names = ['badguy.png', 'badguy2.png', 'badguy3.png', 'badguy4.png']
images = []
for file_name in file_names:
images.append(pygame.image.load("resources/images/" + file_name))
Now the pygame.Surface objects are in images, and the variable names for the lists are less confusing. Another option is to use a list comprehension:
images = [pygame.image.load("resources/images/" + file_name) for file_name in \
("badguy.png", "badguy2.png", "badguy3.png", "badguy4.png")]
This is similar to what you were going for in the code posted. The list of strings will be removed from memory, leaving only pygame.Surface objects in the images list.

Related

What is "setindex! not defined" error in julia?

When I run my code one error occurs with my code setindex! not defined for WeakRefStrings.StringArray{String,1}
CSV File here.
using CSV
EVDdata =CSV.read(raw"wikipediaEVDdatesconverted.csv")
EVDdata[end-9:end,:]
And the Error Code is here
rows, cols = size(EVDdata)
for j =1:cols
for i = 1:rows
if !isdigit(string(EVDdata[i, j])[1])
EVDdata[i,j] = 0
end
end
end
I am working with Julia 1.4.1 on Jupter Notebook
setindex!(collection, item, inds...) is the function that colection[inds...] = item gets lowered to. The error comes from the fact that CSV.read makes an immutable collection.
As Oscar says in his answer, setindex! tries to mutate its arguments, i.e. change the contents of your column in place. When you do CSV.read(), by default immutable columns of type CSV.Column are returned. This is done for performance reason, as it means columns don't have to be copied after parsing.
To get around this, you can do two things:
Pass the keyword argument CSV.read(raw"wikipediaEVDdatesconverted.csv", copycols = true) - this will copy the columns and therefore make them mutable; or
Achieve the same by using DataFrame((raw"wikipediaEVDdatesconverted.csv"))
The second way is the preferred way as CSV.read will be deprecated in the CSV.jl package.
You can see that it's current implementation is basically doing the same thing I listed in (2) above in the source here. Removing this method will allow CSV.jl not to depend on DataFrames.jl anymore.
It could also be done this way
col1dt = Vector{Dates.DateTime}(undef, length(col1))
for v = 1:length(col1)
col1dt[v] = Dates.DateTime(col1[v], "d-u-y")
end

Photoshop script .DS_Store

I'm using Photoshop script. I get files from folders. My problem is that when I get the files and place them in an array the array contains hidden files that are in the folder for example ".DS_Store". I can get around this by using:
if (folders[i] != "~/Downloads/start/.DS_Store"){}
But I would like to use something better as I sometimes look in lots of folders and don't know the "~/Downloads/start/" part.
I tried to use indexOf but Photoshop script does not allow indexOf. Does anybody know of a way to check if ".DS_Store" is in the string "~/Downloads/start/.DS_Store" that works in Photoshop script?
I see this answer but I don't know how to use it to test: Photoshop script to ignore .ds_store
For anyone else looking for a solution to this problem, rather than explicitly trying to skip hidden files like .DS_Store, you can use the Folder Object's getFiles() method and pass an expression to build an array of file types you actually want to open. A simple way to use this method is as follows:
// this expression will match strings that end with .jpg, .tif, or .psd and ignore the case
var fileTypes = new RegExp(/\.(jpg|tif|psd)$/i);
// declare our path
var myFolder = new Folder("~/Downloads/start/");
// create array of files utilizing the expression to filter file types
var myFiles = myFolder.getFiles(fileTypes);
// loop through all the files in our array and do something
for (i = 0; i < myFiles.length; i++) {
var fileToOpen = myFiles[i];
open(fileToOpen);
// do stuff...
}
For anybody looking I used the Polyfill found here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
indexOf() was added to the ECMA-262 standard in the 5th edition; as
such it may not be present in all browsers. You can work around this
by utilizing the following code at the beginning of your scripts. This
will allow you to use indexOf() when there is still no native support.
This algorithm matches the one specified in ECMA-262, 5th edition,
assuming TypeError and Math.abs() have their original values.

Loading OBJ file in three.js

I was trying to visualize a simple 3D model of the cylinder on the browser by importing OBJ file in three.js. I started with running simple example of three.js's OBJ loader:
https://github.com/mrdoob/three.js/blob/master/examples/webgl_loader_obj.html
which works fine locally.
I tried just by replacing the path of OBJ file with that of mine, but it failed to load. I double check the Path and it is correct.
On checking errors on the console on Firefox (Crtl + Shift + k), it says: Syntax Error and throws following error:
[22:59:30.865] Error: WebGL: DrawElements: bound vertex attribute buffers do not have sufficient size for given indices from the bound element array # http://localhost/~harmanpreet/three.js/build/three.min.js:455
The OBJ file is converted from model made in BRL-CAD (.g to .obj conversion). Link to OBJ file: http://devplace.in/~harman/cyl1_bot_dump.obj
Can anybody figure out what is the problem?
Thank you
Your .obj file looks correct according to specs, but I would advise you to use a non-minified three.js version and then look at the code surrounding the error message.
Also what you could try is replacing the "g" in the obj file at start with an "o" (g = group, o = object) - I'm not sure how three.js handles this internally or if it makes a difference, but I guess it won't hurt to try.
Other than that the error seems to be saying something like "I encountered an array index which is out of bounds", meaning a face (f in the .obj file) uses an index which is higher than the highest defined index, but that doesn't seem to be the case with your file (.obj indexes start at 1, so everything should be fine).

how to pass parameters to a Matlab GUI file

i am new to matlab. While working through the Matlab GUI, i faced a problem which is as follows..i want to have 2 figure files, with one figure file calling the other. i know that just by calling the name of the 2nd fig file from the first fig file, we can call the 2nd figure. however, i also wish to send some parameters from one fig file to another.here i need to send the arguments and also obtain these parameters so as to do further processing.i havent been able to find a solution to this problem. i would be glad if someone helps me out with this problem. thanking you in advance
There are three ways I found to do this:
Method 1: Use setappdata and getappdata like so:
setappdata(0,'some_var',value)
some_other_var = getappdata(0,'some_var')
You would use setappdata() in the m-file for fig1 to store whatever data you wanted to pass around, and then call getappdata() in another m-file to retrieve it. The argument 0 to the two functions specifies the MATLAB root workspace, which is accessible by your program everywhere (i.e. it is global). As such, when you close your figures that data will still be available. You may want to use rmappdata to remove them.
Method 2: Use guidata:
Assuming you created your GUI with GUIDE, then you have access to a structure called handles which is passed around everywhere and which you can edit, and so you can do this in a GUI callback:
handles.some_var = some_value
guidata(hObject,handles)
Then you can access handles.some_var elsewhere in some other callback (because handles is automatically passed into it for you) in your other m-file:
some_other_var = get(handles.some_var)
Method 3: Use UserData:
Store the variable you want from your first figure:
set(name_of_fig, 'UserData', some_var)
Then to get it from your other one:
some_other_var = get(name_of_fig, 'UserData')
(Disclaimer: My actual knowledge of MATLAB is not all that great, but it helps to be able to find good resources like this and this, and even this from the official docs. What I've written here may be wrong, so you should definitely consult the docs for more help.)
I would do like this (assuming you're using the GUI builder GUIDE).
Let's say that your figures/m-files are named firstFigure.fig/m and secondFigure.fig/m. In the code of firstFigure, just call secondFigure and pass your parameters as arguments:
someNumber = 1;
someText = 'test';
aMatrix = rand(3);
secondFigure(someNumber, someText, aMatrix);
The arguments will be available to secondFigure as a variable varargin in the callback functions
function varargout = secondFigure(varargin)
and
function secondFigure_OpeningFcn(hObject, eventdata, handles, varargin)
varagin is a cell structure; use cell2mat and char to convert it back:
theNumber = cell2mat(varargin(1));
theText = char(varargin(2));
theTextAgain = cell2mat(varargin(2));
theMatrix = cell2mat(varargin(3));
This may help:
http://www.mathworks.ch/matlabcentral/newsreader/view_thread/171989
The easiest method is to wrap the parameters in a cell array and send them directly to the GUI constructor. A call with two parameters might look like:
figure2({param1, param2})
Then you can unpack the arguments in the opening function (figure2_OpeningFcn) with code like:
handles.par1 = varargin{1}{1};
handles.par2 = varargin{1}{2};
These lines must be placed somewhere before the line that says guidata(hObject, handles);. Then you can access handles.par1 and handles.par2 directly in all the other callbacks.
I assume you are using GUIDE to generate your GUI. You can find figure2_OpeningFcn in figure2.m which will be located in the same directory as figure2.fig.
Note: you can also return values from a figure, returnvalue = my_figure({my_input}). If you'd like instructions on that too, leave a comment and I'll extend my answer.

Pass data from workspace to a function

I created a GUI and used uiimport to import a dataset into matlab workspace, I would like to pass this imported data to another function in matlab...How do I pass this imported dataset into another function....I tried doing diz...but it couldnt pick diz....it doesnt pick the data on the matlab workspace....any ideas??
[file_input, pathname] = uigetfile( ...
{'*.txt', 'Text (*.txt)'; ...
'*.xls', 'Excel (*.xls)'; ...
'*.*', 'All Files (*.*)'}, ...
'Select files');
uiimport(file_input);
M = dlmread(file_input);
X = freed(M);
I think that you need to assign the result of this statement:
uiimport(file_input);
to a variable, like this
dataset = uiimport(file_input);
and then pass that to your next function:
M = dlmread(dataset);
This is a very basic feature of Matlab, which suggests to me that you would find it valuable to read some of the on-line help and some of the documentation for Matlab. When you've done that you'll probably find neater and quicker ways of doing this.
EDIT: Well, #Tim, if all else fails RTFM. So I did, and my previous answer is incorrect. What you need to pass to dlmread is the name of the file to read. So, you either use uiimport or dlmread to read the file, but not both. Which one you use depends on what you are trying to do and on the format of the input file. So, go RTFM and I'll do the same. If you are still having trouble, update your question and provide details of the contents of the file.
In your script you have three ways to read the file. Choose one on them depending on your file format. But first I would combine file name with the path:
file_input = fullfile(pathname,file_input);
I wouldn't use UIIMPORT in a script, since user can change way to read the data, and variable name depends on file name and user.
With DLMREAD you can only read numerical data from the file. You can also skip some number of rows or columns with
M = dlmread(file_input,'\t',1,1);
skipping the first row and one column on the left.
Or you can define a range in kind of Excel style. See the DLMREAD documentation for more details.
The filename you pass to DLMREAD must be a string. Don't pass a file handle or any data. You will get "Filename must be a string", if it's not a string. Easy.
FREAD reads data from a binary file. See the documentation if you really have to do it.
There are many other functions to read the data from file. If you still have problems, show us an example of your file format, so we can suggest the best way to read it.

Resources