Python Forum
[PyGame] another dice roll!
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] another dice roll!
#1
My friend wrote a program that displays an amount of dots (like a dice would), and pauses when you click. I tried to edit this code to use images - but it isn't working. I'm getting an error about putting the images on the screen, even though I've been able to do it in previous programs. Any help? :)

import pygame, sys, random, time
pygame.init()

done = False

size = 500, 500
screen = pygame.display.set_mode(size)

WHITE = 255, 255, 255
coords = 225, 255

one = pygame.image.load('one.png').convert
two = pygame.image.load('two.png').convert
three = pygame.image.load('three.png').convert
four = pygame.image.load('four.png').convert
five = pygame.image.load('five.png').convert
six = pygame.image.load('six.png').convert

def one():
    screen.blit(one, coords)

def two():
    screen.blit(two, coords)

def three():
    screen.blit(three, coords)

def four():
    screen.blit(four, coords)

def five():
    screen.blit(five, coords)

def six():
    screen.blit(six, coords)


while done == False:

    rolled = False

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
            pygame.quit()

    while not rolled:
        screen.fill(WHITE)
        random.choice([one, two, three, four, five, six])()
        pygame.display.flip()
        time.sleep(0.18)

        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN:
                rolled = True
                screen.fill(WHITE)
                random.choice([one, two, three, four, five, six])()
                pygame.display.flip()
                time.sleep(2.5)
Error:
Traceback (most recent call last): File "H:/CODE/dice/dice code 2.py", line 49, in <module> screen.blit(one, coords) TypeError: argument 1 must be pygame.Surface, not function
Reply
#2
1. your surfaces where rewritten as functions . They have the same name.

2. Never use time.sleep. It stop the world.
Use pygame.time.set_timer or pygame.time.get_ticks()
99 percent of computer problems exists between chair and keyboard.
Reply
#3
Quote:one = pygame.image.load('one.png').convert
two = pygame.image.load('two.png').convert
three = pygame.image.load('three.png').convert
four = pygame.image.load('four.png').convert
five = pygame.image.load('five.png').convert
six = pygame.image.load('six.png').convert

convert needs to be convert() otherwsie you are just passing that function to variables one, two, etc. When it blits it creates an error because one is now pygame.image.load('two.png').convert (the function convert)

one = pygame.image.load('one.png').convert()
Recommended Tutorials:
Reply
#4
Yep miss that one.  I saw this.
Quote:one = pygame.image.load(one.png).covert

def one():
    screen.blit(one, coords)

1. You should only have one pygame.display.flip() in a loop.

2. You should have most of the game in a function.

3. pygame.time.Clock to control framerate or pygame.time.delay

My example. Might be a little overkill but i like it.
import pygame
from random import choice

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def tup(self):
        return int(self.x), int(self.y)

    # overload handle Point, tuple, list, single number
    def __add__(self, p):
        if type(p) == Point:
            return Point(self.x + p.x, self.y + p.y)
        elif type(p) == tuple or type(p) == list:
            return Point(self.x + p[0], self.y + p[1])
        return Point(self.x + p, self.y + p)

    def __sub__(self, p):
        if type(p) == Point:
            return Point(self.x - p.x, self.y - p.y)
        elif type(p) == tuple or type(p) == list:
            return Point(self.x - p[0], self.y - p[1])
        return Point(self.x - p, self.y - p)

    def __mul__(self, p):
        if type(p) == Point:
            return Point(self.x * p.x, self.y * p.y)
        elif type(p) == tuple or type(p) == list:
            return Point(self.x * p[0], self.y * p[1])
        return Point(self.x * p, self.y * p)

    def __truediv__(self, p):
        if type(p) == Point:
            return Point(self.x / p.x, self.y / p.y)
        elif type(p) == tuple or type(p) == list:
            return Point(self.x / p[0], self.y / p[1])
        return Point(self.x / p, self.y / p)

