The best way of generating a tile map with canvas - html

So I have a little question here, I'm making a small game right now with canvas, probably going to use OpenGL later but not at the moment.
Now my question is, I see a lot of users doing it with a bunch of arrays like this :
var map = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
Is this really the best way of doing? This seems a bit overkill when you want to create new maps and I suppose big games that use these kind of pixel art maps are not using something like this, or at least not something like this directly.
So how does this works? Do they create an engine that will output these kind of arrays? Like a little gui where you can create a map and then the software will generate this for you? Or how is this all working

Create an editor that gives you a convenient UI to put together new maps and saves them as files. Then include those files in your project. Add a small component that loads them into an array.

Related

How to color specific nodes in the bipartite graph?

I have plotted a bipartite graph using igraph. I want to color specific nodes of my interest with different colors.
The code which i have used is
bmat=as.matrix(read.csv(file = "/home/tejaswini/Desktop/Butterflyplantdata.csv" , sep = "\t", header=T, row.names=1))
bmat
library(igraph)
bg=graph_from_incidence_matrix(bmat)
bg
V(bg)$type #Display the vertex types. They are "FALSE" or "TRUE"
library(bipartite)
plotweb(bmat) #the base function for bipartite network plot
Any help is highly appreciated
Here's an igraph-pure solution:
library(igraph)
anemonefish <- matrix(# http://www.ecologia.ib.usp.br/iwdb/data/anemone_fish/anemonefish.txt
c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 0, 0, 0,
1, 1, 1, 1, 1, 0, 1, 1, 0, 0,
0, 0, 1, 1, 1, 0, 1, 0, 0, 0,
1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
1, 1, 0, 1, 1, 0, 0, 0, 0, 0,
1, 0, 1, 0, 0, 1, 0, 1, 0, 0,
0, 1, 0, 1, 1, 0, 0, 0, 0, 0,
1, 1, 1, 0, 0, 1, 0, 0, 0, 0,
0, 0, 1, 1, 0, 1, 0, 0, 0, 0,
1, 0, 1, 0, 0, 1, 0, 0, 0, 0,
1, 0, 1, 1, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 1, 1, 0, 0,
1, 1, 0, 0, 0, 0, 1, 0, 0, 0,
0, 1, 0, 0, 0, 1, 0, 0, 0, 0,
1, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 0, 0, 0, 0, 0, 0,
1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0),
nrow = 26, byrow = TRUE)
g <- graph_from_incidence_matrix(anemonefish)
plot(g, vertex.label = "", vertex.color = V(g)$type + 1, layout = layout_as_bipartite)
That's if you want to color the nodes by type. If you want some other method of assigning colors, the easiest thing to do is make a node attribute called color:
palette("Tableau 10")
V(g)$color <- sample(1:length(palette()), vcount(g), replace = TRUE)
plot(g, vertex.label = "", layout = layout_as_bipartite)

Computing the difference of the sum of the values of a table daily

