python

Started by
14 comments, last by TheUnbeliever 16 years, 1 month ago
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.
Advertisement
If you don't know what a class is why do you thing you need it?
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
Answers
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.
sorry...

yes, i am using pygame. I need to have two seperate die(sprites) get random numbers(1-6) when I click the mouse on a label.
Do it without classes, learn what classes are, then see how you fit your original solution into classes.
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()

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.
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()

This topic is closed to new replies.

Advertisement