AuthorTopic: making video games, where do you start?, how do you stay focus?  (Read 24080 times)

Offline Emtch

  • 0010
  • *
  • Posts: 157
  • Karma: +0/-0
    • View Profile

Re: making video games, where do you start?, how do you stay focus?

Reply #60 on: September 12, 2008, 06:58:06 am
Now I've gotta reinstall ruby, because something is wrong with sdl (can't load it anymore) and the help files don't work. Really weird because I downloaded it from the official site.
As for tips on ruby, there are plenty of sites; just like this one:
http://www.rubytips.org/
Found like this:
http://www.google.com/search?q=ruby+programming+tips

OMG WHAT IS GOOGLE? I know how to google, thank you.
« Last Edit: September 12, 2008, 07:00:19 am by Emtch »

Offline Atnas

  • Moderator
  • 0100
  • *
  • Posts: 1071
  • Karma: +2/-0
  • very daijōbs
    • paintbread
    • paintbread
    • View Profile

Re: making video games, where do you start?, how do you stay focus?

Reply #61 on: September 12, 2008, 07:20:44 pm
Hey Boeran! Thanks to you, I was able to make a minimally functional linear text adventure for my latin class over the period of a few hours! (loops gave me trouble)
Code: [Select]
#----Define Variables--------
health = 5
maxhealth = 5
strength = 1
#Start
while health > 0
puts "Latin text adventure (press enter to continue)\n"
gets
puts "You climb into the dungeon.\n"
gets
#1
print 'It is dark. Light your torch? (1=yes 2=noo) >'
ask = gets.to_i
#1,1
if ask == 1
print "\nYou can see the passage continues, but you can not see the end.\n"
gets
break
#1,2
elsif ask == 2
puts "you were eaten by a grue"
gets
end
end
#End of first phase, Start of Second
while health > 0
print 'Will you go forward? (1=y 2=n) >'
ask = gets.to_i
if ask == 1
puts "\nYou find yourself face to face with a sphinx.\n\nHe will eat you if you do not answer him correctly.\n"
gets
puts "rawr rawr I am a sphinx what is the object of the preposition in this sentence?\n"
gets
print "Titi ex Roma\n\n"
gets
print '"Is it (1)Titi or (2), Roma?" >'
ask = gets.to_i
if ask == 1
print "You attack the sphinx for #{strength} damage."
gets
print 'He mews miserably as his entrails leak from his shuddering carcass.'
gets
print 'You gained a level! your strength increases by 1!'
strength = strength + 1
puts "\n\n Current Strength: #{strength}"
gets
elsif ask == 2
puts "no you idiot"
gets
puts "Sphinx attacks you for 1 damage, knocking you back in the process!\n"
puts "You land with a thud!!!"
gets
health = health - 1
puts "Health #{health}/#{maxhealth}"
gets
end
end
#end of second phase, start of third
while health > 0
puts "The passage continues. You reach another sphinx\n\n"
gets
print "What are the genetive case feminine endings?\n\n"
puts "1) ae, arum\n"
puts "2) am, as\n\n"
ask = gets.to_i
if ask == 1
print "You attack the sphinx for #{strength} damage."
gets
print 'He mews miserably as his entrails leak from his shuddering carcass.'
gets
print 'You gained a level! your strength increases by 1!'
strength = strength + 1
puts "\n\n Current Strength: #{strength}"
gets
break
elsif ask == 2
puts "no you idiot"
gets
puts "Sphinx attacks you for 2 damage!\n"
gets
puts "You are in pain!!!"
gets
health = health - 2
puts "Health #{health}/#{maxhealth}"
gets
end
end
#end of third start of fourth
while health > 0
puts "The passage continues. You come across yet another sphinx\n\n"
gets
print "What makes a verb passive voice?\n\n"
puts "1) When the subject performs the action of the verb."
puts "2) when the subject recieves the action of the verb\n\n"
ask = gets.to_i
if ask == 1
puts "no you idiot"
gets
puts "Sphinx attacks you for 2 damage!\n"
gets
puts "You are in pain!!!"
gets
health = health - 3
puts "Health #{health}/#{maxhealth}"
gets
elsif ask == 2
print "You attack the sphinx for #{strength} damage."
gets
print 'He mews miserably as his entrails leak from his shuddering carcass.'
gets
print 'You gained a level! your strength increases by 1!'
strength = strength + 1
puts "\n\n Current Strength: #{strength}"
gets
break
end
end
while health > 0
puts "You won yay."
gets
Process.exit
end
end

