Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 79 80 [81] 82 83 ... 91

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

SethCreiyd

  • Bay Watcher
  • [VESPERTINE]
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1200 on: September 20, 2013, 11:25:27 am »

I've been writing a program in python using tkinter and I can't get it to recognize right mouse clicks.  It recognizes left mouse clicks and middle mouse clicks just fine.  Maybe it's a longshot, but anyone got suggestions what could be causing this?

Can you post the code?  All I could say offhand is that <Button-2> is for the middle button and <Button-3> is for the right.

The event in question starts at line 265, change line 298 if you want to change between right and middle mouse button.  It could also be a problem with my system and will work fine for you, which is feedback I'd appreciate.

Spoiler (click to show/hide)

I'm not sure what you're intending with the nested functions, but I'd guess that you wanted ensure that the local App.self parameter is visible to the event callback?  The way it's currently set up, you're recreating the functions every time App is instantiated, and those names are scoped to App.__init__ itself (functions defined within a function are neither static nor global). And as olemars said, you're rebinding the right mouse event with the movecenter function right after you bind the rightmouse function, so the latter is never called.

I'd suggest doing something about the binding conflict, and turning those event functions into real members of App.  Here's an edited version for comparison, with changes mainly in App.__init__:

Code: [Select]
try:
    from tkinter import *
except ImportError:
    from Tkinter import *

from random import randint

damtable = [ [0,0,0,0,0,5,10,20,30,40,60,100],
            [0,0,0,0,0,15,20,30,40,50,70,100],
            [0,0,0,0,5,25,30,40,50,70,80,100],
            [0,0,0,0,10,30,40,50,60,80,90,100],
            [0,0,0,5,15,35,50,60,70,90,100,100],
            [0,0,5,10,25,40,60,70,90,100,100,100],
            [0,5,10,20,30,50,70,80,100,100,100,100],
            [5,10,15,25,35,60,80,90,100,100,100,100],
            [10,15,20,30,45,70,90,100,100,100,100,100],
            [15,20,20,35,55,80,100,100,100,100,100,100]
          ]

stacklist = []

def ordinal(x):
    if x%10 == 1:
        return str(x) + "st"
    elif x%10 == 2:
        return str(x) + "nd"
    elif x%10 == 3:
        return str(x) + "rd"
    else:
        return str(x) + "th"

class Unit(object):
    def __init__(self, profile, *name):
        self.active = "nothing"
        self.health = 100
        self.armor = profiles[profile][0]
        self.weapons = profiles[profile][1]
        self.target = -1
        if len(name)== 1:
            self.name = name
        else:
            self.name = ordinal(profiles[profile][3])+ " " + profiles[profile][2]
            profiles[profile][3] += 1
    def unitreport(self):
        return str(self.name) + "; Armor:" + str(self.armor) + "; Weapons:" + str(self.weapons) + "; Target: " + str(self.target+1) + "; Health: " + str(self.health)

           
profiles = {
"Guard" : [20, ["Rayguns", "Raypistols"], "Guards", 1], "Rebel" : [20, ["Rayguns", "Raypistols"], "Rebels", 1], "Militia" : [10, ["Squad AA", "Militia Rifles"], "Militia", 1]
}#profile order is armor, weapons, name, and the number 1 (used in the counting function for names)
arms = {
"Rayguns": [10, 10, 1, 1], "Raypistols": [5, 5, 2, 2], "Squad AA": [1, 5, -1, -1], "Militia Rifles": [2, 20, 12, 16]


#hitroll, damroll, distance it starts shooting at, distance it keeps shooting for
}

namelist = []

for name in profiles:
    namelist.append(name)

class Stack(object):
    def __init__(self, x_pos, y_pos, *units):
        self.destroyed = []   
        self.units = []
        self.x_pos = x_pos
        self.y_pos = y_pos
        self.x_gfx = (self.x_pos-1)*mygrid.size*3/2+mygrid.offsetx
        if self.x_pos%2 == 1:
            self.y_gfx = (self.y_pos-1)*mygrid.size*.86+mygrid.offsety
        else:
            self.y_gfx = (self.y_pos-1)*mygrid.size*.86+mygrid.offsety-mygrid.size*.86
       
        for unit in units:
            self.units.append(unit)
        stacklist.append(self)
    def showunits(self):
        for unit in self.units:
            print(unit.unitreport())
    def find_gfx_pos(self):
        self.x_gfx = (self.x_pos-1)*mygrid.size*3/2+mygrid.offsetx
        if self.y_pos%2 == 1:
            self.y_gfx = (self.y_pos-1)*mygrid.size*.86+mygrid.offsety
        else:
            self.y_gfx = (stack2.y_pos-1)*mygrid.size*.86+mygrid.offsety-mygrid.size*.86*evenoddrow
    def clearunits(self):
        for unit in self.units:
            if unit.health <= 0:
                self.destroyed.append(unit)
                self.units.remove(unit)
    def addunit(self, unittype):
        self.units.append(Unit(unittype))

