This topic is for Python programmers only. Please do not post anything here unless you know what you are talking about!
Post your some of your python code below so we can run it and see what you created!
RULES
1. Do not post any code that could harm a computer in ANY way shape or form.
2. I am not responsible for any code that may harm your computer
3. Use your own code.
Here a basic number guessing game. I'm still learning and its VERY basic.
from random import randint
secret = randint(1, 30)
print("Welcome!")
guess = 0
while guess != secret:
g = input("Guess the number:")
guess = int(g)
if guess == secret:
print("You Win!")
else:
if guess > secret:
print("Too High!")
else:
print("Too Low!")
print("Game Over")
use the code tags:
[code ][ /code]
without the spaces it's:
[code]from random import randint
secret = randint(1, 30)
print("Welcome!")
guess = 0
while guess != secret:
g = input("Guess the number:")
guess = int(g)
if guess == secret:
print("You Win!")
else:
if guess > secret:
print("Too High!")
else:
print("Too Low!")
print("Game Over")[/code]
Now if I can find out how to generate random numbers in C#..
My version in C#, without exception handling:
[code]using System;
namespace randomgame
{
class MainClass
{
public static void Main (string[] args)
{
Random random = new Random ();
int rand = random.Next (1, 31), guess = 0;
string guessstring;
Console.WriteLine("Welcome");
while (guess != rand) {
Console.WriteLine("Guess the number");
guessstring = Console.ReadLine ();
guess = int.Parse (guessstring);
if (guess == rand) {
Console.WriteLine("You win!");
}else if (guess > rand){
Console.WriteLine("Too high!");
}else{
Console.WriteLine("Too low!");
}
}
Console.WriteLine("Game over");
}
}
}[/code]
import sys #importing sys means you can use sys.exit() to quit the program
# Room class
class Room(object):
""" Room """
def __init__(self,description):
self.description = description
#A few sample rooms from my Lord of the Rings Text Adventure
room1 = Room("Bedroom1. You are in a bedroom in The Prancing Pony Inn. \neast: Hallway")
room2 = Room("You are in the hallway. \neast: Bedroom \nwest: Bedroom \nnorth: Hallway")
room3 = Room("You are at the northern end of the hallway. \nsouth: Hallway")
room4 = Room("Bedroom2. You are in a bedroom in The Prancing Pony Inn. \nwest: Hallway")
# Room coordinates
room1.coordinates = [0,room2,0,0]
room2.coordinates = [room3,room4,0,room1]
room3.coordinates = [0,0,0,room2]
room4.coordinates = [0,0,0,room2]
# Character class
class Character(object):
def __init__(self,name,gender,hair,age,Health,location = room1):
self.name = name
self.gender = gender
self.hair = hair
self.age = age
self.Health = Health
self.location = location
self.inv = []
def look(self):
place = self.location
print(place.description)
#Creating main character
characters = []
Wallace = Character("Wallace","m","black",20,100)
characters.append(Wallace)
currentchar = Wallace
#Introduction
print ("Welcome to Wallace's text adventure.")
print ("What is your name?")
currentchar.name = raw_input()
print ("Hello",currentchar.name)
print ('Type "commands" to see the command list')
#This prints the room description
currentchar.look()
#The game loop, once it has gone through the loop it will ask for the player to type in input again
while True:
command = raw_input().lower()
if command == "n" or command == "north":
if currentchar.location.coordinates[0] == 0: #0 means there is no exit in that direction
print("You cannot go that way.")
else:
currentchar.location = currentchar.location.coordinates[0] #changes location to the north one for the current room
currentchar.look()
elif command == "e" or command == "east":
if currentchar.location.coordinates[1] == 0:
print("You cannot go that way.")
else:
currentchar.location = currentchar.location.coordinates[1]
currentchar.look()
elif command == "s" or command == "south":
if currentchar.location.coordinates[2] == 0:
print("You cannot go that way.")
else:
currentchar.location = currentchar.location.coordinates[2]
currentchar.look()
elif command == "w" or command == "west":
if currentchar.location.coordinates[3] == 0:
print("You cannot go that way.")
else:
currentchar.location = currentchar.location.coordinates[3]
currentchar.look()
elif command == "commands":
print '"n","e","s", and "w" make your character go north, east, south, and west respectively'
print '"end" to quit'
print '"look" to look around the room'
print '"players" to see the player list'
elif command == "players":
print(characters)
elif command == "end" or command == "quit":
sys.exit() # This quits the game
A template for a python text adventure
Rollback Post to RevisionRollBack
I would rather live my life as if there is a God and die to find out there isn't, than live my life as if there isn't and die to find out there is.Albert Camus
Haha I can tell this is going to turn into a "who can show the most complex program" contest
Does anyone know how I would implement pathfinding in Python? I'm trying to learn how to use A* for a top down shooter I'm making but I can't get my head round it :/
I would rather live my life as if there is a God and die to find out there isn't, than live my life as if there isn't and die to find out there is.Albert Camus
Haha I can tell this is going to turn into a "who can show the most complex program" contest
Does anyone know how I would implement pathfinding in Python? I'm trying to learn how to use A* for a top down shooter I'm making but I can't get my head round it :/
If you want some basic pathfinding hack something together like
if player.left > object.left:
object.move_ip(speed,0)
until you understand a* or make something better than that simple example for moving left
Also these programs on here are short and simple anyone with a decent amount of skill in python would make something better in decent amount of time.
Rollback Post to RevisionRollBack
I Play games, and program in Python, If you need help send me a message!
But when they come to an obstacle it can't get round it
For something requiring more skill and time here's the code so far for my 3d game using Panda3D (it needs tree models and textures though):
import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject
from pandac.PandaModules import *
loadPrcFileData("", "fullscreen 1")
loadPrcFileData("","win-size 1024 768")
from direct.interval.IntervalGlobal import LerpFunc
import sys
from random import randint,choice
base.setFrameRateMeter(True)
lastPos = base.cam.getPos()
updateDistanceSquared = 64*64
skybox = loader.loadModel("skybox")
skybox.setScale(512)
skybox.setBin('background', 1)
skybox.setDepthWrite(0)
skybox.setLightOff()
skybox.reparentTo(render)
water = loader.loadModel("water1")
water.reparentTo(render)
water.setScale(512)
##water.setBin('background', 1)
##water.setDepthWrite(0)
water.setZ(10)
water.setTexture(loader.loadTexture("after.png"))
chunksize = 128
perlin = PerlinNoise2(40,40)
##print perlin(0,0)
name = -1
chunks = []
chunks1 = []
chunks2 = []
coords = []
grass = loader.loadTexture("grass1.png")
class chunk(object):
x = 0
y = 0
north = False
east = False
south = False
west = False
North = 0
East = 0
West = 0
South = 0
## directions = [x,y1,x1,y,x,y-chunksize,x-chunksize,y]
done = False
offsetx = 0
offsety = 0
#Load Tree Models
trees = []
tree = loader.loadModel("trees/treeHighA")
tree.setScale(0.5)
tree.flattenStrong()
trees.append(tree)
tree1 = loader.loadModel("trees/treeHighB")
tree1.setScale(0.5)
tree1.flattenStrong()
trees.append(tree1)
tree2 = loader.loadModel("trees/treeHighC")
tree2.setScale(0.5)
tree2.flattenStrong()
trees.append(tree2)
tree3 = loader.loadModel("trees/treeLowA")
tree3.setScale(0.5)
tree3.flattenStrong()
trees.append(tree3)
tree4 = loader.loadModel("trees/treeLowB")
tree4.setScale(0.5)
tree4.flattenStrong()
trees.append(tree4)
tree5 = loader.loadModel("trees/treeLowC")
tree5.setScale(0.5)
tree5.flattenStrong()
trees.append(tree5)
def __init__(self,x,y,offsetx,offsety):
self.x = x
self.y = y
self.offsetx = x
self.offsety = y
self.x1 = self.x+chunksize
self.y1 = self.y+chunksize
def makeChunk(self):
global name
if [self.x,self.y] not in coords:
self.n = PNMImage(chunksize+1,chunksize+1)
self.t = PNMImage(chunksize+1,chunksize+1)
for y in xrange(chunksize+1):
for x in xrange(chunksize+1):
p = perlin.noise(x+self.offsetx,y-self.offsety)
p+=2
if p >= 0:
p*=127.5
## p+=10
self.n.setRedVal(x,y,p)
self.n.setGreenVal(x,y,p)
self.n.setBlueVal(x,y,p)
## if p < 10:
## self.t.setRedVal(x,y,25)
## self.t.setGreenVal(x,y,25)
## self.t.setBlueVal(x,y,p+150)
## elif p > 10 and p < 50:
## self.t.setRedVal(x,y,p+150)
## self.t.setGreenVal(x,y,p+150)
## self.t.setBlueVal(x,y,100)
## elif p > 50:
## self.t.setRedVal(x,y,0)
## self.t.setGreenVal(x,y,400-p)
## self.t.setBlueVal(x,y,0)
## elif p < 0:
## self.t.setRedVal(x,y,25)
## self.t.setGreenVal(x,y,25)
## self.t.setBlueVal(x,y,p+150)
self.n.makeGrayscale()
name+=1
name1 = str(name)
## self.t.write("maps/"+name1+"_.png")
self.n.write("maps/"+name1+".png")
## self.T = Texture()
## self.T.load(self.t)
#GeoMipTerrain Stuff
terrain = GeoMipTerrain("terrain")
terrain.setHeightfield(self.n)
terrain.setBlockSize(128)
terrain.setNear(256)
terrain.setFar(512)
terrain.setMinLevel(2)
terrain.setFocalPoint(base.camera)
terrain.setBorderStitching(True)
root = terrain.getRoot()
root.reparentTo(render)
root.setSz(100)
self.scaleZ = root.getSz()
terrain.setAutoFlatten(GeoMipTerrain.AFMStrong)
terrain.generate()
ts = TextureStage("ts0")
root.setTexture(ts,grass)#loader.loadTexture("test.png"))
## root.setLight(FPS.plnp)
## root.setShaderInput("light", FPS.plnp)
## root.setLight(FPS.alnp)
## root.setShaderAuto()
chunks.append(terrain)
chunks1.append(root)
root.setPos(self.x,self.y,0)
coords.append([self.x,self.y])
#Make Trees
tree = choice(self.trees)
t = tree.copyTo(render)
x = self.x+randint(0,chunksize)
y = self.y+randint(0,chunksize)
if terrain.getElevation(x,y) > 20:
t.setPos(x,y,terrain.getElevation(x,y)*self.scaleZ-15)
class FPS(object,DirectObject):
#make a chunk
start = chunk(0,0,0,0)
start.makeChunk()
start.done = True
currentChunk = start
chunks2.append(start)
#make chunks around it - east
c = chunk(currentChunk.x+chunksize,currentChunk.y,currentChunk.offsetx+chunksize-1,currentChunk.offsety)
c.makeChunk()
currentChunk.east = True
currentChunk.East = c
c.done = True
c.West = currentChunk
chunks2.append(c)
# - west
c = chunk(currentChunk.x-chunksize,currentChunk.y,currentChunk.offsetx-chunksize+1,currentChunk.offsety)
c.makeChunk()
currentChunk.west = True
currentChunk.West = c
c.done = True
c.East = currentChunk
chunks2.append(c)
# - north
c = chunk(currentChunk.x,currentChunk.y+chunksize,currentChunk.offsetx,currentChunk.offsety+chunksize-1)
c.makeChunk()
currentChunk.north = True
currentChunk.North = c
c.done = True
c.South = currentChunk
chunks2.append(c)
# - south
c = chunk(currentChunk.x,currentChunk.y-chunksize,currentChunk.offsetx,currentChunk.offsety-chunksize+1)
c.makeChunk()
currentChunk.south = True
currentChunk.South = c
c.done = True
c.North = currentChunk
chunks2.append(c)
def __init__(self):
global c,chunks,chunks1
self.winXhalf = base.win.getXSize()/2
self.winYhalf = base.win.getYSize()/2
## self.makeTrees()
self.initCollision()
## self.initShadows()
c = 0.0
self.initPlayer()
self.initWally()
def makeTrees(self):
self.trees = []
self.tree = loader.loadModel("trees/treeHighA")
self.tree.setScale(0.2)
self.trees.append(self.tree)
self.tree1 = loader.loadModel("trees/treeHighB")
self.tree1.setScale(0.2)
self.trees.append(self.tree1)
self.tree2 = loader.loadModel("trees/treeHighC")
self.tree2.setScale(0.2)
self.trees.append(self.tree2)
self.tree3 = loader.loadModel("trees/treeLowA")
self.tree3.setScale(0.2)
self.trees.append(self.tree3)
self.tree4 = loader.loadModel("trees/treeLowB")
self.tree4.setScale(0.2)
self.trees.append(self.tree4)
self.tree5 = loader.loadModel("trees/treeLowC")
self.tree5.setScale(0.2)
self.trees.append(self.tree5)
def initCollision(self):
#initialize traverser
base.cTrav = CollisionTraverser()
base.cTrav.setRespectPrevTransform(True)
# base.cTrav.showCollisions(render)
#initialize pusher
self.pusher = CollisionHandlerPusher()
# collision bits
self.groundCollBit = BitMask32.bit(0)
self.collBitOff = BitMask32.allOff()
def initPlayer(self):
#load man
self.man = render.attachNewNode('man') # keep this node scaled to identity
self.man.setPos(chunksize/2,chunksize/2,25)
# should be avatar model
# model = loader.loadModel('teapot')
# model.reparentTo(self.man)
# model.setScale(.05)
# camera
base.camera.reparentTo(self.man)
base.camera.setPos(0,0,8.7)#2.7
base.camLens.setNearFar(.1,1000) #originally at 1000
base.disableMouse()
props = WindowProperties()
props.setCursorHidden(True)
base.win.requestProperties(props)
#create a collsion solid around the man
manC = self.man.attachCollisionSphere('manSphere', 0,0,1, .4, self.groundCollBit,self.collBitOff)
self.pusher.addCollider(manC,self.man)
base.cTrav.addCollider(manC,self.pusher)
speed = 50
Forward = Vec3(0,speed*2,0)
Back = Vec3(0,-speed,0)
Left = Vec3(-speed,0,0)
Right = Vec3(speed,0,0)
Stop = Vec3(0)
self.walk = Stop
self.strafe = Stop
self.jump = 0
taskMgr.add(self.move, 'move-task')
self.jumping = LerpFunc( Functor(self.__setattr__,"jump"),
duration=1.5, fromData=.25, toData=0)
self.accept( "escape",sys.exit )
self.accept( "space" , self.startJump)
self.accept( "s" , self.__setattr__,["walk",Back] )
self.accept( "w" , self.__setattr__,["walk",Forward])
self.accept( "s-up" , self.__setattr__,["walk",Stop] )
self.accept( "w-up" , self.__setattr__,["walk",Stop] )
self.accept( "a" , self.__setattr__,["strafe",Left])
self.accept( "d" , self.__setattr__,["strafe",Right] )
self.accept( "a-up" , self.__setattr__,["strafe",Stop] )
self.accept( "d-up" , self.__setattr__,["strafe",Stop] )
self.manGroundColNp = self.man.attachCollisionRay( 'manRay',
0,0,.6, 0,0,-1,
self.groundCollBit,self.collBitOff)
self.manGroundHandler = CollisionHandlerGravity()
self.manGroundHandler.addCollider(self.manGroundColNp,self.man)
base.cTrav.addCollider(self.manGroundColNp, self.manGroundHandler)
def startJump(self):
if self.manGroundHandler.isOnGround():
self.jumping.start()
def move(self,task):
dt=globalClock.getDt()
# mouse
md = base.win.getPointer(0)
x = md.getX()
y = md.getY()
if base.win.movePointer(0, self.winXhalf, self.winYhalf):
self.man.setH(self.man, (x - self.winXhalf)*-0.1)
base.camera.setP( clampScalar(-90,90, base.camera.getP() - (y - self.winYhalf)*0.1) )
# move where the keys set it
moveVec=(self.walk+self.strafe)*dt # horisontal
moveVec.setZ( self.jump ) # vertical
self.man.setFluidPos(self.man,moveVec)
# jump damping
if self.jump>0:
self.jump = clampScalar( 0,1, self.jump*.9)
#check if new chunk needed
if self.man.getX() > self.currentChunk.x1+chunksize/6 and self.currentChunk.East != 0:
self.currentChunk = self.currentChunk.East
print "east"
if not self.currentChunk.east:
c = chunk(self.currentChunk.x+chunksize,self.currentChunk.y,self.currentChunk.offsetx+chunksize-1,self.currentChunk.offsety)
c.makeChunk()
self.currentChunk.east = True
self.currentChunk.East = c
c.done = True
c.West = self.currentChunk
chunks2.append(c)
if not self.currentChunk.west:
c = chunk(self.currentChunk.x-chunksize,self.currentChunk.y,self.currentChunk.offsetx-chunksize+1,self.currentChunk.offsety)
c.makeChunk()
self.currentChunk.west = True
self.currentChunk.West = c
c.done = True
c.East = self.currentChunk
chunks2.append(c)
if not self.currentChunk.north:
c = chunk(self.currentChunk.x,self.currentChunk.y+chunksize,self.currentChunk.offsetx,self.currentChunk.offsety+chunksize-1)
c.makeChunk()
self.currentChunk.north = True
self.currentChunk.North = c
c.done = True
c.South = self.currentChunk
chunks2.append(c)
if not self.currentChunk.south:
c = chunk(self.currentChunk.x,self.currentChunk.y-chunksize,self.currentChunk.offsetx,self.currentChunk.offsety-chunksize+1)
c.makeChunk()
self.currentChunk.south = True
self.currentChunk.South = c
c.done = True
c.North = self.currentChunk
chunks2.append(c)
elif self.man.getX() < self.currentChunk.x-chunksize/6 and self.currentChunk.West != 0:
self.currentChunk = self.currentChunk.West
print "west"
if not self.currentChunk.east:
c = chunk(self.currentChunk.x+chunksize,self.currentChunk.y,self.currentChunk.offsetx+chunksize-1,self.currentChunk.offsety)
c.makeChunk()
self.currentChunk.east = True
self.currentChunk.East = c
c.done = True
c.West = self.currentChunk
chunks2.append(c)
if not self.currentChunk.west:
c = chunk(self.currentChunk.x-chunksize,self.currentChunk.y,self.currentChunk.offsetx-chunksize+1,self.currentChunk.offsety)
c.makeChunk()
self.currentChunk.west = True
self.currentChunk.West = c
c.done = True
c.East = self.currentChunk
chunks2.append(c)
if not self.currentChunk.north:
c = chunk(self.currentChunk.x,self.currentChunk.y+chunksize,self.currentChunk.offsetx,self.currentChunk.offsety+chunksize-1)
c.makeChunk()
self.currentChunk.north = True
self.currentChunk.North = c
c.done = True
c.South = self.currentChunk
chunks2.append(c)
if not self.currentChunk.south:
c = chunk(self.currentChunk.x,self.currentChunk.y-chunksize,self.currentChunk.offsetx,self.currentChunk.offsety-chunksize+1)
c.makeChunk()
self.currentChunk.south = True
self.currentChunk.South = c
c.done = True
c.North = self.currentChunk
chunks2.append(c)
if self.man.getY() > self.currentChunk.y1+chunksize/6 and self.currentChunk.North != 0:
self.currentChunk = self.currentChunk.North
print "north"
if not self.currentChunk.east:
c = chunk(self.currentChunk.x+chunksize,self.currentChunk.y,self.currentChunk.offsetx+chunksize-1,self.currentChunk.offsety)
c.makeChunk()
self.currentChunk.east = True
self.currentChunk.East = c
c.done = True
c.West = self.currentChunk
chunks2.append(c)
if not self.currentChunk.west:
c = chunk(self.currentChunk.x-chunksize,self.currentChunk.y,self.currentChunk.offsetx-chunksize+1,self.currentChunk.offsety)
c.makeChunk()
self.currentChunk.west = True
self.currentChunk.West = c
c.done = True
c.East = self.currentChunk
chunks2.append(c)
if not self.currentChunk.north:
c = chunk(self.currentChunk.x,self.currentChunk.y+chunksize,self.currentChunk.offsetx,self.currentChunk.offsety+chunksize-1)
c.makeChunk()
self.currentChunk.north = True
self.currentChunk.North = c
c.done = True
c.South = self.currentChunk
chunks2.append(c)
if not self.currentChunk.south:
c = chunk(self.currentChunk.x,self.currentChunk.y-chunksize,self.currentChunk.offsetx,self.currentChunk.offsety-chunksize+1)
c.makeChunk()
self.currentChunk.south = True
self.currentChunk.South = c
c.done = True
c.North = self.currentChunk
chunks2.append(c)
elif self.man.getY() < self.currentChunk.y-chunksize/6 and self.currentChunk.South != 0:
self.currentChunk = self.currentChunk.South
print "south"
if not self.currentChunk.east:
c = chunk(self.currentChunk.x+chunksize,self.currentChunk.y,self.currentChunk.offsetx+chunksize-1,self.currentChunk.offsety)
c.makeChunk()
self.currentChunk.east = True
self.currentChunk.East = c
c.done = True
c.West = self.currentChunk
chunks2.append(c)
if not self.currentChunk.west:
c = chunk(self.currentChunk.x-chunksize,self.currentChunk.y,self.currentChunk.offsetx-chunksize+1,self.currentChunk.offsety)
c.makeChunk()
self.currentChunk.west = True
self.currentChunk.West = c
c.done = True
c.East = self.currentChunk
chunks2.append(c)
if not self.currentChunk.north:
c = chunk(self.currentChunk.x,self.currentChunk.y+chunksize,self.currentChunk.offsetx,self.currentChunk.offsety+chunksize-1)
c.makeChunk()
self.currentChunk.north = True
self.currentChunk.North = c
c.done = True
c.South = self.currentChunk
chunks2.append(c)
if not self.currentChunk.south:
c = chunk(self.currentChunk.x,self.currentChunk.y-chunksize,self.currentChunk.offsetx,self.currentChunk.offsety-chunksize+1)
c.makeChunk()
self.currentChunk.south = True
self.currentChunk.South = c
c.done = True
c.North = self.currentChunk
chunks2.append(c)
skybox.setPos(base.camera,0,0,0)
## water.setPos(base.camera,-50,-50,10)
return task.cont
def moveGuy(self,task):
global xpos
self.m.setX(xpos)
xpos+=0.5
return task.cont
def initWally(self):
self.wally = loader.loadModel("wally")
self.wally.reparentTo(render)
self.wally.setPos(0,0,4)
def initShadows(self):
# Add a light to the scene.
self.lightpivot = render.attachNewNode("lightpivot")
self.lightpivot.setPos(0,0,25)
self.plight = PointLight('plight')
self.plight.setColor(Vec4(1, 1, 1, 1))
self.plight.setAttenuation(Vec3(0.001,0.001,0))#0.7,0.05,0))
self.plnp = self.lightpivot.attachNewNode(self.plight)
self.plnp.setPos(45, 45, 400)
# Add an ambient light
self.alight = AmbientLight('alight')
self.alight.setColor(Vec4(0.2, 0.2, 0.2, 1))
self.alnp = render.attachNewNode(self.alight)
# create a sphere to denote the light
sphere = loader.loadModel("sphere")
sphere.setScale(40)
self.shaderenable = 1
# Add a task to keep updating the terrain
def updateTask(task):
global lastPos
pos = base.cam.getPos()
distSq = (pos - lastPos).lengthSquared()
for i in chunks:
i.getRoot().setCollideMask(BitMask32.bit(0))
if distSq > updateDistanceSquared:
lastPos = pos
for i in chunks:
i.update()
return task.cont
taskMgr.add(updateTask, "update")
FPS()
render.setShaderAuto()
run()
Rollback Post to RevisionRollBack
I would rather live my life as if there is a God and die to find out there isn't, than live my life as if there isn't and die to find out there is.Albert Camus
The Meaning of Life, the Universe, and Everything.
Join Date:
12/29/2012
Posts:
56
Member Details
Here's something I wrote a few weeks ago.
def curry(a):
def z(f):
class C(object):__init__,__call__=lambda c,*s:setattr(c,"s",s),lambda c,*s:f(*c.s+s)if len(c.s+s)>=a else type(c)(*c.s+s)
return C()
return z
This is a codegolf-y implementation of Haskell-style curried functions in Python. One uses it like this:
from functional import curry
@curry(3) # Evaluate after 3 arguments have been passed
def add_three(x, y, z):
return x + y + z
a = add_three # takes (x, y, z), returns add_three(x, y, z)
b = a(1) # b(y, z), returns add_three(1, y, z)
c = b(2) # c(z), returns b(2, z) or add_three(1, 2, z)
d = c(3) # equivalent to b(2, 3), a(1, 2, 3) or add_three(1, 2, 3) -> contains 6
e = b(4, 5) # equivalent to a(1, 4, 5) or add_three(1, 4, 5) -> contains 10
f = b() # equivalent to b
I've got a clearly-written version somewhere, but I can't find it.
Post your some of your python code below so we can run it and see what you created!
RULES
1. Do not post any code that could harm a computer in ANY way shape or form.
2. I am not responsible for any code that may harm your computer
3. Use your own code.
Here a basic number guessing game. I'm still learning and its VERY basic.
[code ][ /code]
without the spaces it's:
[code]from random import randint secret = randint(1, 30) print("Welcome!") guess = 0 while guess != secret: g = input("Guess the number:") guess = int(g) if guess == secret: print("You Win!") else: if guess > secret: print("Too High!") else: print("Too Low!") print("Game Over")[/code]
Now if I can find out how to generate random numbers in C#..
My version in C#, without exception handling:
[code]using System; namespace randomgame { class MainClass { public static void Main (string[] args) { Random random = new Random (); int rand = random.Next (1, 31), guess = 0; string guessstring; Console.WriteLine("Welcome"); while (guess != rand) { Console.WriteLine("Guess the number"); guessstring = Console.ReadLine (); guess = int.Parse (guessstring); if (guess == rand) { Console.WriteLine("You win!"); }else if (guess > rand){ Console.WriteLine("Too high!"); }else{ Console.WriteLine("Too low!"); } } Console.WriteLine("Game over"); } } }[/code]
System.Random.
This is a Python topic. :/
Thanks for helping her though!
A template for a python text adventure
hi
Does anyone know how I would implement pathfinding in Python? I'm trying to learn how to use A* for a top down shooter I'm making but I can't get my head round it :/
If you want some basic pathfinding hack something together like
if player.left > object.left:
object.move_ip(speed,0)
until you understand a* or make something better than that simple example for moving left
Also these programs on here are short and simple anyone with a decent amount of skill in python would make something better in decent amount of time.
At the moment I use something a bit like that except that it finds the rise and run:
But when they come to an obstacle it can't get round it
For something requiring more skill and time here's the code so far for my 3d game using Panda3D (it needs tree models and textures though):
This is a codegolf-y implementation of Haskell-style curried functions in Python. One uses it like this:
I've got a clearly-written version somewhere, but I can't find it.
I don't think the if input == will work. That variable has no value. And I don't think os.shutdown is a python command.
hi
Dude this is for python not LUA plus it won't work because input isn't assigned.
And this is a python related thread. Thanks for saying something useless.
hi
I made an awesome customizable sentence generator!
Check it out here:
https://repl.it/BPPG
Java is where it's at. OOP ftw!