I created Tetris in SOLIDWORKS

Yes, I really built Tetris in SOLIDWORKS. I call it CADtris.

I had no idea if it was even possible.

Turns out that it is 🙂

Free download

This is the button you are looking for, right?

The CADtris SOLIDWORKS add-in is free to use, forever. It works with SOLIDWORKS 2018 and newer.

But why?

I am a maker, first and foremost. If I’m creating, I’m happy.

That’s why I build my products, continually tweak my website and write all of my blog posts.

But as soon as I know how to do something, it stops being fun.

So I wanted to try something else. Drew and Lightning are awesome and I still love working on those products, but I wanted to make something more random, more fun. Something that doesn’t have to be perfect.

So, I built my first ever game. And I built it in SOLIDWORKS exactly because it doesn’t make sense.

How I built Tetris in SOLIDWORKS

I do make SOLIDWORKS add-ins for a living, so I had a strong foundation to start with. I stole code from Drew to manipulate blocks and I repurposed code from Lightning to send scores to an online database. But I still had to build a lot from scratch.

The very first step was creating a sketch with a single block, that would step down every second. That alone already felt like magic when it worked. This was three months ago.

Tetris in solidworks - CADtris First falling piece

Then, I added keyboard shortcuts so that you could use the arrow keys. You need a task pane to catch the key presses, so I added that as well:

buttons in taskpane

Now it was time to create the game area. It consists of 10×20 squares, just like the official Tetris. Every time the timer fires or you press a key, the active tetromino should move. If there is no other piece in the way, the move is allowed. If it hits the side, the move is ignored and if the active piece hits the bottom, it becomes part of the stack.  After it becomes parts of the stack, you add a random new shape at a random position and with a random rotation at the top.

Then, you need rotations. When you try to rotate the active piece and it hits a side, it may need a kick to the side to stay inside the game arena. If that position fits, we allow it. Here’s an example of a kick to the right:

Tetris for solidworks - CADtris kick

When the active piece becomes part of the stack and creates a full row, you need to delete the full rows and shift the rest down. To do this, I explode the sketch block into four square blocks. Otherwise, selecting one line in a block and hitting delete would delete the entire sketch block.

solidworks explode block

Now, all we need to add is a scoring system. I based mine loosely on one of the many official scoring systems, where you get points based on the level that you’re in and the number of rows you complete at once.

Make it go faster

Because I use a timer that fires every second or so, a different process than the main SOLIDWORKS process does the API requests. And when you do this, your calls to the API are slower, like 100+ times slower. Where getting the blocks in the current sketch normally takes less than a millisecond, it can now take 200ms or more. So I was in a constant fight with SOLIDWORKS to keep Tetris fast.

Luckily, I have done my research when it comes to making the API faster. You can read the result in this blog post, and I found even more methods when I wrote my ebook Secrets to SOLIDWORKS Performance. Go check it out:

Secrets to SOLIDWORKS Performance

A complete guide to making your models fast by learning what makes them slow.

 

What made it hard

There were at least ten moments where I thought “If I don’t get this to work, I will never be able to finish the game“. Those were the toughest hours.

But when I broke down each problem into tiny, tiny pieces, I was able to work through each and every one of them.

Timing

I already mentioned speed and timing. Tetris goes faster when you reach the next level, and so does CADtris. But there is a limit and it highly depends on your computer. So I can make no promises on how fast it will go. I did make the levels a little slower than the official speeds so that more people can enjoy my game.

When the game goes too fast for your computer, a block might not move to the correct position, or two blocks will be added. If you see this, don’t say I didn’t warn you 😉 I have put a lot of time into making it robust already and I’m currently satisfied by how playable the game is.

To put things into perspective with actual games that run at 60Hz or more: my modern laptop was able to run CADtris at 8 Hertz. Getting all blocks from a sketch was the slowest component.

Never steal the focus

If you ask SOLIDWORKS to do something complex, like creating a new file, the main window will steal the focus of the application and you don’t receive keyboard shortcuts anymore. So I had to find add, delete and move methods that would never steal the focus. Sometimes I lock the user interface to avoid losing the focus. Coincidentally, locking the model also makes API calls faster.

Deleting + moving rows

This took me two days to figure out. Using simple sketch lines was too slow, so I used blocks. But you can’t just box-select around all blocks in a row and delete the selection, this will also delete the blocks below and above. Lasso selections aren’t available. You can’t just delete part of a block, which I why I have to explode them as soon as they land.

Solidworks explode block

Selecting blocks one by one was too slow, so I use MultiSelect. Deleting them would steal the focus, so I lock the model first.

To move a selection, I use MoveOrCopy. I did have to keep an internal list of all block instances to know which blocks existed above the deleted row(s).

Making it look good

In games and movies, looking good is the main objective. It doesn’t matter how complex or fake it is, if it looks good once, you are done. This approach is diametrically opposed to building business software that people rely on, which needs to function in all situations, with all possible inputs.

That is another reason why I wanted to make this game. The goals are just completely different. I did spend way too much time browsing the API docs to find out how to color objects though, so that part of my job still stayed the same 😐

Can you beat the high score?

Every game needs a score and every game that is connected to the internet needs a global high score. So I added both.

When you start your first game, you need to enter a nickname. When your game is over, we send your score to our database. And when your score is really impressive, you can get into the global top 10 and become famous 😀

To see who’s best in your company, we create an eight-letter Company ID and your own list of high scores. Share your Company ID with a colleague to fight for your company’s number one spot.

tetris in solidworks - CADtris - by CAD Booster

Download it here

I am giving away CADtris, because what else could I do? It’s just a fun side project that got out of hand 🙂 If you want to support me, check out the links below.

If you like this, you’ll love…

My drawing automation add-in Drew. Drew stores your drawing preferences and creates a new drawing with views and outer dimensions in one click. Adding a sheet per unique body also takes just one click.

Secrets to SOLIDWORKS Performance: an ebook about everything that makes your models and drawings fast and slow. If you learn just one new thing, your investment will be worth it, and I cover 100+ subjects in this ebook.

My fastener add-in Lightning makes working with fasteners fun again. Never scroll through a long list of filenames ever again, just select a type, size and material and drag the bolt into your assembly. Lightning is a SOLIDWORKS partner product, just like Drew.

Don't miss the next post. Get a free add-in.

Subscribe to our newsletter and get our TimeSavers add-in for free.