class Grid(object): #to be used in map creation
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.hexes = []
        self.size = 30
        self.offsetx = 40
        self.offsety = 40
        self.offset = 40
       
    def draw(self):   
        ox = self.offsetx
        oy = self.offsety
        for x in range(self.width):
            for y in range(self.height):
                for evenoddrow in [0, 1]:
                    self.hexes.append([
                        x*2+evenoddrow, y*2+evenoddrow,
                        ox-self.size+self.size*x*3+3/2*self.size*evenoddrow,
                        oy+self.size*y*1.72-self.size*.86*evenoddrow,
                        ox-self.size/2+self.size*x*3+3/2*self.size*evenoddrow,
                        oy+self.size*.86+self.size*y*1.72-self.size*.86*evenoddrow,
                        ox+self.size/2+self.size*x*3+3/2*self.size*evenoddrow,
                        oy+self.size*.86+self.size*y*1.72-self.size*.86*evenoddrow,
                        ox+self.size+self.size*x*3+3/2*self.size*evenoddrow,
                        oy+self.size*y*1.72-self.size*.86*evenoddrow,
                        ox+self.size/2+self.size*x*3+3/2*self.size*evenoddrow,
                        oy-self.size*.86+self.size*y*1.72-self.size*.86*evenoddrow,
                        ox-self.size/2+self.size*x*3+3/2*self.size*evenoddrow,
                        oy-self.size*.86+self.size*y*1.72-self.size*.86*evenoddrow
                    ])

mygrid = Grid(10,15)
mygrid.draw()


stack1 = Stack(1, 1, Unit("Militia"), Unit("Militia"))
stack2 = Stack(2, 3, Unit("Militia"), Unit("Militia"))
stack3 = Stack(5, 5, Unit("Militia"), Unit("Militia"))

def settargets(stack1, stack2, distance):
    count = (len(stack1.units))%(len(stack2.units))
    for unit in stack1.units:
        unit.target = -1
        for weapon in unit.weapons:
            if (arms[weapon][2]<=distance) and (arms[weapon][2] + arms[weapon][3] > distance):
                unit.target = (len(stack1.units)+count)%len(stack2.units)
                unit.active = weapon
                count +=1
            else:
                pass
   
def tohit(unit1, unit2):
    hitroll = randint(1, 20)
    if (hitroll + arms[unit1.active][0] >= unit2.armor):
        return True
    else:
        return False

def damage(unit1, unit2):
    if arms[unit1.weapons[0]][1] >= unit2.armor:
        rat =  (arms[unit1.active][1]-arms[active][1]%unit2.armor)/unit2.armor
        unit2.health -= damtable[int(5-rat)][randint(0,9)]
    else:
        rat = (unit2.armor-unit2.armor% arms[unit1.active][1])/ arms[unit1.active][1]
        unit2.dam = damtable[int(5+rat)][randint(0,9)]
        unit2.health -= unit2.dam
    if unit2.health > 0:
        print( unit1.name + " shoot " + unit1.active + " and deals " + str(unit2.dam) + " damage to " + unit2.name + "  HP: " + str(unit2.health))
    else:
        print( unit1.name + " shoot " + unit1.active + " and deals  " + str(unit2.dam) + " damage to " + unit2.name + "  HP: " + str(unit2.health) + " " + unit2.name + " DESTROYED")

   
