Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 68 69 [70] 71 72 ... 91

Author Topic: Programming Help Thread (For Dummies)  (Read 100553 times)

professorlamp

  • Bay Watcher
    • View Profile
    • joereynoldsaudio
Re: Programming Help Thread (For Dummies)
« Reply #1035 on: February 27, 2013, 07:02:28 pm »

I'll take your word for it and start on python, c++ isn't too confusing yet but by the look of that python code I think I'd prefer it. Cheers for the tip :D

Thankfully I'm not too far into c++ to restart my learning

edit: There are two versions of python (2.7 and 3.0) which should I go for?
2nd edit: I've gone for 2.7, judging from other posts, it seems like the way to go
« Last Edit: February 27, 2013, 07:12:53 pm by professorlamp »
Logged
I write music for video games!
www.joereynoldsaudio.com

kytuzian

  • Bay Watcher
    • View Profile
    • Kytuzian - Youtube
Re: Programming Help Thread (For Dummies)
« Reply #1036 on: February 27, 2013, 09:21:55 pm »

I'll take your word for it and start on python, c++ isn't too confusing yet but by the look of that python code I think I'd prefer it. Cheers for the tip :D

Thankfully I'm not too far into c++ to restart my learning

edit: There are two versions of python (2.7 and 3.0) which should I go for?
2nd edit: I've gone for 2.7, judging from other posts, it seems like the way to go

Actually, I would always advise going for the most recent stable version. It will generally be the best. And there aren't that many differences, except for
Code: [Select]
print "Hello World!"
in 2.7 versus
Code: [Select]
print ("Hello World!")
in 3.2.

JanusTwoface

  • Bay Watcher
  • murbleblarg
    • View Profile
    • jverkamp.com
Re: Programming Help Thread (For Dummies)
« Reply #1037 on: February 27, 2013, 09:53:26 pm »

About the only reason to go for 2.7 is that there are some libraries / system default versions that haven't been updated to 3.x. It's not something that you're likely to run into for a while, but something to keep in mind. It is getting better.

And there are a few minor gotchas that you'll have to keep in mind if you are working in one and reading tutorials in the other. The biggest is honestly what kytuzian said, where print requires parenthesis now*. The rest you'll come across later, but only if you dig a bit deeper into the guts of the language.

* If you're curious, it's because it's a function instead of a statement now. Honestly, it makes more sense. It's annoying to have to retrain your fingers to actually type the parenthesis though.
Logged
You may think I'm crazy / And I think you may be right
But life is ever so much more fun / If you are the crazy one

My blog: Photography, Programming, Writing
Novels: A Sea of Stars, Confession

professorlamp

  • Bay Watcher
    • View Profile
    • joereynoldsaudio
Re: Programming Help Thread (For Dummies)
« Reply #1038 on: March 07, 2013, 01:04:17 pm »

Hello again :D

I've started working on a small project to improve my programming, the programme is a sort of miniature game. The dog gets hungry and then eats something to replenish its hunger,that's what's supposed to happen. At the moment I've managed to load up a separate window with a background and a capital 'D' representing the dog. The next problem I'm facing is refreshing or flipping (?) the screen to load more text onto the screen. Ideally I'd like it to display a message at the bottom of the screen but am unsure of where to go for this.

I have a feeling its the placement of the commands and their indentation in the 'while' loop. At the moment I'm trying to output the message that should display when hunger reaches 150 or lower. The programme itself works in IDLE, just not in the separate window.

Anyway, here is the code...

Code: [Select]

import time
import random
import pygame
pygame.init ()

#colours
red    = ( 240,65 ,138 )
white  = ( 255,255,255 )
grey   = ( 163,139,149 )
black  = ( 0  ,0  ,0   )

#randoms
animals = [ 'rat', 'cat', 'mole', 'mouse', 'hare', 'badger', 'human!']
name    = [ 'Holly', 'Charlie', 'Jack', 'Buela','Daisy' ]
petage  =  (random.randint (1,6))
likes   = [ 'playing', 'hunting', 'chasing', 'running', 'eating' ]

#Window
size =[700,600]
screen = pygame.display.set_mode(size)
pygame.display.set_caption ("Dog Hunt!")

