CH Student Individual Projects

Hi all,

Whether you are a CHRHS programming student or a forum member looking to help a new learner, here’s what you can expect.

Posting project code here is optional.
Post if you are interested in learning.
Share your code via a GitHub link to minimize clutter.
Read the FAQ page before posting.
Be respectful in your writing and in your expectations of others’ time.

Thanks,
CWS

Also a good idea if you’re a student posting code or asking a question is to say you’re a student. That way we know that you’re just starting to program and taking a class in school. That will help us in our response when we start getting similar questions from different users.

Hello,
Here is some code I have been working on for some time: https://gist.github.com/trikyle/8d3108d9a846f12701b2

I have been having a problem with the game being a little glitchy, specially after playing through the game a couple times. I think part of the problem has to do with the touches not being at the same time (I don’t use multi touch) and it makes it slower and not as smooth. If you find any other ways that my code is slowing itself down as you play longer it would be more than helpful to know where those problems are.

Thank you,
Kyle (CH student)

@kylewood The first thing you should do is remove the print statement in the touched function. That is constantly printing the x,y values as you move the paddles. The more you print, the slower the program runs. You should use print statements when you’re debugging a program or starting to write it, but you should never use print in a completed program unless it’s part of the actual program.

@kylewood, great job

Hello,

This is the latest version of a game that I have been working on for quite a while: https://gist.github.com/bradenfisher18

I am a beginning programmer taking the class here at CHRHS. My intent with this game was to use all of the concepts that I have learned and apply them to create a functional and somewhat fun original game. Right now I am trying to work out a couple bugs, such as how I sometimes lose more than one life at a time if I get hit after just recovering from another hit. Are there any other overall suggestions for ways to improve gameplay?

Thank you

@bfisher I didn’t see the error you were talking about. Maybe I didn’t play it long enough. One complaint I have is too many comments. It makes the code hard to read, at least for me. There’s no need to comment a line that displays text, saying that it displays that text. Same with variables. If a variable name has a good name, there’s no need to add a comment saying what it means and the value it’s getting. Other than that, it’s a good game to play. The asteroids coming at an angle makes them harder to hit.

@bfisher - finding bugs will always be a challenge, so it will pay to get good at doing it. There are some things you can do to make it easier

  1. Test your code after adding just a few lines, rather than adding dozens of lines before testing again

  2. It is easier to test if you break your code into small functions, each of which can be tested separately. It is much easier to find bugs in 10 lines of code than in 100 lines.

  3. Print statements will help you see what is happening

  4. If you get really, really stuck, try simplifying. Comment out chunks of code and run again, until you can find where the bug is happening. If you ask the forum for help, post code, but try to post the simplest possible code that has the problem - you will get a quicker response that way.

@bfisher One way you can make things easier is to simplify your code. You have a lot of code in the draw() and touched() functions. Move all of your game states into seperate functions.

Here’s what your draw() and touched() functions would look like if you moved each game state code into their own functions. It would also make finding bugs easier because you would only be looking in the section of code affected by the bug instead of a lot of code that’s not affected.

function draw()
    background(0)
    sprite(moonsurfaceimg,WIDTH/2,0,WIDTH,75)
    startimer=startimer+DeltaTime
    
    if startimer>=1/starFrequency then 
        LaunchStar()  
        startimer=0 
    end 
    
    for i,s in pairs(S) do 
        s.x=s.x-s.s 
        if s.x+s.w<0 then 
            table.remove(S,i) 
        end 
        sprite(starimg,s.x,s.y,s.w,s.h)
    end  
      
    if gameState==1 then
        drawState1()
    end
    
    if gameState==2 then
        drawState2()
    end
    
    if gameState==3 then
        drawState3()
    end
    
    if gameState==4 then
        drawState4()
    end
            
    if gameState==5 then
        drawState5()
    end

end

function touched(touch)
    if gameState==1 then
        touchState1()
    end
    
    if gameState==2 then
        touchState2()
    end
    
    if gameState==3 then
        touchState3()
    end
    
    if gameState==4 then
        touchState4()
    end
    
    if gameState==5 then
        touchState5()
    end  
  
end

Or, use a pointer. Here, the variable scene points to whatever stage of the game you’re at. All the code for handling splash, game over etc, can be kept in the same tab:

--# Main

function setup()
  scene = Splash()
end

function draw()
  scene:draw()
end

function touched(touch)
  scene:touched(touch)
end

--# Splash
Splash = class()

function Splash:init()
  --set up your splash screen, title of the game, "Start" button etc
end

function Splash:draw()
  --draw the splash screen
end

function Splash:touched(touch)
  --check whether Start button has been pressed
  -- if so, scene = Game()
end

--# Game
Game = class()

function Game:init()
  --set up the game
end

function Game:draw()
  -- update the game
  --check for GameOver event, if so scene = GameOver()
  --draw the game
end

function Game:touched(touch)
  --game touch events
end

--# GameOver
GameOver = class()

function GameOver:init()
  --set up your gameover screen, hiscores, "restart", "quit" button etc
end

function GameOver:draw()
  --draw the GameOver screen
end

function GameOver:touched(touch)
  --check whether restart button has been pressed
  -- if so, scene = Game()
  --check whether quit button pressed
  --if so scene = Splash()
end

Thank you for the help @dave1707 @Ignatz and @yojimbo2000

I made each game state have it’s own function so now I don’t have as much code in my draw and touch functions. I also took out a lot of the comments, which helped make my code easier to read. Doing these allowed me to fix the bugs I was talking about.

Thanks again, and anybody who wants to see the final version can see it here: https://gist.github.com/bradenfisher18

@bfisher Nice job rewriting your code. Its a lot easier looking at it. Great job fixing your own bugs. You’ll find coding is a lot easier and more fun when you don’t have to rely on someone else to help.