def fight(stack1, stack2):
    if len(stack2.units)>0 and len(stack1.units)>0:
        for distance in range(20):
            if len(stack2.units)>0 and len(stack1.units)>0:
                #print("Range: " + str(distance))
                settargets(stack1, stack2, distance)
                settargets(stack2, stack1, distance)
                for shooter in stack1.units:
                    if shooter.target != -1:
                        if tohit(shooter, stack2.units[shooter.target]):
                            damage(shooter, stack2.units[shooter.target])
                        else:
                            print(shooter.name + " shoots " + shooter.active + " and misses " + stack1.units[shooter.target].name)
                for shooter in stack2.units:
                    if shooter.target != -1:
                        if tohit(shooter, stack1.units[shooter.target]):
                            damage(shooter, stack1.units[shooter.target])
                        else:
                            print(shooter.name + " shoots " + shooter.active + " and misses " + stack1.units[shooter.target].name)
                    shooter.target = -1
                stack1.clearunits()
                stack2.clearunits()
                print("")
            else:
                pass
    elif len(stack1.units)>0:
        print("stack 2 is empty")
    elif len(stack2.units)>0:
        print("stack 1 is empty")
    else:
        print("they're all dead, jim.")

def drawunit(self, unit_to_draw):
    self.can.create_image(((unit_to_draw.x_pos-1)*mygrid.size*3/2+mygrid.offsetx,(unit_to_draw.y_pos-1)*mygrid.size*.86+mygrid.offsety),image = unit.image)

class App:

    def __init__(self, master):

        self.unitimage = PhotoImage(file="unit.gif")
        frame = Frame(master)
        frame.pack()
       
        self.pickedstack = 1
        self.active_stack = stack1

        self.button = Button(frame, text="QUIT", fg="red", command=frame.quit)
        self.button.pack(side=LEFT)

        self.fight = Button(frame, text="Fight", command=self.fight)
        self.fight.pack(side=LEFT)
       
        self.show_units = Button(frame, text="Show Units", command=self.show_units)
        self.show_units.pack(side=LEFT)
       
        self.selectfirst = Button(frame, text="Stack One", command=self.selectfirst)
        self.selectfirst.pack(side=LEFT)
       
        self.selectsecond = Button(frame, text="Stack Two", command=self.selectsecond)
        self.selectsecond.pack(side=LEFT)
       
        self.can = Canvas(width=1000, height=700)
        self.can.pack(side = LEFT)
       
        for hex in mygrid.hexes:
            self.can.create_polygon([hex[2],hex[3],hex[4],hex[5],hex[6],hex[7],hex[8],hex[9],hex[10],hex[11],hex[12],hex[13]], outline = "black", fill = "white", width = 1)
       
        #self.can.create_image(self.can.create_image((43,43), image = self.unitimage)
        #self.can.create_image((129,146), image = self.unitimage)
       
        self.can.create_image((stack1.x_gfx, stack1.y_gfx), image = self.unitimage)
        self.can.create_image((stack2.x_gfx, stack2.y_gfx), image = self.unitimage)
        self.can.create_image((stack3.x_gfx, stack3.y_gfx), image = self.unitimage)
       
        self.can.bind("<Button-1>",self.findhex)
        self.can.bind("<Button-2>",self.movecenter)
        self.can.bind("<Button-3>",self.rightclick)
       
        self.listbox = Listbox(master)
        self.listbox.pack()
                       
        for item in namelist:
            self.listbox.insert(END, item)
       
    def findhex(self, event):       
        clickx = (event.x-mygrid.offset+mygrid.size)/mygrid.size/3*2
        clicky = (event.y-mygrid.offset)/(mygrid.size*.86)/2
        if int(round(.5+clickx))%2==0:
            clicky += .5
       
        if round(clickx+1/6)<round(.5+clickx):
            tri_x = clickx-round(clickx-.5)
            tri_y = clicky-round(clicky-.5)
            if round(tri_y)==0:
                #print("above")
                if tri_x*1.73<tri_y:
                    clicky+=1
                    clickx-=1
            else:
                #triangle below halfway on left hexagon of the junction
                if (.3-tri_x>(tri_y-.5)/1.73):
                    clickx-=1
                    if round(.5+clickx)%2==1:
                        clicky-=1
               
        #print(round(.5+clickx), round(clicky+1))
        for item in stacklist:
            if item.x_pos == round(.5+clickx) and item.y_pos == round(clicky+1):
                self.active_stack = item
                print(self.active_stack)
       
    def rightclick(self, event):
        clickx = (event.x-mygrid.offset+mygrid.size)/mygrid.size/3*2
        clicky = (event.y-mygrid.offset)/(mygrid.size*.86)/2
        if int(round(.5+clickx))%2==0:
            clicky += .5
       
        if round(clickx+1/6)<round(.5+clickx):
            tri_x = clickx-round(clickx-.5)
            tri_y = clicky-round(clicky-.5)
            if round(tri_y)==0:
                #print("above")
                if tri_x*1.73<tri_y:
                    clicky+=1
                    clickx-=1
            else:
                #triangle below halfway on left hexagon of the junction
                if (.3-tri_x>(tri_y-.5)/1.73):
                    clickx-=1
                    if round(.5+clickx)%2==1:
                        clicky-=1
        print(round(.5+clickx), round(clicky+1))
        print(self.active_stack.x_pos,self.active_stack.y_pos)
       
        if self.active_stack.x_pos == round(.5+clickx) and (self.active_stack.y_pos == (1+clicky)):
            self.active_stack.y_pos += 1
            print(self.active_stack.x_pos,self.active_stack.y_pos)
        elif self.active_stack.x_pos == round(.5+clickx) and self.active_stack.y_pos == (clicky-1):
            self.active_stack.y_pos += -1
       
        self.can.create_image((self.active_stack.x_gfx, self.active_stack.y_gfx), image = self.unitimage)
   
    def movecenter(self, event):
        mygrid.offsetx += (event.x-280)
        mygrid.offsety += (event.y-280)
        mygrid.draw()
        self.can.update_idletasks()
       
    def unitadd(self):
        print(namelist[int(self.listbox.curselection()[0])])
        if self.pickedstack == 1:
            stack1.units.append(Unit(namelist[int(self.listbox.curselection()[0])]))
        else:
            stack2.units.append(Unit(namelist[int(self.listbox.curselection()[0])]))
       
    def fight(self):
        fight(stack1, stack2)
   
    def show_units(self):
        print("Unit Report:")
        stack1.showunits()
        stack2.showunits()
   
    def selectfirst(self):
        self.selectfirst.config(relief=SUNKEN)
        self.selectsecond.config(relief=RAISED)
        self.pickedstack = 1
   
    def selectsecond(self):
        self.selectfirst.config(relief=RAISED)   
        self.selectsecond.config(relief=SUNKEN)
        self.pickedstack = 2
       
