Jump to content
  • Advertisement
Sign in to follow this  
baseballmike1124

python

This topic is 3872 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

Advertisement
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
Do it without classes, learn what classes are, then see how you fit your original solution into classes.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!