Re: need help to get my python image to move around using tkinter
thankyou so much, that is the exact help I required to put me in the right direction :D -- https://mail.python.org/mailman/listinfo/python-list
Random number help
I need a way of generating a random number but there is a catch: I don't want to include certain numbers, is this possible? random.randint(1,100) works as it will randomly pick numbers between 1 and 100 but say i don't want 48 to come out is there a way of doing this. It needs to be an integer too so not a list unless there is a way to convert list to int Many Thanks Tom -- https://mail.python.org/mailman/listinfo/python-list
Re: Random number help
On Wednesday, 23 November 2016 19:30:04 UTC, Thomas Nyberg wrote: > On 11/23/2016 02:17 PM, Thomas Grops via Python-list wrote: > > I need a way of generating a random number but there is a catch: > > > > I don't want to include certain numbers, is this possible? > > > > random.randint(1,100) works as it will randomly pick numbers between 1 and > > 100 but say i don't want 48 to come out is there a way of doing this. It > > needs to be an integer too so not a list unless there is a way to convert > > list to int > > > > Many Thanks Tom > > > If you specifically want to exclude 48, you could create the list of > acceptable numbers and make a random choice from it. For example: > > >>> import random > >>> l = list(range(1, 48)) + list(range(49, 101)) > >>> random.choice(l) > 64 > > Cheers, > Thomas Thankyou, I just tried this but it seems to be printing every value excluding 48, I am just after a single value -- https://mail.python.org/mailman/listinfo/python-list
Re: Random number help
On Wednesday, 23 November 2016 19:30:21 UTC, Chris Kaynor wrote: > On Wed, Nov 23, 2016 at 11:17 AM, Thomas Grops via Python-list > wrote: > > I need a way of generating a random number but there is a catch: > > > > I don't want to include certain numbers, is this possible? > > > > random.randint(1,100) works as it will randomly pick numbers between 1 and > > 100 but say i don't want 48 to come out is there a way of doing this. It > > needs to be an integer too so not a list unless there is a way to convert > > list to int > > There are a few ways to accomplish this, depending on the exact > requirements. Here are some basic ideas: > - Generate a list of all valid values, and take the one at a random > index. random.sample may be useful. This is guaranteed to complete > (and in only one try), but generally takes extra memory. It is also a > reasonably easy way to sample without replacement (remove the picked > item each time). > - Generate a random number from the larger range, and retry if you get > one in the invalid set. Basically akin to rolling a die, and rerolling > until you don't get a 6. This could theoretically take forever, > however it is often good enough in practice. This will become more and > more inefficient as set of invalid values grows (especially if the > invalid set is a superset of the whole range) many thanks but am I missing something when I use a list it returns a value with the type list not integer? I need an integer -- https://mail.python.org/mailman/listinfo/python-list
Re: Random number help
Thankyou for all your help I have managed to pick a way that works from your suggestions :D -- https://mail.python.org/mailman/listinfo/python-list
Help with two issues, buttons and second class object
Hi I have created some code, which moves a rectangle around and when it hits
the edge it picks a random new direction. It does this by the count function
within my class. I am wanting to create a button to randomly change count but I
my class seems to be getting errors.
I also wanted to create a second class object tank2 but it doesn't seem to move
when I create it with tank2.Tank(x,y,vx,vy) and call tank1.move()
Can anyone help me many thanks.
from tkinter import *
import time
import random
#Properties for the tank
class Tank():
#life, speed, starting position, vectors, size of tank
def __init__(self, x, y, vx, vy):
self.__life=10
#self.canvas=canvas
self.speed = 1
self.x = x
self.y = y
self.vx = vx
self.vy = vy
self.count=0
#self.w = tank.width()
#self.h = tank.height()
self.id = canvas.create_rectangle(x,y,vx,vy, fill='green')
#Create left button and call function to print left
'''buttonL=Button(canvas,text='Change Direction',
command=self.changeDirection())
buttonL.pack(side=BOTTOM, anchor=SW)'''
#print(self.x)
#tank attacked
def attack(self):
print('ouch!')
self.__life -= 1
#check life
def checkLife(self):
if self.__life <= 0:
print('dead')
else:
print(str(self.__life) + " life left")
#respawn at starting point
#def respawn(self):
#medic pack
def medic(self):
self.__life += 5
#move directions
def right(self):
canvas.move(self.id,+5,0)#move right
#reposition x,vx,y,vy values
self.x+=5
self.vx+=5
#Update canvas
canvas.update()
time.sleep(0.1)
def left(self):
canvas.move(self.id,-5,0)#move left
#reposition x,vx,y,vy values
self.x+=-5
self.vx+=-5
#Update canvas
canvas.update()
time.sleep(0.1)
def up(self):
canvas.move(self.id,0,-2)#move up
#reposition x,vx,y,vy values
self.y+=-2
self.vy+=-2
#Update canvas
canvas.update()
time.sleep(0.1)
def down(self):
canvas.move(self.id,0,+2)#move down
#reposition x,vx,y,vy values
self.y+=2
self.vy+=2
#Update canvas
canvas.update()
time.sleep(0.1)
def upLeft(self):
canvas.move(self.id,-1,-1)#move upLeft
#reposition x,vx,y,vy values
self.y+=-1
self.vy+=-1
self.x+=-1
self.vx+=-1
#Update canvas
canvas.update()
time.sleep(0.1)
def upRight(self):
canvas.move(self.id,+1,-1)#move upRight
#reposition x,vx,y,vy values
self.y+=-1
self.vx+=1
self.vy+=-1
self.x+=1
#Update canvas
canvas.update()
time.sleep(0.1)
def downLeft(self):
canvas.move(self.id,-1,+1)#move downLeft
#reposition x,vx,y,vy values
self.x+=-1
self.vx+=-1
self.y+=1
self.vy+=1
#Update canvas
canvas.update()
time.sleep(0.1)
def downRight(self):
#move downRight
canvas.move(self.id,+1,+1)
#reposition x,vx,y,vy values
self.x+=1
self.vx+=1
self.y+=1
self.vy+=1
#Update canvas
canvas.update()
time.sleep(0.1)
def count(self,count):
#Count triggers direction of movement
self.count = count
print (count)
#movement
def move(self):
# Loop for steps in movement
for t in range(1, 1):
#Move direction depending on count value
if self.count==0:
self.left()
if self.count==1:
self.right()
if self.count==2:
self.up()
if self.count==3:
self.down()
if self.count==4:
self.upLeft()
if self.count==5:
self.upRight()
if self.count==6:
self.downRight()
if self.count==7:
self.downLeft()
# If a boundary has been crossed, pick a direction randomly
#Left border
if self.x <= 0:
#banned directions
excludedNumbers = [0,4,7]
#define random integer to be selected
randomNumber = random.randint(0,8)
#nested while loop so that the banned directions are not
selected
while randomNumber in excludedNumbers:
randomNumber = random.randint(0,8)
#feed allowed random direction back to the count
self.count=randomNumber
#Right border
elif self.vx >= 1000:
Re: Help with two issues, buttons and second class object
Wow thankyou that code is really good, I had no programming knowledge until 2 months ago, I enjoy your descriptions it is really helpful for me. I like to understand what the code does before using it myself or a variant of it. Will tweak bits tonight the project is in tomorrow. This code is just a side to the one I completed and will be handing in so im just doing this for fun and learning but the past months but as I learn new things I keep wanting to rebuild my program. I will be adding obstacles into the code too and eventually enemies, that explains the commented out code for attack and medic. I will upload the code when I am done with it or get stuck again to see what your feedback is :D -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with two issues, buttons and second class object
Peter, in your code what does that self.root = root mean in the __init__ function of the class -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with two issues, buttons and second class object
Also I am struggling to understand: def move_tank(self, dx, dy): self.x += dx self.y += dy self.canvas.move(self.id, dx, dy) Where does the dx and dy values get input? -- https://mail.python.org/mailman/listinfo/python-list