root = Tk()

app = App(root)

root.mainloop()

When you access instance.some_func, you get a reference to a Bound Method, which can be passed as a function object without further parameterizing the reference to self.  Class methods can be called in any order as long as they're defined somewhere in the Class, so you can bind them during App's initialization without worrying about their visibility.
Logged

mainiac

  • Bay Watcher
  • Na vazeal kwah-kai
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1201 on: September 20, 2013, 10:36:34 pm »

The reason why the functions were both bound to the same key is that I thought I had deleted the second one but hadn't.  My documentation, as you might have noticed, is non-existent and this incident made me quite aware of that.  I think I'll need to sit down for a few hours and just go through and comment all the stuff I've done so far.

I also don't have a very clear understanding of what exactly the app does.  This is a project where I'm teaching myself as I go along.  I have figured out that the app gives me a space where I can invoke tkinter resources and sorta works like a main loop in java.  But I'm not entirely sure what you mean by "real members of App."  Your feedback is definitely appreciated on this matter though.
Logged
Ancient Babylonian god of RAEG
--------------
[CAN_INTERNET]
[PREFSTRING:google]
"Don't tell me what you value. Show me your budget and I will tell you what you value"
« Last Edit: February 10, 1988, 03:27:23 pm by UR MOM »
mainiac is always a little sarcastic, at least.

Gentlefish

  • Bay Watcher
  • [PREFSTRING: balloon-like qualities]
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1202 on: October 16, 2013, 02:14:06 am »

So, I want to show a friend of mine some programs I made in a class. Problem is, I compile these on a linux server connected through PuTTY using g++

Is there any way the a.out that gets compiled useable on a windows, or what should I use to compile the raw code?

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: Programming Help Thread (For Dummies)
« Reply #1203 on: October 16, 2013, 02:26:05 am »

Assuming you have the source, you just need to find any old compiler (codeblocks is an IDE that is light and uses gcc/++) and compile it into an exe.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1204 on: October 17, 2013, 01:01:31 am »

Having a dumb problem, been banging my head against the wall about it for a little while,
it's such a simple problem.  I think I just figured out a way to do it, will try it tomorrow cuz about to go to sleep.


Oh wow, I'm dumb, I just realized how much easier this could be.  why do I always need to make things harder than they are.