I have a table communities that tells me how many times different pages were shared online.
CREATE TABLE communities
(`community_id` int, `shares_copylink` int, `shares_email` int, `shares_facebook` int, `shares_messenger` int, `shares_pinterest` int, `shares_twitter` int, `shares_whatsapp` int)
;
INSERT INTO communities
(`community_id`, `shares_copylink`, `shares_email`, `shares_facebook`, `shares_messenger`, `shares_pinterest`, `shares_twitter`, `shares_whatsapp`)
VALUES
(1, 0, 0, 0, 0, 0, 0, 0),
(2, 2, 2, 196, 0, 0, 0, 0),
(3, 8, 20, 0, 8, 0, 0, 12),
(6, 0, 0, 0, 0, 0, 0, 0),
(7, 0, 0, 32, 0, 0, 0, 0),
(8, 2, 2, 0, 1, 0, 0, 0),
(9, 0, 0, 0, 0, 0, 0, 0),
(12, 0, 0, 1, 1, 0, 0, 1),
(14, 1, 1, 11, 0, 0, 0, 3),
(16, 0, 0, 0, 0, 0, 0, 0),
(59, 0, 0, 0, 0, 0, 0, 0),
(60, 0, 0, 0, 0, 0, 0, 0),
(61, 0, 0, 0, 0, 0, 0, 0),
(62, 2, 3, 28, 0, 0, 0, 9),
(65, 0, 0, 0, 0, 0, 0, 0),
(66, 0, 0, 0, 0, 0, 0, 0),
(67, 0, 0, 0, 0, 0, 0, 0),
(68, 0, 0, 0, 0, 0, 0, 0),
(69, 0, 0, 0, 0, 0, 0, 0),
(70, 0, 0, 0, 0,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, 0, 0, 0),
(143, 0, 0, 0, 0, 0, 0, 0),
(145, 0, 1, 39, 1, 0, 0, 4),
(146, 0, 0, 0, 0, 0, 0, 0),
(147, 0, 0, 0, 0, 0, 0, 0),
(148, 0, 1, 1, 1, 0, 0, 0)
;
I want to start a query that would run every day and would insert in a table how many shares have occures today.
So I started with creating a table with the number of queries up to today:
CREATE TABLE shares (Date date, Shares int);
INSERT INTO
`shares` (`Date`, `Shares`)
SELECT
CURDATE() as date,
SUM(shares_copylink) + SUM(shares_email) + SUM(shares_facebook) + SUM(shares_messenger) + SUM(shares_pinterest) + SUM(shares_twitter) + SUM(shares_whatsapp) AS total
FROM communities
I now need to run a query that does the difference between the sum of all shares* columns of communities (which would be updated) and the value stored in shares. I will run it for every day D+1 and insert the results in shares so it should always be equal to the sum of shares* columns in communities.
I tried:
SELECT CURDATE(), all_shares - all_shares_but_today AS daily_shares FROM
(
(
SUM(shares_copylink) + SUM(shares_email) + SUM(shares_facebook) + SUM(shares_messenger) + SUM(shares_pinterest) + SUM(shares_twitter) + SUM(shares_whatsapp)
) AS all_shares FROM communities,
(
SUM(`Shares`)
) AS all_shares_but_today)
But it returns:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SUM(shares_copylink) + SUM(shares_email) + SUM(shares_facebook) + SUM(shares_mes' at line 4
I also tried something in this Fiddle but it creates a cartesian product.
I'm using MySQL 5.7
Try below, I'm not sure if this is what you need.
SELECT CURDATE(),
(
(
SELECT
(SUM(shares_copylink)
+ SUM(shares_email)
+ SUM(shares_facebook)
+ SUM(shares_messenger)
+ SUM(shares_pinterest)
+ SUM(shares_twitter)
+ SUM(shares_whatsapp)) as sumval
FROM communities
) - SUM(`Shares`)
) AS daily_shares
FROM shares

How can i cast a dart map <String, dynamic> to <String, List<List<int>>>?

I read up some data from shared prefs and jsonDecode it into a variable called shapes. When I inspect the shapes in the debugger it looks like the right type. But when I assign it to the following variable "theShapes" I get errors such as Unhandled Exception: type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, List<List<int>>>'
static var theShapes = <String, List<List<int>>>{
'general': [
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
],
....
};
The code used to try to cast the "shapes" variable to the type of "theShapes" is like this at the moment:
theShapes = shapes
.map((key, value) => MapEntry(key, List.castFrom(value).toList()));
here you go
go(String data){
final decodedData = jsonDecode(data);
if(decodedData is Map){
return decodedData.map<String,List<List<int>>>((key, value) => MapEntry(key as String, (value as List).map<List<int>>((e) => (e as List).map<int>((i) => i as int).toList()).toList()));
}
return null;
}

moore neighbourhood code to find number of neighbour cells with 1s

guys im given a task where i have to find the number of 1s around a cell in a grid.example from the example figure i should get 3 since there are 3 1s around the cell. so i did the code and i got it right but when i used a function to do the same code its giving me errors and i need your help.
def count_neighbours(((1, 0, 0, 1, 0),(0, 1, 0, 0, 0),(0, 0, 1, 0, 1),(1, 0, 0, 0, 0),(0, 0, 1, 0, 0),), 1, 2):
grid=count_neighbours[0]
row=count_neighbours[1]
col=count_neighbours[2]
length=len(grid)
s=0
d=0
for i in range(row-1,row+2):
for j in range(col-1,col+2):
if i>=0 and j>=0:
if i<=length-1 and j<=length-1:
if grid[i][j]==1:
s+=1
if grid[row][col]==1:
d+=1
total = s-d
return total
#error goes like this
def count_neighbours(((1, 0, 0, 1, 0),(0, 1, 0, 0, 0),(0, 0, 1, 0, 1),(1, 0, 0, 0, 0),(0, 0, 1, 0, 0),), 1, 2):
^
SyntaxError: invalid syntax
Please look into the basics of the function definition in python. You should not put the input values in the function definition.
I feel this is what you are looking for.
def count_neighbours(inputgrid):
grid=inputgrid[0]
row=inputgrid[1]
col=inputgrid[2]
length=len(grid)
s=0
d=0
for i in range(row-1,row+2):
for j in range(col-1,col+2):
if i>=0 and j>=0:
if i<=length-1 and j<=length-1:
if grid[i][j]==1:
s+=1
if grid[row][col]==1:
d+=1
total = s-d
print (total)
return total
count_neighbours((((1, 0, 0, 1, 0),(0, 1, 0, 0, 0),(0, 0, 1, 0, 1),(1, 0, 0, 0, 0),(0, 0, 1, 0, 0),), 1, 2))

Counting ocurences in a list in Mathematica (with code, but not fully working)

My problem is this:
I have a list list and I want to iterate through this list and count the number of times each values from 0 to 1 occur in this in a step size step. I have written the following code:
list = {0.2, 0.2, 0.7, 0.8, 0.17};
countingfornormal[list_, step_] := Module[{temp, num, res, i},
temp = Round[list, step];
num = {};
For[i = 0, i <= 1, i += step, {
res = Count[temp, i];
AppendTo[num, {i, res}];
}];
num
]
However, for a step size of 0.01, the output only seems to count the values up to 0.17. It's really frustrating me and I can't seem to figure out why. I have also noted that for a step size of 0.02, the output counts the values up to 0.34.
I round off at the start because originally I was using a larger list with values to around 7 places after the decimal point.
I thought maybe it was a timing issue but I'm unsure how to fix such a thing.
Any help would be greatly appreciated.
Perhaps better:
list = {0.2, 0.2, 0.7, 0.8, 0.17};
b = BinCounts[list, {0, 1, 0.01}]
(*
->{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0}
*)
BarChart#b