• Advertisement
Sign in to follow this  

python

This topic is 3626 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey there guys, im new to this whole python language. I need to have a program that uses classes and definition, the only problem is, i dont know what a class is.

Share this post


Link to post
Share on other sites
Advertisement
If you don't know what a class is why do you thing you need it?

Share this post


Link to post
Share on other sites
I have to write a program with 2 dice that are sprites, which spin when you click a label.

I need to make both the die and the label classes with def in them

Share this post


Link to post
Share on other sites
You said when you *click*... that implies you're working in some sort of existing framework like PyGame. Give more information if you want any kind of advice from here, because there are a just far too many possible GUI frameworks in which you might click on something.

Share this post


Link to post
Share on other sites
This is what I have right now.... the thing i cant figure out is why I cant make 2 dif dice with dif coordinates. everytime I do this, it only makes one box


import pygame, random
pygame.init()

screen = pygame.display.set_mode((640, 480))

class Die(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((150, 150))
self.image.fill((160, 0, 160))
self.rect = self.image.get_rect()
self.rect.centerx = 100
self.rect.centery = 100

class Label(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.font = pygame.font.SysFont("Arial", (30))
self.text = ""
self.center = (320, 240)

def update(self):
self.image = self.font.render(self.text, 1, (0, 0, 0))
self.rect = self.image.get_rect()
self.rect.center = self.center

def main():
pygame.display.set_caption("Rolling Dice With Voice")

background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((255, 255, 255))
screen.blit(background, (0,0))

label = Label()
labelEvent = Label()
die1 = Die()
die2 = Die()

allSprites = pygame.sprite.Group(die1, die2, label, labelEvent)

die1.centerx = 200
die1.centery = 100
die2.centerx = 400
die2.centery = 100

label.text = "Click here to roll the die"
label.center = (320, 50)

clock = pygame.time.Clock()
keepGoing = True
while keepGoing:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
elif event.type == pygame.MOUSEMOTION:
(mouseX, mouseY) = pygame.mouse.get_pos()
labelEvent.text = "mouse: (%d, %d)" % (mouseX, mouseY)
elif event.type == pygame.MOUSEBUTTONDOWN:
labelEvent.text = "button press"
elif event.type == pygame.KEYDOWN:
labelEvent.text = "key down"

allSprites.clear(screen, background)
allSprites.update()
allSprites.draw(screen)

pygame.display.flip()

if __name__ == "__main__":
main()

pygame.quit()

Share this post


Link to post
Share on other sites
Quote:
Original post by baseballmike1124
This is what I have right now.... the thing i cant figure out is why I cant make 2 dif dice with dif coordinates. everytime I do this, it only makes one box.

You have two Labels, but they both have the same position, so one obscures the other (whichever was drawn last).

Also, doing die.centerx = 200 is not equivalent to die.rect.centerx = 200. You can make it so, but it isn't automatically so.

Share this post


Link to post
Share on other sites
I now have this... The only thing that I need to know is how to get the sum of die1.roll() and die2.roll so that I can play the sound files associated with them, but i do not know how to do that. Can someone please help?

thanks

import pygame, random
pygame.init()
pygame.mixer.init()

screen = pygame.display.set_mode((640, 480))

class Die(pygame.sprite.Sprite):
def __init__(self, die_x, die_y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((150, 150))
self.image.fill((160, 0, 160))
self.rect = self.image.get_rect()
self.rect.centerx = die_x
self.rect.centery = die_y

def roll(self):
dieroll = random.randint(1,6)
self.image.fill((160, 0, 160))
if dieroll == 1:
pygame.draw.circle(self.image, (0,0,0), (75,75), (15), (0))
if dieroll == 2:
pygame.draw.circle(self.image, (0,0,0), (40,40), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (110,110), (15), (0))
if dieroll == 3:
pygame.draw.circle(self.image, (0,0,0), (40,40), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (110,110), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (75,75), (15), (0))
if dieroll == 4:
pygame.draw.circle(self.image, (0,0,0), (40,40), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (40,110), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (110,40), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (110,110), (15), (0))
if dieroll == 5:
pygame.draw.circle(self.image, (0,0,0), (40,40), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (40,110), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (110,40), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (110,110), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (75,75), (15), (0))
if dieroll == 6:
pygame.draw.circle(self.image, (0,0,0), (40,40), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (110,110), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (40,110), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (110,40), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (40,75), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (110,75), (15), (0))








## pygame.draw.circle(Surface, color, pos, radius, width=0): return Rect


class Label(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.font = pygame.font.SysFont("Arial", (30))
self.text = ""
self.center = (320, 240)

def update(self):
self.image = self.font.render(self.text, 1, (0, 0, 0))
self.rect = self.image.get_rect()
self.rect.center = self.center


def main():
pygame.display.set_caption("Rolling Dice With Voice")

background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((255, 255, 255))
screen.blit(background, (0,0))

label = Label()
labelEvent = Label()
die1 = Die(230, 150)
die2 = Die(420, 150)



number2 = pygame.mixer.Sound("two.ogg")
number3 = pygame.mixer.Sound("three.ogg")
number4 = pygame.mixer.Sound("four.ogg")
number5 = pygame.mixer.Sound("five.ogg")
number6 = pygame.mixer.Sound("six.ogg")
number7 = pygame.mixer.Sound("seven.ogg")
number8 = pygame.mixer.Sound("eight.ogg")
number9 = pygame.mixer.Sound("nine.ogg")
number10 = pygame.mixer.Sound("ten.ogg")
number11 = pygame.mixer.Sound("eleven.ogg")
number12 = pygame.mixer.Sound("twelve.ogg")




allSprites = pygame.sprite.Group(die1, die2, label, labelEvent)


label.text = "Click here to roll the die"
label.center = (320, 50)

clock = pygame.time.Clock()
keepGoing = True
while keepGoing:
clock.tick(30)
mouse = pygame.mouse.get_pos()
mouse_x = mouse[0]
mouse_y = mouse[1]
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
if event.type == pygame.MOUSEBUTTONDOWN:
if label.rect.collidepoint(mouse[0], mouse[1]):
die1.roll()
die2.roll()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
number2.play()

allSprites.clear(screen, background)
allSprites.update()
allSprites.draw(screen)

pygame.display.flip()

if __name__ == "__main__":
main()

pygame.quit()

Share this post


Link to post
Share on other sites
Please enclose your code in [source lang="python"] [/source] for long snippets or [code] and for short snippets. The former gives a scrolling box with syntax highlighting; both preserve whitespace.

Particularly since, from what I know, Python uses indentation as part of the syntax. :-)

Share this post


Link to post
Share on other sites
[Sourcelang="python"]

import pygame, random
pygame.init()
pygame.mixer.init()

screen = pygame.display.set_mode((640, 480))

class Die(pygame.sprite.Sprite):
def __init__(self, die_x, die_y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((150, 150))
self.image.fill((160, 0, 160))
self.rect = self.image.get_rect()
self.rect.centerx = die_x
self.rect.centery = die_y

def roll(self):
dieroll = random.randint(1,6)
self.image.fill((160, 0, 160))
if dieroll == 1:
pygame.draw.circle(self.image, (0,0,0), (75,75), (15), (0))
if dieroll == 2:
pygame.draw.circle(self.image, (0,0,0), (40,40), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (110,110), (15), (0))
if dieroll == 3:
pygame.draw.circle(self.image, (0,0,0), (40,40), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (110,110), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (75,75), (15), (0))
if dieroll == 4:
pygame.draw.circle(self.image, (0,0,0), (40,40), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (40,110), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (110,40), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (110,110), (15), (0))
if dieroll == 5:
pygame.draw.circle(self.image, (0,0,0), (40,40), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (40,110), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (110,40), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (110,110), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (75,75), (15), (0))
if dieroll == 6:
pygame.draw.circle(self.image, (0,0,0), (40,40), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (110,110), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (40,110), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (110,40), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (40,75), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (110,75), (15), (0))








## pygame.draw.circle(Surface, color, pos, radius, width=0): return Rect


class Label(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.font = pygame.font.SysFont("Arial", (30))
self.text = ""
self.center = (320, 240)

def update(self):
self.image = self.font.render(self.text, 1, (0, 0, 0))
self.rect = self.image.get_rect()
self.rect.center = self.center


def main():
pygame.display.set_caption("Rolling Dice With Voice")

background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((255, 255, 255))
screen.blit(background, (0,0))

label = Label()
labelEvent = Label()
die1 = Die(230, 150)
die2 = Die(420, 150)



number2 = pygame.mixer.Sound("two.ogg")
number3 = pygame.mixer.Sound("three.ogg")
number4 = pygame.mixer.Sound("four.ogg")
number5 = pygame.mixer.Sound("five.ogg")
number6 = pygame.mixer.Sound("six.ogg")
number7 = pygame.mixer.Sound("seven.ogg")
number8 = pygame.mixer.Sound("eight.ogg")
number9 = pygame.mixer.Sound("nine.ogg")
number10 = pygame.mixer.Sound("ten.ogg")
number11 = pygame.mixer.Sound("eleven.ogg")
number12 = pygame.mixer.Sound("twelve.ogg")




allSprites = pygame.sprite.Group(die1, die2, label, labelEvent)


label.text = "Click here to roll the die"
label.center = (320, 50)

clock = pygame.time.Clock()
keepGoing = True
while keepGoing:
clock.tick(30)
mouse = pygame.mouse.get_pos()
mouse_x = mouse[0]
mouse_y = mouse[1]
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
if event.type == pygame.MOUSEBUTTONDOWN:
if label.rect.collidepoint(mouse[0], mouse[1]):
die1.roll()
die2.roll()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
number2.play()

allSprites.clear(screen, background)
allSprites.update()
allSprites.draw(screen)

pygame.display.flip()

if __name__ == "__main__":
main()

pygame.quit()

[/Source]

Share this post


Link to post
Share on other sites
You're supposed to re-copy and paste the source in between [source][/source] tags. Like this:


import pygame, random
pygame.init()
pygame.mixer.init()

screen = pygame.display.set_mode((640, 480))

class Die(pygame.sprite.Sprite):
def __init__(self, die_x, die_y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((150, 150))
self.image.fill((160, 0, 160))
self.rect = self.image.get_rect()
self.rect.centerx = die_x
self.rect.centery = die_y

def roll(self):
dieroll = random.randint(1,6)
self.image.fill((160, 0, 160))
if dieroll == 1:
pygame.draw.circle(self.image, (0,0,0), (75,75), (15), (0))
if dieroll == 2:
pygame.draw.circle(self.image, (0,0,0), (40,40), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (110,110), (15), (0))
if dieroll == 3:
pygame.draw.circle(self.image, (0,0,0), (40,40), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (110,110), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (75,75), (15), (0))
if dieroll == 4:
pygame.draw.circle(self.image, (0,0,0), (40,40), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (40,110), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (110,40), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (110,110), (15), (0))
if dieroll == 5:
pygame.draw.circle(self.image, (0,0,0), (40,40), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (40,110), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (110,40), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (110,110), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (75,75), (15), (0))
if dieroll == 6:
pygame.draw.circle(self.image, (0,0,0), (40,40), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (110,110), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (40,110), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (110,40), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (40,75), (15), (0))
pygame.draw.circle(self.image, (0,0,0), (110,75), (15), (0))








## pygame.draw.circle(Surface, color, pos, radius, width=0): return Rect


class Label(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.font = pygame.font.SysFont("Arial", (30))
self.text = ""
self.center = (320, 240)

def update(self):
self.image = self.font.render(self.text, 1, (0, 0, 0))
self.rect = self.image.get_rect()
self.rect.center = self.center


def main():
pygame.display.set_caption("Rolling Dice With Voice")

background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((255, 255, 255))
screen.blit(background, (0,0))

label = Label()
labelEvent = Label()
die1 = Die(230, 150)
die2 = Die(420, 150)



number2 = pygame.mixer.Sound("two.ogg")
number3 = pygame.mixer.Sound("three.ogg")
number4 = pygame.mixer.Sound("four.ogg")
number5 = pygame.mixer.Sound("five.ogg")
number6 = pygame.mixer.Sound("six.ogg")
number7 = pygame.mixer.Sound("seven.ogg")
number8 = pygame.mixer.Sound("eight.ogg")
number9 = pygame.mixer.Sound("nine.ogg")
number10 = pygame.mixer.Sound("ten.ogg")
number11 = pygame.mixer.Sound("eleven.ogg")
number12 = pygame.mixer.Sound("twelve.ogg")




allSprites = pygame.sprite.Group(die1, die2, label, labelEvent)


label.text = "Click here to roll the die"
label.center = (320, 50)

clock = pygame.time.Clock()
keepGoing = True
while keepGoing:
clock.tick(30)
mouse = pygame.mouse.get_pos()
mouse_x = mouse[0]
mouse_y = mouse[1]
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
if event.type == pygame.MOUSEBUTTONDOWN:
if label.rect.collidepoint(mouse[0], mouse[1]):
die1.roll()
die2.roll()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
number2.play()

allSprites.clear(screen, background)
allSprites.update()
allSprites.draw(screen)

pygame.display.flip()

if __name__ == "__main__":
main()

pygame.quit()




I got this by going to "edit" your post (it won't let me actually submit an edit of course, but it shows the original text submitted for the post, including all whitespace). You can click "edit" on this post to see how I managed to type the tags above without them creating another source box. :)

Anyway. What you need to do is get the rolled numbers back from the die-rolling function, so that you can add them up and play the corresponding sound. In Python, as in almost every other commonly used programming language, the way you "get something back from" a function is to 'return' it. Then, you can use the call to the function in place of the value that will be returned. It works exactly like in mathematics: if we define f(x) = x^2 - 3x + 2, then we can write y = f(5) + 12, and we are claiming that y = 24. Similarly:


def roll(self):
dieroll = random.randint(1,6)
self.image.fill((160, 0, 160))
# all the drawing stuff from before... this is actually a horrible way
# do things, but let's take it one step at a time.
# Once we're done drawing, just return the value:
return dieroll


# Later on,
if label.rect.collidepoint(mouse[0], mouse[1]):
# This is the "similar" part.
result = die1.roll() + die2.roll()
# And then you use that result to pick the corresponding sound. Hint:
# put all those pygame.mixer.Sound objects into a dict, instead of having
# each in a separate variable. Use the number as the key and the Sound
# object as the value. Then just look up the Sound that corresponds to the
# 'result' value.


Share this post


Link to post
Share on other sites
Thank you very very much... That really did it for me. It all works very well

thank you to everyone who responded to this thread.

ill prob have another problem next week and i hope that you guys are as helpful then as you have been now

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
You can click "edit" on this post to see how I managed to type the tags above without them creating another source box. :)


<aside>Somewhat irritatingly, you can't – they're only escaped once, it appears. If anyone's interested: just replace one of the characters with its HTML escaped ASCII code – e.g. '&#101;' for the 'e'. Please note that you only want to do this when demonstrating how to use the tags, rather than actually use them yourself!</aside>

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement