我将如何多次显示?

2024-09-29 17:14:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个骰子游戏,它要求我随机显示6个骰子。我遇到的问题是,每当我按下按钮执行代码时,它只会显示一次随机选择的骰子,即使我告诉它选择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)

Tags: imageselfsizeifshowloadval骰子
1条回答
网友
1楼 · 发布于 2024-09-29 17:14:53

如果die1、die2等每一个都代表一个die,那么您只显示其中一个代码,请尝试

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

相关问题 更多 >

    热门问题