def create_dice():
    dice_image = []
    size = Point(30, 30)
    for x in range(1,7):
        surface = pygame.Surface(size.tup())
        surface.fill(pygame.color.Color('white'))

        if x == 1:
            pos = size / 2
            pygame.draw.circle(surface, pygame.color.Color('black'), pos.tup(), 3)
        elif x == 2:
            pos = size / 4
            pygame.draw.circle(surface, pygame.color.Color('black'), pos.tup(), 3)
            pos = pos * 3
            pygame.draw.circle(surface, pygame.color.Color('black'), pos.tup(), 3)
        elif x == 3:
            pos = size / 4
            pygame.draw.circle(surface, pygame.color.Color('black'), pos.tup(), 3)
            pos = pos * 3
            pygame.draw.circle(surface, pygame.color.Color('black'), pos.tup(), 3)
            pos = size / 2
            pygame.draw.circle(surface, pygame.color.Color('black'), pos.tup(), 3)
        elif x == 4 or x == 5:
            pos = size / 4
            pygame.draw.circle(surface, pygame.color.Color('black'), pos.tup(), 3)
            pos = pos * (3, 1)
            pygame.draw.circle(surface, pygame.color.Color('black'), pos.tup(), 3)
            pos = size / (4, 4 / 3) # size.y / 4 * 3 = size.y / (4 / 3)
            pygame.draw.circle(surface, pygame.color.Color('black'), pos.tup(), 3)
            pos = pos * (3, 1)
            pygame.draw.circle(surface, pygame.color.Color('black'), pos.tup(), 3)

            if x == 5:
                pos = size / 2
                pygame.draw.circle(surface, pygame.color.Color('black'), pos.tup(), 3)

        elif x == 6:
            pos = size / 4
            pygame.draw.circle(surface, pygame.color.Color('black'), pos.tup(), 3)
            pos = pos * (1, 3)
            pygame.draw.circle(surface, pygame.color.Color('black'), pos.tup(), 3)
            pos = size / (4, 2)
            pygame.draw.circle(surface, pygame.color.Color('black'), pos.tup(), 3)
            pos = pos * (3, 1)
            pygame.draw.circle(surface, pygame.color.Color('black'), pos.tup(), 3)
            pos = size / (4 / 3, 4)
            pygame.draw.circle(surface, pygame.color.Color('black'), pos.tup(), 3)
            pos = pos * (1, 3)
            pygame.draw.circle(surface, pygame.color.Color('black'), pos.tup(), 3)

        dice_image.append(surface)
    return dice_image

def game():
    pygame.init()
    pygame.display.set_caption("Dice Game")
    screen = pygame.display.set_mode((800, 600))
    clock = pygame.time.Clock()
    dice_images = create_dice()

    dice_roll = {
        'roll': False,
        'image': choice(dice_images),
        'next_tick': None,
        'flip': 0
    }

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    running = False
                elif event.key == pygame.K_SPACE:
                    dice_roll['roll'] = True
                    dice_roll['next_tick'] = None

            elif event.type == pygame.QUIT:
                running = False

        screen.fill(pygame.color.Color('black'))
        for x in range(6):
            screen.blit(dice_images[x], (50 + 50 * x, 100))

        ticks = pygame.time.get_ticks()
        if dice_roll['roll']:
            if dice_roll['next_tick'] is None or dice_roll['next_tick'] < ticks:
                if dice_roll['next_tick'] is None:
                    dice_roll['next_tick'] = ticks + 200
                else:
                    dice_roll['next_tick'] += 200

                dice_roll['image'] = choice(dice_images)
                dice_roll['flip'] += 1
                if dice_roll['flip'] > 6:
                    dice_roll['roll'] = False
                    dice_roll['flip'] = 0

        screen.blit(dice_roll['image'], (100, 150))
        pygame.display.flip()
        clock.tick(30)

    pygame.quit()

if __name__ == '__main__':
    game()
99 percent of computer problems exists between chair and keyboard.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] dice roll? mzmingle 1 7,214 Nov-27-2017, 11:58 AM
Last Post: Mekire
  [PyGame] ROLL function Zman350x 4 4,975 Aug-03-2017, 02:55 PM
Last Post: Zman350x

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020