Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Beginner having trouble with a tiny shooter game
- glyserine
-
New Scratcher
2 posts
Beginner having trouble with a tiny shooter game
Hi all. I'm new to scratch, just started online cs50 course for fun and I was trying to create a small shooter game where I could press spacebar to shoot a projectile and have some enemies spawn that I could shoot at by using the up-down-left-right arrows.
I have tried to google how to fix my code but it seems that the code becomes very complex with those tips. I've mostly created this what I have by just trying it out with the little intro we had in the course and I found some advice here on how to create a clone of myself and use that as a projectile.
But I can't seem to be able to create a projectile that would actually “destroy” the enemies it touches. I have tried different variations of my code but it seems that maybe the projectile is not properly touching the enemy? I basically tried to hide the other sprite when it touches the projectile but it doesn't work. One option is that the way I spawn enemies is not a good solution.
You can play my game and get 20 points but I'm not sure if it's because the projectile sometimes hits the enemies at a random. I also are unable to restart the counter without doing it by hand after each game.
The graphics and what not are not a finished product, I aim to use my own paint images if I can just get the code to work. Am I being too ambitious for a beginner and should just switch to “point mouse and click to shoot”? It seems that that might be a bit easier to code, we had some examples that I could probably use to create that.
The link to my work is : https://scratch.mit.edu/projects/600888817
If anybody has any tips, please, I'd be eternally grateful. I know the code looks amateurish but it's just what I came up with by trying to use logic and testing.
I have tried to google how to fix my code but it seems that the code becomes very complex with those tips. I've mostly created this what I have by just trying it out with the little intro we had in the course and I found some advice here on how to create a clone of myself and use that as a projectile.
But I can't seem to be able to create a projectile that would actually “destroy” the enemies it touches. I have tried different variations of my code but it seems that maybe the projectile is not properly touching the enemy? I basically tried to hide the other sprite when it touches the projectile but it doesn't work. One option is that the way I spawn enemies is not a good solution.
You can play my game and get 20 points but I'm not sure if it's because the projectile sometimes hits the enemies at a random. I also are unable to restart the counter without doing it by hand after each game.
The graphics and what not are not a finished product, I aim to use my own paint images if I can just get the code to work. Am I being too ambitious for a beginner and should just switch to “point mouse and click to shoot”? It seems that that might be a bit easier to code, we had some examples that I could probably use to create that.
The link to my work is : https://scratch.mit.edu/projects/600888817
If anybody has any tips, please, I'd be eternally grateful. I know the code looks amateurish but it's just what I came up with by trying to use logic and testing.
- chopbot
-
Scratcher
81 posts
Beginner having trouble with a tiny shooter game
for enemy sprite
when I start as a clone
forever
if <touching [sprite 1] ?> then
change [counter] by (1)
delete this clone
end
end
- glyserine
-
New Scratcher
2 posts
Beginner having trouble with a tiny shooter game
for enemy spritewhen I start as a clone
forever
if <touching [sprite 1] ?> then
change [counter] by (1)
delete this clone
end
end
I've tried to this before and tried to integrate it again. But it doesn't seem to have the wanted effect. Maybe because I have the instruction on how the copied enemies need to keep moving? Maybe I need to find a better way to spawn enemies for your suggestion to work.
- deck26
-
Scratcher
1000+ posts
Beginner having trouble with a tiny shooter game
One of your problems is the projectile clones are deleted too quickly when the touch something - add a wait 0 secs before deleting the clone. That gives the other object chance to also detect the touch.
The repeat 10000 is unnecessary in the projectile clone since you have a repeat uintil and a delete this clone so it can't repeat the outer loop. The broadcast also has no receivers so is unnecessary.
You green flag script with if touching grunt only runs immediately after the green flag is clicked - it presumably needs a loop around it.
You need to set counter to 0 before starting game play.
It can be useful to only code for clones and keep the grunt sprite hidden. Tell the clones to show in the clone script rather than showing the sprite and creating the clones and create 20 clones instead of 19. It means if you change the code you're not having to do it twice - clones and sprite.
Your main problem is using glide. What you're doing is gliding for 2 seconds, checking for touching a projectile, glide again, check again and so on. If you're going to use glide you need the touch detection to be in another script.
Finally, remember if you use sprite1 for the projectiles the grunts will also detect touching the player as if it is a projectile. I'd make the projectiles a different cloned sprite.
The repeat 10000 is unnecessary in the projectile clone since you have a repeat uintil and a delete this clone so it can't repeat the outer loop. The broadcast also has no receivers so is unnecessary.
You green flag script with if touching grunt only runs immediately after the green flag is clicked - it presumably needs a loop around it.
You need to set counter to 0 before starting game play.
It can be useful to only code for clones and keep the grunt sprite hidden. Tell the clones to show in the clone script rather than showing the sprite and creating the clones and create 20 clones instead of 19. It means if you change the code you're not having to do it twice - clones and sprite.
Your main problem is using glide. What you're doing is gliding for 2 seconds, checking for touching a projectile, glide again, check again and so on. If you're going to use glide you need the touch detection to be in another script.
Finally, remember if you use sprite1 for the projectiles the grunts will also detect touching the player as if it is a projectile. I'd make the projectiles a different cloned sprite.
- Discussion Forums
- » Help with Scripts
-
» Beginner having trouble with a tiny shooter game