I think I need else statements because it screws up when you don't hit one or two on a question.

And compiled: www.lolipopsicle.com/latin/latin.exe

Offline Beoran

  • 0010
  • *
  • Posts: 112
  • Karma: +1/-0
    • View Profile

Re: making video games, where do you start?, how do you stay focus?

Reply #62 on: September 12, 2008, 08:34:12 pm
Atnas, I think you could also clean it up considerably by using several procedures/functions. But today I'm too lazy to fix up yours, so sorry for that. :p
Kind Regards, Beoran.

Offline Beoran

  • 0010
  • *
  • Posts: 112
  • Karma: +1/-0
    • View Profile

Re: making video games, where do you start?, how do you stay focus?

Reply #63 on: September 14, 2008, 08:28:21 am
Well, finally, here it is, the next installment with the beginning of Classes.

Code: [Select]
require 'rubygems'
require 'sdl'
# Load the libraries we need.

# We make a new type of object, or a class, that will be used to
# group all information about the player together
class Player
  attr_accessor :x # the player has an x position
  attr_accessor :y # the player has a y position
  attr_accessor :w # the player has a width
  attr_accessor :h # the player has a height 
  attr_accessor :vx # the player has a horizontal speed
  attr_accessor :vy # the player has a vertical speed
  attr_accessor :color # the player has a color
end

# We make a new type of object, or a class, that will be used to
# group all information about the game together
class Game
  attr_accessor :screen # the game has a screen
  attr_accessor :done   # The game is done or not done.
  attr_accessor :color  # the game has a background color 
end


# We make a new type of object, or a class, that will be used to
# group all information about the grass together
class Grass
  attr_accessor :x # the grass has an x position
  attr_accessor :y # the grass has a y position
  attr_accessor :w # the grass has a width
  attr_accessor :h # the grass has a height
  attr_accessor :color # the grass has a color
end

# Define a function to handle the input
def handle_input(input, player, game)
  intype= input.class
  # Intype now contains what class, that is what kind of input we got
  if intype == SDL::Event::Quit
    # Clicked the close window icon.
    game.done = true
    # Now, it's over.
  elsif intype == SDL::Event::KeyDown
    # Someone pressed a key.
    key = input.sym
    # We get 'te "sum", that is the key sumbol for this input
    if key == SDL::Key::ESCAPE
      # If someone presses the escape key, we're also done.
      game.done = true
    elsif key == SDL::Key::LEFT
      player.vx = -1
      # Pressing left arrow key, so the player should move to the left
      # For that, we make the speed of the player negative
    elsif key == SDL::Key::RIGHT
      player.vx = 1
      # Pressing right arrow key, so the player should move to the right
      # For that, we make the speed of the player positive
    end 
  elsif intype == SDL::Event::KeyUp
    # A key was released
    key = input.sym
    # Get what key was released
    if key == SDL::Key::LEFT
      # If the left key was released, the player should stop moving.
      player.vx = 0
    elsif key == SDL::Key::RIGHT
      # If the right key was released, the player should also stop moving.
      player.vx = 0
    end
  end 
  # Here, we don't need a result. 
end 


# A function to calculate the new player position
def update_player(player)
  player.x = player.x + player.vx
  # Set the new position of the player based upon the
  # speed of the player
end

# A procedure to draw the screen
def draw_screen(game, player, grass)
  screen = game.screen
  # Store the game screen temporarily in a variable for ease of use. 
  screen.fill_rect(0, 0, screen.w, screen.h, game.color)
  # Draw the sky on the background.
  screen.fill_rect(grass.x, grass.y, grass.w, grass.h, grass.color)
  # Draw the grassy floor
  screen.fill_rect(player.x, player.y, player.w, player.h, player.color)
  # Draw the player as a square
  screen.flip
  # And display the screen.
end


game    = Game.new
player  = Player.new
grass   = Grass.new

game.screen = SDL::Screen.open(640, 480, 0, 0)
# Open the screen, like usually, and store it in the game.
game.done   = false
# The game is not done.
game.color  = game.screen.map_rgb(0, 255, 255)
# Set the game's background color.