yaay programming, finally started working on my project again, it's been months.
spent 2 hours or so today bughunting, slow but steady progress.

I just can't wait until it's playable
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

dennislp3

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1205 on: October 18, 2013, 01:15:18 am »

I really need to get back into programming...stopped because of a rather life changing move and have not gotten back into it...which is only my fault...
Logged

head

  • Bay Watcher
  • Whoop Whoop.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1206 on: November 21, 2013, 04:03:11 am »

https://github.com/headswe/ProjectNova

A image.
Spoiler (click to show/hide)

just release this code, hoping someone can make something cool out of it

Its not even close to game. but the calculations for ship items and is from aurora and the game is real-time ships max-speed are based on engines, lots of stuff.

Some of the code is decently commented.

Sidenote: i had no idea where to post it so i posted it here.
« Last Edit: November 21, 2013, 04:07:09 am by head »
Logged
Dev on Baystation12- Forums
Steam Username : Headswe

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: Programming Help Thread (For Dummies)
« Reply #1207 on: November 21, 2013, 04:20:35 am »

Care to explain more?
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

head

  • Bay Watcher
  • Whoop Whoop.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1208 on: November 21, 2013, 04:25:31 am »

Uh, well its a start for a 4X i was writing , or more like the clone of aurora with less VisualBasic and real-time.

Didn't really get that far, so i found it last night and figured i could toss it out there and hope for someone to finish it.


Code is in C#
graphic lib is SFML.NET

Logged
Dev on Baystation12- Forums
Steam Username : Headswe

Anvilfolk

  • Bay Watcher
  • Love! <3
    • View Profile
    • Portuguese blacksmithing forum!
Re: Programming Help Thread (For Dummies)
« Reply #1209 on: November 21, 2013, 10:05:45 am »

SFML <3

How are the SFML.NET bindings? Any good, well supported, etc?

Pranz

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1210 on: November 21, 2013, 10:20:37 am »

So, I want to show a friend of mine some programs I made in a class. Problem is, I compile these on a linux server connected through PuTTY using g++

Is there any way the a.out that gets compiled useable on a windows, or what should I use to compile the raw code?

Fetch the source and compile it on a windows machine or use a cross-compiler.
Logged
DF Valentine
I punched a hamsterwoman in the mouth and her teeth exploded out of his head like gory shrapnel and littered the ground. Happy Valentines Day.

head

  • Bay Watcher
  • Whoop Whoop.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1211 on: November 25, 2013, 09:17:01 am »

SFML <3

How are the SFML.NET bindings? Any good, well supported, etc?

Was a while since i used them. the overall bindings are great. not sure on support.
Logged
Dev on Baystation12- Forums
Steam Username : Headswe

mainiac

  • Bay Watcher
  • Na vazeal kwah-kai
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1212 on: December 04, 2013, 03:17:08 pm »

Can anyone tell me how to turn a python program into an executable in 3.x versions of python?  I've found the internet to be less then clear on the subject.
Logged
Ancient Babylonian god of RAEG
--------------
[CAN_INTERNET]
[PREFSTRING:google]
"Don't tell me what you value. Show me your budget and I will tell you what you value"
« Last Edit: February 10, 1988, 03:27:23 pm by UR MOM »
mainiac is always a little sarcastic, at least.

Anvilfolk

  • Bay Watcher
  • Love! <3
    • View Profile
    • Portuguese blacksmithing forum!
Re: Programming Help Thread (For Dummies)
« Reply #1213 on: December 04, 2013, 04:21:51 pm »

Went through that recently. Look at cx_Freeze. I couldn't make it work for Python 3.3, but apparently works, but is fiddly, with Python 3.2. Best of luck, and do report back!

mainiac

  • Bay Watcher
  • Na vazeal kwah-kai
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1214 on: December 04, 2013, 07:31:54 pm »

Darn, cx_freeze is what I've been trying as it's the only thing I could find that even seems to attempt 3.3 compatibility.  I can't make heads or tails of it, I've tried excecuting commands in the python command prompt and the windows command prompt and get nothing but error messages.
Logged
Ancient Babylonian god of RAEG
--------------
[CAN_INTERNET]
[PREFSTRING:google]
"Don't tell me what you value. Show me your budget and I will tell you what you value"
« Last Edit: February 10, 1988, 03:27:23 pm by UR MOM »
mainiac is always a little sarcastic, at least.
Pages: 1 ... 79 80 [81] 82 83 ... 91