Feeds:
Posts
Comments

Archive for the ‘Programming’ Category

  • Single quotes and double quotes are the same
  • How to check if strings are anagrams of each other (I’m really proud of the way I did this)
  • Non-recursive factorials. I cheated a tad, but I almost had what was suggested. I fixed it on my own, though.
  • How to convert decimal to binary (not in Python, by hand, but it was in a video series about Python)
  • How to use Wing IDE to view a watch window for Python
  • What <> means, and that it’s no longer supported
Advertisements

Read Full Post »

I like game shows and trivia questions. I like Python and computers. I thought it would be fun to try to merge the two. I found a list of Family Feud questions from the SNES game of the same name on GameFAQs. I copied it out to a file and tried to parse it through Python so it would give me a random question and its answers.

I started in January but just managed to get it working yesterday. I need to redo it into a dictionary or something with key-value pairs (The question string, and a set of answers). There are also some cases where it doesn’t work.

It’s heavily commented because I hate doing something like this and coming back to it later with no idea how it works.

This might seem simple (and it is) but it’s a big step for me because I figured it out of my own volition and some Googling. I usually just search until I find the code I want, but this was different because I thought it through until I figured out the pieces I needed and looked up those pieces.

import random
f = open("C:\\path\\to\\questions.txt" , 'rU')
questions = []
answers = []
ansq = []
#most of this could be changed. I don't need that answers list I don't think
for line in f:
    if ".." not in line: #if it's a question (i.e. without leader lines to the point amount, thus not an answer)
        if line == "\n":
            pass
        else:
            questions.append(line)
    else: #if it's not a question, then it's an answer (which is a wrong assumption I need to fix)
        if line == "\n":
            pass
        else:
            answers.append(line) #so add it to the answers list instead

#if question matches line
#look for the answers below it until the next question
#    while line not in questions
#    print line
            
qtoask = random.choice(questions) #pick a random question from the questions list

f.seek(0) # go back to beginning of file
for line in f:
    if line == qtoask: #rescan it; if the line matches the random question
        line = f.next() #go to the next line
        #ansq.append(line)
        while ".." in line:#and find all the responses to that question (they all have dotted leader lines)
            #This if condition will never be true if it's in this while block. Duh.
            if line == "\n": #if the line is a newline (Enter)
                pass #skip it
            else:
                ansq.append(line) #otherwise, add it to the list of answers to the question
                line = f.next() #go to the next line and do it again

print qtoask
for a in ansq: #Beautify. Just print the list of answers so it's easy to read
    print a + "\n"
f.close()

#TODO: refactor into lists (or dictionaries?)

Things I’ve learned from this so far:

  • You have to manually reset the file (via seek())
  • It’s easier to exclude than include in order to get a specific set of data
  • The next() function
  • I was really happy to find out about random.choice()

Read Full Post »

I’m not a math person, but I’ve been wanting to redo an old project for a little while now. I don’t understand stuff like vectors and deltas. However, I found these two links yesterday and I think they’ll really help me.

If you’re wondering, I want to remake my Pong clone so the ball bounces at angles other than 90 degrees. I’d also like to fix the collision detection (which the second link handles).

Read Full Post »

Our Internet service was down for six or so days. That was the only time this year I’ve been productive.

I started playing around with Blender’s Python scripting and wanted to do something really basic just to see if I could do it. With help, I did. I present to you: Select objects by type.

It just pops up a menu and selects all the objects in the active scene that are of the type you select in the menu. I was working on multiple selection but Blender’s built-in toggle buttons — “Draw.Create(True/False)”  — hate me.

It doesn’t work with 2.5. I (kind of) tried to get it working, but I have to say I feel bad for the people porting stuff over.

It’s noobish. It’s ugly. It’s useless. It’s buggy. It…works at least. 😀

#!BPY
"""
Name: 'Select Objects by Type'
Blender: 249
Group: 'Object'
Tooltip: 'Selects all objects in a scene based on their type (camera, mesh, lamp, etc.) regardless of layer'
"""
#With help from "Find by Data Use" script by Campbell Barton
from Blender import *
import bpy
import BPyMessages

sce = bpy.data.scenes.active
for object in sce.objects:
	if object.sel == 1:
			object.sel = 0
			
			
def objectSelect(type, scene):
	objCount = 0
	for object in scene.objects:
		if object.type == type:
			object.sel = 1
			objCount += 1
	#If there aren't any objects of a particular type in the scene
	if objCount == 0:
		Draw.PupMenu("Error%t|No objects of type in scene")
	else:
		Draw.PupMenu("Selected " + str(objCount) + " object(s)")
	
			
condition = Draw.PupMenu("Which objects to select?%t"
"|Meshes|Lamps|Cameras|Curves|Surfaces|Empties"
"|Armatures|Lattices|Text|Meta|All")

if condition == 1:
	objectSelect('Mesh', sce)
		
elif condition == 2:		
	objectSelect('Lamp', sce)
	
elif condition == 3:		
	objectSelect('Camera', sce)
			
elif condition == 4:
	objectSelect('Curve', sce)
		
elif condition == 5:		
	objectSelect('Surf', sce)
			
elif condition == 6:		
	objectSelect('Empty', sce)
			
elif condition == 7:
	objectSelect('Armature', sce)
	
elif condition == 8:		
	objectSelect('Lattice', sce)
			
elif condition == 9:		
	objectSelect('Text', sce)
	
elif condition == 10:
	objectSelect('MBall', sce)

#Get all objects if we didn't pick a category
elif condition == 11:
	totalSceneObj = 0		
	for object in sce.objects:
			object.sel = 1
			totalSceneObj += 1
	Draw.PupMenu("Selected " + str(totalSceneObj) + " object(s)")

Read Full Post »

After finding out Microsoft had released a free book for learning C++, I took them right up on it and downloaded it as soon as I could. The book is, well… not particularly well-proofread but I’ll give it the benefit of the doubt and say that the print edition is better.

Regardless, something about that book has led me to a new level of understanding.

I have officially programmed my first game on my own: TicTack. And I did most of it in one day. I just sat down yesterday, thought “I’m finally going to finish that damn thing”, and, somehow, here it is.TicTack screenshot

Literally years have gone by that I’ve wanted to create even a simple game on my own (barring research). Tic-tac-toe was one of those things I thought I’d never be able to accomplish. But something about that book unlocked a new door for me. Suddenly it all just fell into place. I’ve had the Visual C++ project sitting there for months, thinking “maybe someday..”

It isn’t the most complex or elegant solution to a tic-tac-toe game, nor does it have any AI, but I’m proud. It does have almost 200 lines (well..including some blank ones). This is the biggest program I’ve ever made.

It also has lots of bugs, specifically concerning user input. I’ve tried to fix some of them by limiting the input but it still will either infinite loop or otherwise mess up if you don’t put in what the program asks for. It’s not graceful at failing (like me).

Although I’m happy with TicTack, I have to mention that it’s pretty much spaghetti code. It has functions but they’re disorganized, and I’ve used magic numbers everywhere. I’m working on cleaning it up but I really think I’m just making it worse.

It’s officially my pet project. I’ll be tinkering with it for a while (and the source here will be updated accordingly).

My next big-huge-fun-OMG-look-I-can-actually-do-it project is probably going to be this with graphics, either in Win32 API (doubtful), or Python and Pygame. I’m stuck between the two because I don’t know Python but Win32 is above my level. Either way I have the logic already pretty much coded though.

But let’s try to conquer some of the rest of that book first. Pointers. Yay. Something I don’t think I’ll ever understand. Then again, haven’t I said that before? 😉

Source code available after the jump:
(more…)

Read Full Post »