grass.h     = 40
grass.w     = game.screen.w
grass.x     = 0
grass.y     = game.screen.h - grass.h
grass.color = game.screen.map_rgb(0  ,255,0)
# Set the grass's attributes.

player.w      = 40
player.h      = 40
player.vx     = 0
player.x      = game.screen.w  / 2
player.y      = game.screen.h - player.h - grass.h
player.color  = game.screen.map_rgb(255,255,0)
# Set up the player's attributes.

while not game.done
# Keep doing the next lines while done is not true
  input          = SDL::Event.poll
  # We ask SDL to get the input event that is occuring
  handle_input(input, player, game)
  # Process the input in our own function
  update_player(player)
  # Update the player's position based upon the player's speed.
  draw_screen(game, player, grass)
  # Display the screen.
end
# End of the while "loop". Everything from while to this end
# will be reprated until the variable done becomes true

What is new about this? Well, remember that in the previous tutorial we had a lot of parameters that were passed from one function to another. To help with that, we make use of objects and classes to group all related data together. What's an object? Well, its a piece of data that contains different elements grouped together, and which you can access by name.  What is a class? Well, you can understand a class as being like a cookie cutter, or a template, which you can use to determine what the layout of an object created from that class will be.

So, that's why we first describe the class Player as having certain attributes. Attributes of a class or object are properties of that object in which you can store data. Basically, in ruby, there are two kinds of attributes, namely accessors, and readers. With an accessor, you can look up the data inside an object, and also change it. In Ruby, you define an acessor using attr_accessor.  With a reader, you can only look up the data and not change it. In Ruby, toy define a reader using attr_reader. So, by saying attr_accessor :color inside the Player class, we say that a player will have a color that can be changed at will.

Now, to create an object from a class, we simply use the new method of that class. Like that, a class is also acting a bit like a special kind of module. By saying player = Player.new, we create a new object that has all the attributes that we described in the class Player. Thats' why we can do things like player.vx = 0 , we are setting the x direction speed attribute of the player to zero.  Like that, th player object contains all data about the player grouped together neatly. This is a big advantage over using loose variables, since in any big program, you'll have many different variables, and it becomes quite messy when you have to keep track of them individually. With classes and objects, you can group related variables together in a neat package, keeping things more structured and  neat.
Kind Regards, Beoran.

Offline Beoran

  • 0010
  • *
  • Posts: 112
  • Karma: +1/-0
    • View Profile

Re: making video games, where do you start?, how do you stay focus?

Reply #64 on: September 14, 2008, 11:19:28 am
Hey Atnas, here is an improved version of your program, it uses objects and functions. I hope it's not too difficult for you , but if it is, please tell me and I'll explain whatever part you don't understand. As you should understand, now, you can add easily more phases to the game.

Code: [Select]
#
# Improved version Latin game, using classes and object too. 
#

class Player
  attr_accessor :health
  attr_accessor :maxhealth
  attr_accessor :level
  attr_accessor :strength
end 

class Phase
  attr_accessor :text
  attr_accessor :question
  attr_accessor :first
  attr_accessor :second
  attr_accessor :correct
  attr_accessor :correcttext
  attr_accessor :levelup
  attr_accessor :damage
  attr_accessor :damagetext
end

def choice(question,first, second) 
  puts question
  # show question and ask one time.
  puts first + " (1), " + second + " (2)." 
  line    = gets
  answer  = line.to_i
  # keep asking if it's not 1 or 2
  while answer !=  1 and answer != 2
    puts "Please type 1 or 2 only, followed by an enter."
    puts first + " (1), " + second + " (2)." 
    line    = gets
    answer  = line.to_i
  end
  return answer
end

# Damage the player if he gives a wrong answer
def player_damage(player, damage, message) 
  puts message
  player.health -= damage
  if damage > 0
    puts "You suffered #{damage} points of damage."
    puts "Curent health: #{player.health} / #{player.maxhealth} ."
  end
  if player .health < 0
    puts "Oh my, looks like you didn't survive that one!"
    return false
  end
    return true
end

# Level up the player if he gives a right answer
def player_levelup(player)
  puts 'You gained a level! Your strength increases by 1!'
  player.strength += 1
  puts "Current Strength: #{player.strength}"
end

