Python Forum
[PyGame] another dice roll!
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] another dice roll!
#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


Messages In This Thread
another dice roll! - by mzmingle - Nov-28-2017, 02:14 PM
RE: another dice roll! - by Windspar - Nov-28-2017, 02:30 PM
RE: another dice roll! - by metulburr - Nov-28-2017, 03:55 PM
RE: another dice roll! - by Windspar - Nov-28-2017, 04:47 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] dice roll? mzmingle 1 8,184 Nov-27-2017, 11:58 AM
Last Post: Mekire
  [PyGame] ROLL function Zman350x 4 6,059 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