done = False
clock = pygame.time.Clock()

while done == False:
  clock.tick (10)
  for event in pygame.event.get():
   if event.type == pygame.QUIT:
    done = True
 
  screen.fill (red)

  font = pygame.font.Font (None, 25)
  text = font.render ("D", True, black)
  screen.blit (text, [250,250])

  pygame.display.flip()

#hunger countdown timer
  hunger = 200
  while hunger == hunger:
   hunger = hunger - 1
   time.sleep (.2)
   print (hunger)
   if hunger ==150:
    print ("Your creature is hungry and has began hunting for a" ,animals [random.randint (0,6)])
    font = pygame.font.Font (None,25)
    text = font.render ("Your creature is hungry and has began hunting for a",True,black)
    screen.blit (text, [250,250])
    pygame.display.flip()
    #hunt for an animal: animals [ random.randint (0,4)]
    if hunger == 0:
     font = pygame.font.Font (None,25)
     text= font.render ("Your dog has died from starvation",True,black)
     screen.blit (text, [250,250])
     pygame.display.flip()
     #remove dog sprite, game over.

pygame.quit()

'''

#***Dog generation***

print (" D << This is your dog, it's name is" ,name [random.randint (0,4)],"it is",petage,"and  likes" ,likes [random.randint (0,4)])

#sleeping countdown timer
fatigue = 240
while fatigue == fatigue:
 fatigue = fatigue -1
 time.sleep (.2)
 print (fatigue)
 if fatigue < 90:
  print ("Your creature is tired, let him rest")
 if fatigue == 0:
  print ("Your creature has died from lack of sleep")

rat     = 30 #hunger, use only this for now
cat     = 90 #hunger
owl     = 150#hunger
mouse   = 20 #hunger
hare    = 60 #hunger
***Values***
hunger  = 200
'''
 
Logged
I write music for video games!
www.joereynoldsaudio.com

JanusTwoface

  • Bay Watcher
  • murbleblarg
    • View Profile
    • jverkamp.com
Re: Programming Help Thread (For Dummies)
« Reply #1039 on: March 07, 2013, 01:32:18 pm »

One oddity that I'm seeing right now:
Code: [Select]
while hunger == hunger:
This will always be true and thus should be an infinite loop. What are you trying to do with this line?

I also think that copy/pasting your code might have broken the indentation (which is kind of important for Python code :))). The biggest part is that I'm not sure exactly how the while/if/if from lines 41-58 are supposed to nest.

Could you upload it to something like Pastebin? Just click, paste your code, set the language to Python, and share the link it gives you.
Logged
You may think I'm crazy / And I think you may be right
But life is ever so much more fun / If you are the crazy one

My blog: Photography, Programming, Writing
Novels: A Sea of Stars, Confession

professorlamp

  • Bay Watcher
    • View Profile
    • joereynoldsaudio
Re: Programming Help Thread (For Dummies)
« Reply #1040 on: March 07, 2013, 02:23:06 pm »

Hmmm... you're right about hunger, I didn't think about that always being true. It should be above 151 and over that amount he is fine, and when he reaches 150 he begins hunting, if he can't catch prey by the time hunger reaches 0, he is dead.

Here is the link from pastebin

http://pastebin.com/m9ZETgwf

About the lines; I assumed that those statements meant that -
" if hunger gets to 150 print the message ("message!") and also display the ("message!") on the game screen
likewise with the if statement, after that, I thought it would just output the message when it meets that condition. To be honest I copied and pasted those parts just to test to see if it works, I'm still absorbing a lot of python so I'm a bit iffy all round.
« Last Edit: March 07, 2013, 02:29:41 pm by professorlamp »
Logged
I write music for video games!
www.joereynoldsaudio.com

JanusTwoface

  • Bay Watcher
  • murbleblarg
    • View Profile
    • jverkamp.com
Re: Programming Help Thread (For Dummies)
« Reply #1041 on: March 07, 2013, 03:07:40 pm »

Hmm. Well, the formatting is the same in the Pastebin. So I'm guessing it's you doing strange things rather than the copy/paste. :)