# Play one phase
def phase_play(phase, player)
  while (player.health > 0)
    puts phase.text
    answer    =   choice(phase.question, phase.first, phase.second)
    if answer ==  phase.correct   
      puts phase.correcttext     
      # Show text for correct answer
      # Correct answer, level up player if wwe have to
      if phase.levelup 
        player_levelup(player)
      end 
      return true
    end
    player_damage(player, phase.damage, phase.damagetext)
  end 
  return false
end

# Plays all phases
def phase_playall(player, phases)
  for phase in phases do
    alive = phase_play(phase, player)
    if not alive
      return false
    end
  end   
  return true
end

# Initialize player object
player            = Player.new
player.health     = 5
player.maxhealth  = 5
player.strength   = 1
# Initialize game object


phase1             = Phase.new
phase1.text        = "You climb into the dungeon."
phase1.question    = "It is dark. Light your torch?"
phase1.first       = 'Yes'
phase1.second      = 'No'
phase1.correct     = 1
phase1.correcttext = "You can see the passage continues, but you cannot see the end."
phase1.damagetext  = "You are attacked by hungy a grue!"
phase1.damage      = 5
phase1.levelup     = false


phase2             = Phase.new
phase2.text        = ""
phase2.question    = "Will you go forward?"
phase2.first       = 'Yes'
phase2.second      = 'No'
phase2.correct     = 1
phase2.correcttext = "You find yourself face to face with a sphinx.\nHe will eat you if you do not answer him correctly."
phase2.damagetext  = "What, you want to keep standing here forever?"
phase2.damage      = 0
phase2.levelup     = false

phase3             = Phase.new
phase3.text        = "Rawr, rawr! I am a sphinx!"
phase3.question    = "What is the object of the preposition in this sentence?\nTiti ex Roma.\n"
phase3.first       = 'Titi'
phase3.second      = 'Roma'
phase3.correct     = 1
phase3.correcttext = "You attack the sphinx! \n He mews miserably as his entrails leak from his shuddering carcass."
phase3.damagetext  = "No, you idiot! Sphinx attacks you, knocking you back in the process!\nYou land with a thud!!!"
phase3.damage      = 2
phase3.levelup     = true



phase4             = Phase.new
phase4.text        = "The passage continues. You reach another sphinx.\n"
phase4.question    = "What are the genetive case feminine endings?\n"
phase4.first       = "am, as"
phase4.second      = "ae, arum"
phase4.correct     = 2
phase4.correcttext = "You attack the sphinx! \n He mews miserably as his entrails leak from his shuddering carcass."
phase4.damagetext  = "No, you idiot! Sphinx attacks you, knocking you back in the process!\nYou land with a thud!!!"
phase4.damage      = 2
phase4.levelup     = true


puts "Latin text adventure (press enter to continue)\n"
gets

phases = [ phase1, phase2, phase3, phase4 ]

won = phase_playall(player, phases)
if won
  puts "Congratulations, you won the game!"
else
  puts "Game Over!"
end
Kind Regards, Beoran.

Offline Atnas

  • Moderator
  • 0100
  • *
  • Posts: 1071
  • Karma: +2/-0
  • very daijōbs
    • paintbread
    • paintbread
    • View Profile

Re: making video games, where do you start?, how do you stay focus?

Reply #65 on: September 14, 2008, 12:41:18 pm
Woah, thanks man. I'm going to study this.

Offline Froli

  • 0010
  • *
  • Posts: 293
  • Karma: +1/-0
  • Dragon Highlord
    • View Profile

Re: making video games, where do you start?, how do you stay focus?

Reply #66 on: September 14, 2008, 01:09:28 pm
Sorry for the off topic but, is there anyone here who is both a programmer and a good pixel/graphic artist? Having both skills at your whim is pretty awesome.

Offline PypeBros

  • 0100
  • ***
  • Posts: 1220
  • Karma: +2/-0
  • Pixel Padawan
    • PypeBros
    • View Profile
    • Bilou Homebrew's Blog.

Re: making video games, where do you start?, how do you stay focus?

Reply #67 on: September 15, 2008, 11:30:58 am
Sorry for the off topic but, is there anyone here who is both a programmer and a good pixel/graphic artist? Having both skills at your whim is pretty awesome.
i do both. I let you judge whether i'm a good pixel artist.
And well, somehow, it may be awesome, but it means you've got heavier work on any project you're stepping in :P