我怎么会多次显示?

时间:2016-02-24 23:20:32

标签: python pygame

我正在制作骰子游戏,这需要我随机显示6个骰子。我得到的问题是,当我按下按钮执行代码时,它只会显示随机选择的骰子ONCE,即使我告诉它选择6个随机骰子。我的问题是:我如何在代码中重复数字,以便在多次选择相同的模具(单个模具)时显示骰子?

   if val == 1:
        show.blit(die1, (150,50))          
    elif val == 2:
        show.blit(die2, (250,50))
    elif val == 3:
        show.blit(die3, (150,150))
    elif val == 4:
        show.blit(die4, (250,150))
    elif val == 5:
        show.blit(die5, (150,250))
    elif val == 6:
        show.blit(die6, (250,250))

以下是此代码的其余代码:

## gDieClass.py

import pygame
from pygame.locals import *
from random import randint

show = pygame.display.set_mode((500,500))
die1 = pygame.image.load('Dice1.tga')
die2 = pygame.image.load('Dice2.tga')
die3 = pygame.image.load('Dice3.tga')
die4 = pygame.image.load('Dice4.tga')
die5 = pygame.image.load('Dice5.tga')
die6 = pygame.image.load('Dice6.tga')


class graphic_die(object):

    # class that displays a graphical rep. of 6 sided die

    def __init__(self, size, surf, pos):

        # define some values
        self.SURF = surf
        self.POS = pos
        self.SIZE   = size


        self.__DIESURF = pygame.Surface((size, size), flags=SRCALPHA, depth=32)
        self.__DIESURF.fill((0, 0, 0, 0))

        self.value = 1

        ## Dice Colors
        self.die_color  = (0,0,0,0)

        self.__RADIUS = self.SIZE//10
        __HSIZE  = self.SIZE//2
        __QSIZE  = self.SIZE//4
        self.PIPRAD = self.SIZE//10

        # create Pips/Dots in standard places

    def __drawBackground(self):

        # create square with rounded corners
        pygame.draw.circle(self.__DIESURF, self.die_color, (self.__RADIUS, self.__RADIUS), self.__RADIUS)
        pygame.draw.circle(self.__DIESURF, self.die_color, (self.SIZE - self.__RADIUS, self.__RADIUS), self.__RADIUS)
        pygame.draw.circle(self.__DIESURF, self.die_color, (self.__RADIUS, self.SIZE - self.__RADIUS), self.__RADIUS)
        pygame.draw.circle(self.__DIESURF, self.die_color, (self.SIZE - self.__RADIUS, self.SIZE - self.__RADIUS), self.__RADIUS)

        pygame.draw.rect(self.__DIESURF, self.die_color, Rect((self.__RADIUS, 0), (self.SIZE - 2 * self.__RADIUS, self.SIZE)))
        pygame.draw.rect(self.__DIESURF, self.die_color, Rect((0, self.__RADIUS), (self.SIZE, self.SIZE - 2 * self.__RADIUS))) 

    def __makePip(self, point):

        # helper function to make pips
        pygame.draw.circle(self.__DIESURF, self.pip_color, point, self.PIPRAD)            

    def setRandValue(self):
        self.value = randint(1, 6)

    def __drawValue(self, val):

        self.__drawBackground()

        # Creates PIPs at Value
        if val == 1:
            show.blit(die1, (150,50))          
        if val >= 2:
            show.blit(die2, (250,50))
        if val >= 3:
            show.blit(die3, (150,150))
        if val >= 4:
            show.blit(die4, (250,150))
        if val >= 5:
            show.blit(die5, (150,250))
        if val >= 6:
            show.blit(die6, (250,250))

    def display(self):
            self.__drawValue(self.value)
            self.SURF.blit(self.__DIESURF, self.POS)

1 个答案:

答案 0 :(得分:0)

如果die1,die2等每个都代表一个骰子,那么你只展示那个代码中的一个,试试

if val >= 1:
    show.blit(die1, (150,50))          
if val >= 2:
    show.blit(die2, (250,50))
if val >= 3:
    show.blit(die3, (150,150))
if val >= 4:
    show.blit(die4, (250,150))
if val >= 5:
    show.blit(die5, (150,250))
if val == 6:
    show.blit(die6, (250,250))