The thing to remember about Python is that indentation is used to define blocks. So if you have any sort of conditional or looping construct (for/while/if), anything that is indented within that is going to only run if the loop runs / the conditional holds true.

That being said, I moved around your code a bit and added some comments:
http://pastebin.com/8GKsnXTi

It doesn't work quite as I think you're intending yet, but it's a bit closer. If you wait for long enough ((200 - 150) * 0.2 = 10 seconds), it will print the first message. Right now it chooses a different animal to hunt each tick after that (because the print statement has the random in it), but that's fixable. Take a look at the changes I made and see what makes sense / what doesn't.
Logged
You may think I'm crazy / And I think you may be right
But life is ever so much more fun / If you are the crazy one

My blog: Photography, Programming, Writing
Novels: A Sea of Stars, Confession

professorlamp

  • Bay Watcher
    • View Profile
    • joereynoldsaudio
Re: Programming Help Thread (For Dummies)
« Reply #1042 on: March 07, 2013, 03:26:02 pm »

Hey Janus

thank you soooooo much for helping. The majority of it makes sense to me, it's just that sometimes I will just copy the code with no real understanding of what it does, which really doesn't help myself. Anyway, I found the main problem was the    pygame.display.flip()      command, I didn't realise that it matters where you put the command and that it should be done AFTER draw commands, I moved it around and it works (although the screen does still hang). I've looked at yours and think I'll study that for a bit and hopefully learn a thing or two.

Also I'm not very confident with the indentation yet but I'm getting there slowly :D
Logged
I write music for video games!
www.joereynoldsaudio.com

professorlamp

  • Bay Watcher
    • View Profile
    • joereynoldsaudio
Re: Programming Help Thread (For Dummies)
« Reply #1043 on: March 08, 2013, 01:36:03 pm »

Carrying on with Python, I've noticed a few external programs necessary to really get the best out of Python. Tkinter and Pygame seem to be among the most popular, are there any others that I should be aware of/download? And are there ways of doing what is possible in pygame/tkinter in Python alone?



Sorry about all the questions, if you couldn't tell I'm fairly new to coding  ;D
Logged
I write music for video games!
www.joereynoldsaudio.com

JanusTwoface

  • Bay Watcher
  • murbleblarg
    • View Profile
    • jverkamp.com
Re: Programming Help Thread (For Dummies)
« Reply #1044 on: March 08, 2013, 02:19:36 pm »

Anyway, I found the main problem was the    pygame.display.flip()      command, I didn't realise that it matters where you put the command and that it should be done AFTER draw commands, I moved it around and it works (although the screen does still hang). I've looked at yours and think I'll study that for a bit and hopefully learn a thing or two.
It's a bit non-intuitive, but it's actually a really good thing that they're doing there in the long run. By default, Pygame will double buffer all drawing for you, meaning that all draw screens are to a second 'off screen' version of your game. What flip does is exchanges the onscreen one and the offscreen one. The advantage is that even if you have *a lot* of drawing commands, you still won't see the in between steps where the image is only partially drawn.

Carrying on with Python, I've noticed a few external programs necessary to really get the best out of Python. Tkinter and Pygame seem to be among the most popular, are there any others that I should be aware of/download? And are there ways of doing what is possible in pygame/tkinter in Python alone?
Just about every language has external libraries necessary to make games. Mostly because games require a lot of features (mostly graphics, but also sound/keyboard input/etc) that aren't necessary for non-games. It's a good experience to have because it shows how to install packages in the language, but I agree that it can be annoying ( Pygame is particularly bad, it took me about 15 minutes to get it set up on my machine to debug your code earlier :) ).

You could try using the curses library. That's built in for Python 2.7+ (I think) and will let you build ASCII games. It's a different programming style and the API isn't necessarily the easiest to get used to, but there are a few good curses tutorials out there. Since what you were already working is basically a curses game anyways, it might not be too big a switch.

Sorry about all the questions, if you couldn't tell I'm fairly new to coding  ;D
No worries from me! I teach CS IRL (and quite often the 101 course), so I'm more to used to question. It's always refreshing to see someone independently choose to learn to code.

About all I'd worry is if people get annoyed at the thread filling up with this one subtopic, but if that's a problem, I'm sure someone will speak up.
Logged
You may think I'm crazy / And I think you may be right
But life is ever so much more fun / If you are the crazy one

My blog: Photography, Programming, Writing
Novels: A Sea of Stars, Confession

Carrion

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1045 on: March 08, 2013, 07:24:27 pm »

Hey Janus

thank you soooooo much for helping. The majority of it makes sense to me, it's just that sometimes I will just copy the code with no real understanding of what it does, which really doesn't help myself. Anyway, I found the main problem was the    pygame.display.flip()      command, I didn't realise that it matters where you put the command and that it should be done AFTER draw commands, I moved it around and it works (although the screen does still hang). I've looked at yours and think I'll study that for a bit and hopefully learn a thing or two.

Also I'm not very confident with the indentation yet but I'm getting there slowly :D

One thing I noticed--which is really a subjective matter--I think it would help anyone who wants to read your code if you used four spaces to a line instead of what it looks like you're using one.  It looks like that's part of what Janus did to your code to make it more understandable.  There are lots of arguments about tabs vs. spaces, but the python dev docs favor the four spaces formatting.  As it says, don't mix tabs and spaces in your code.  Check the documentation here if you're curious, particularly the code lay-out section.

All that said, I'm just learning python myself, so take that advice for what it's worth.
Logged

professorlamp

  • Bay Watcher
    • View Profile
    • joereynoldsaudio
Re: Programming Help Thread (For Dummies)
« Reply #1046 on: March 08, 2013, 08:31:40 pm »

Yer, I read that in my python programming book aswell, they recommended either 2 or 4 spaces or to use tab.
Next question (:D)

I'm trying to design a menu system within the game, so I'd probably be using TKinter for the menu's and then pygame for the game/sprites etc...
Is it possible to use both at the same time without an awkward workaround?
Logged
I write music for video games!
www.joereynoldsaudio.com

lorb

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1047 on: March 09, 2013, 03:12:58 pm »

Is it possible to use both at the same time without an awkward workaround?

As a long time programmer I tell you it's not worth the trouble if your goal is writing a piece of software. Pick one and use it for everything. If your primary goal is learning new and at times complicated stuff .. go ahead and use both at the same time :)
Logged
Please be gracious in judging my english. (I am not a native speaker/writer.)
"This tile is supported by that wall."

professorlamp

  • Bay Watcher
    • View Profile
    • joereynoldsaudio
Re: Programming Help Thread (For Dummies)
« Reply #1048 on: March 11, 2013, 04:56:45 pm »

Okay and again...

I've copied a lot of code and then modified values and things within that code to get used to it. One thing that I'm not entirely sure of is the use of parentheses. Sometimes these are crucial but there's quite a lot of instances where if I remove them, the program behaves exactly the same as it did before.

The only real differentiation I know of concerning these parentheses is between tuples and lists, so what other important things are there to know about parentheses  and when to use them?
Logged
I write music for video games!
www.joereynoldsaudio.com

JanusTwoface

  • Bay Watcher
  • murbleblarg
    • View Profile
    • jverkamp.com
Re: Programming Help Thread (For Dummies)
« Reply #1049 on: March 11, 2013, 10:14:31 pm »

Hmm. That's actually a bit of an odd question. Never really thought much about it.

So far as I can think, you need parenthesis to do the following things:
- function calls: f(1, 2); range(10); print('Hello world!')
- controlling precedence: (1 + 2) * (3 + 4)
- creating tuples: (1, 2, 3)

Technically, you only need the comma to create tuples, but it's good practice to use the parenthesis as well. So far as the difference between tuples and lists, in most instances they're the same thing. The big difference is that lists are mutable and tuples are not--you can change the elements in a list or add/remove elements, but you can't do the same with tuples.

Hopefully that helps.

Do you have any examples you're confused about it in particular?
Logged
You may think I'm crazy / And I think you may be right
But life is ever so much more fun / If you are the crazy one

My blog: Photography, Programming, Writing
Novels: A Sea of Stars, Confession
Pages: 1 ... 68 69 [70] 71 72 ... 91