Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygame-如何计算玩家';s点正确吗?_Python_Pygame - Fatal编程技术网

Python Pygame-如何计算玩家';s点正确吗?

Python Pygame-如何计算玩家';s点正确吗?,python,pygame,Python,Pygame,我正在为骰子游戏编写python/pygame代码。然而,我对球员的得分有疑问。我的代码无法正确计算玩家的点数。我真的需要有人帮我解决这个问题 这里是规则 球员积分=100分 如果玩家点数>=10,则玩家点数为-10(继续玩) 如果玩家积分

我正在为骰子游戏编写python/pygame代码。然而,我对球员的得分有疑问。我的代码无法正确计算玩家的点数。我真的需要有人帮我解决这个问题

这里是规则

  • 球员积分=100分
  • 如果玩家点数>=10,则玩家点数为-10(继续玩)
  • 如果玩家积分<10,“游戏结束”(游戏结束)
  • 一类五分:玩家积分+30
  • 一类四分:玩家积分+20
  • 三类:玩家积分+10
  • 满座:玩家积分+15
  • 直杆:球员积分+20
  • 两对:玩家积分+5
  • 无赢款:玩家积分+0
这是我的代码:

gDieMain

#gDieMain.py
import pygame, sys# Imports pygame package, Imports system package
from pygame.locals import * # You will have namespace shortcuts
from gDieClass import gDie
from gDiebuttonClass import simpleButton
from random import *

pygame.init() # Initializes pygame for the program and computer

# Global Variables
PPOINTS = 100
DIENUM = 10

# Color Value
GREY = (128, 128, 128)
GREEN = (0, 100, 0)
LTGREEN = (0, 255, 0)
DKGREEN = (174, 177, 120)
WHITE = (255, 255, 255)
BLACK = (40, 40, 40)
RED = (201, 0, 22)

# Button Value

SIZE = 100

hbXsize = 130
hbYsize = 30

hbBut1XPOS = 85
hbBut2XPOS = 285
hbBut3XPOS = 485
hbBut4XPOS = 685
hbBut5XPOS = 885

hbButYPOS = 215

#Set up the window
DISPLAYWIDTH = 1125
DISPLAYHEIGHT = 600

DISPLAYSURF = pygame.display.set_mode((1200, 600))
pygame.display.set_caption('DICE GAME GRAPHICS')
BGCOLOR = (0, 128, 0)
DISPLAYSURF.fill(BGCOLOR)

# Set up the Player's Points (unfinished)

#PCOLOR = (40, 40, 40)
#POINTFONT = pygame.font.SysFont("Times New Roman", 65) 

#PPFONT = pygame.font.SysFont("ScriptinaPro", 120)

#Button Layout
BUTTONWIDTH = 400
BUTTONHEIGHT = BUTTONWIDTH//7
BUTXPOS = (DISPLAYWIDTH - BUTTONWIDTH)//2
BUTYPOS = int(DISPLAYHEIGHT * .70)

HDISPLAYWIDTH = DISPLAYWIDTH//2
HDISPLAYHEIGHT = DISPLAYHEIGHT//2
CENTERBUTXPOS = HDISPLAYWIDTH - DISPLAYWIDTH//16
HSIZE = SIZE//2
GAP = (DISPLAYWIDTH//DIENUM - SIZE)//2

# pygame.font.SysFont()
# create a Font object from the system fonts
# SysFont(name, size, bold=False, italic=False) -> Font
myfont = pygame.font.SysFont("Times New Roman", 65)
#myfont1 = pygame.font.SysFont("Times New Roman", 65)
myfont2 = pygame.font.SysFont("Times New Roman", 35)

# Dice Layout
DIEYPOS  = DISPLAYHEIGHT//4
DIE1XPOS = HDISPLAYWIDTH - HSIZE - 2 * GAP - 2 * SIZE
DIE2XPOS = HDISPLAYWIDTH - HSIZE - GAP - SIZE
DIE3XPOS = HDISPLAYWIDTH - HSIZE
DIE4XPOS = HDISPLAYWIDTH + HSIZE + GAP
DIE5XPOS = HDISPLAYWIDTH + HSIZE + 2 * GAP + SIZE

# Dice Position
position1 = (100, 100)
position2 = (300, 100)
position3 = (500, 100)
position4 = (700, 100)
position5 = (900, 100)

# Font Position
myfontPosition1 = (375, 20)
myfontPosition2 = (100, 500)
myfontPosition3 = (240, 260)

# Instantiate Objects
d1 = gDie(SIZE, position1)
d2 = gDie(SIZE, position2)
d3 = gDie(SIZE, position3)
d4 = gDie(SIZE, position4)
d5 = gDie(SIZE, position5)

#Make die object list

b1 = simpleButton(hbXsize, hbYsize, GREEN, LTGREEN, 'Dice 1',
                  DISPLAYSURF, (hbBut1XPOS, hbButYPOS))
b2 = simpleButton(hbXsize, hbYsize, GREEN, LTGREEN, 'Dice 2',
                  DISPLAYSURF, (hbBut2XPOS, hbButYPOS))
b3 = simpleButton(hbXsize, hbYsize, GREEN, LTGREEN, 'Dice 3',
                  DISPLAYSURF, (hbBut3XPOS, hbButYPOS))
b4 = simpleButton(hbXsize, hbYsize, GREEN, LTGREEN, 'Dice 4',
                  DISPLAYSURF, (hbBut4XPOS, hbButYPOS))
b5 = simpleButton(hbXsize, hbYsize, GREEN, LTGREEN, 'Dice 5',
                  DISPLAYSURF, (hbBut5XPOS, hbButYPOS))

# Instantiate Roll Buttons

firstRoll = simpleButton(BUTTONWIDTH, BUTTONHEIGHT, GREEN, LTGREEN,
                         'First Roll (10 pts)', DISPLAYSURF, (BUTXPOS, BUTYPOS))
secondRoll = simpleButton(BUTTONWIDTH, BUTTONHEIGHT, GREEN, LTGREEN,
                         'Second Roll', DISPLAYSURF, (BUTXPOS, BUTYPOS))
thirdRoll = simpleButton(BUTTONWIDTH, BUTTONHEIGHT, GREEN, LTGREEN,
                         'Last Roll', DISPLAYSURF, (BUTXPOS, BUTYPOS))

# Text label

#textLabel = simpleButton(600, BUTTONHEIGHT, DKGREEN, LTGREEN,
#                         '', DISPLAYSURF, (250, 300))

textLabel = simpleButton(600, BUTTONHEIGHT, BLACK, LTGREEN,
                         '', DISPLAYSURF, (250, 320))

# pygame.font.SysFont()
# create a Font object from the system fonts
# SysFont(name, size, bold=False, italic=False) -> Font
#myfont = pygame.font.SysFont("Sylfaen", 65)

# render text
# render()
# draw text on a new Surface
# render(text, antialias, color, background=None) -> Surface
#label = myfont.render("DICE POKER SIM!", 0, LTGREEN)


# Assign Objects Values

def setValue(dieList):

    newDieList = []

    for x in dieList:        
        if not x.get_hold():
            dieNumber = randint(1, 6)
            x.setValue(dieNumber)
        newDieList.append(x.VALUE)

    return newDieList

def displayAllDie(surf):
    d1.displayDie(surf, position1[0], position1[1])
    d2.displayDie(surf, position2[0], position2[1])
    d3.displayDie(surf, position3[0], position3[1])
    d4.displayDie(surf, position4[0], position4[1])
    d5.displayDie(surf, position5[0], position5[1])

def displayButtons():
    firstRoll.displayBut()

def displayHoldButtons():
    b1.displayBut()
    b2.displayBut()
    b3.displayBut()
    b4.displayBut()
    b5.displayBut()

def score(dice):

    global PPOINTS

    counts = [0] * 7

    for value in dice:
        counts[value] += 1

    if PPOINTS >= 10:
        PPOINTS -= 10

    if 5 in counts:
        PPOINTS += 30
        return "Five of a kind: ", "You win 30 points ", PPOINTS

    elif 4 in counts:
        PPOINTS += 20
        return "Four of a kind: ", "You win 20 points ", PPOINTS

    elif (3 in counts) and (2 in counts):
        PPOINTS += 15
        return "Full House: ", "You win 15 points ", PPOINTS

    elif 3 in counts:
        PPOINTS += 10
        return "Three of Kind: ", "You win 10 points ", PPOINTS

    elif not (2 in counts) and (counts[1] == 0 or
                                counts[6] == 0):
        PPOINTS += 25
        return "Straight: ", "You win 25 points ", PPOINTS

    elif counts.count(2) == 2:
        PPOINTS += 5
        return "Two Pairs: ", "You win 5 points ", PPOINTS  

    else:
        PPOINTS += 0
        return "No winning: ", "You win 0 points ", PPOINTS


def stillText(dieList, surf):
    a = setValue(dieList) 
    displayAllDie(surf)
    return a

def allDieHold(dieList):
    for x in dieList:
        x.HOLD = False

def allDieHit(dieList):
    for x in dieList:
        x.HOLD = True 


dieList = [d1, d2, d3, d4, d5]  

setValue(dieList)

def main():

    setValue(dieList)
    displayButtons()
    allDieHit(dieList)
    allDieHold(dieList)
    handresult = ""
    handscore = ""
    PPOINTS = ""

    keepPlaying = True

    while keepPlaying: #Main Game Loop

        for event in pygame.event.get():
            if event.type == QUIT: # Exit the game
                pygame.quit()
                sys.exit()
            elif event.type == KEYDOWN: # Exit the game
                if event.key == K_ESCAPE:
                    pygame.quit()
                    return          

            if event.type == MOUSEBUTTONDOWN:
                mouseXY = pygame.mouse.get_pos()
                if firstRoll.clicked(mouseXY):
                    firstRoll.display_HILIGHTED()
                elif secondRoll.clicked(mouseXY):
                    secondRoll.display_HILIGHTED()
                elif thirdRoll.clicked(mouseXY):
                    thirdRoll.display_HILIGHTED()

                elif d1.clicked(mouseXY):
                    d1.switch_hold()
                elif d2.clicked(mouseXY):
                    d2.switch_hold()
                elif d3.clicked(mouseXY):
                    d3.switch_hold()
                elif d4.clicked(mouseXY):
                    d4.switch_hold()
                elif d5.clicked(mouseXY):
                    d5.switch_hold()                    

            if event.type == MOUSEBUTTONUP:
                if firstRoll.clicked(mouseXY):
                    DISPLAYSURF.fill(BGCOLOR)
                    setValue(dieList)
                    allDieHit(dieList)
                    allDieHold(dieList)                    
                    stillText(dieList, DISPLAYSURF)
                    secondRoll.displayBut()
                    firstRoll.inActive()
                    secondRoll.active()
                    #thirdRoll.inActive()
                    #displayHoldButtons() 
                elif secondRoll.clicked(mouseXY):
                    stillText(dieList, DISPLAYSURF)
                    thirdRoll.displayBut()
                    secondRoll.inActive()
                    thirdRoll.active()    
                elif thirdRoll.clicked(mouseXY):
                    a = stillText(dieList, DISPLAYSURF)
                    firstRoll.displayBut()
                    thirdRoll.inActive()
                    firstRoll.active()
                    handresult, handscore, PPOINTS= score(a)
                    print(score(a))


        b1.displayBut()
        b2.displayBut()
        b3.displayBut()
        b4.displayBut()
        b5.displayBut()
        textLabel.LABEL = handresult + str(handscore) + str(PPOINTS)
        textLabel.displayBut()

        DISPLAYSURF.blit(myfont.render('DICE GAME', True, LTGREEN), myfontPosition1)
        #DISPLAYSURF.blit(myfont1.render("Player's Points: 100", True, LTGREEN), myfontPosition2)
        DISPLAYSURF.blit(myfont2.render("Click on Dice: Red to Hold and White to Roll", True, LTGREEN), myfontPosition3)
        # render()
        # draw text on a new Surface
        # render(text, antialias, color, background=None) -> Surface

        displayAllDie(DISPLAYSURF)

        pygame.display.update()

main()
gDieClass

#gDieClass.py
import pygame
from pygame.locals import *

class gDie:

#class that displays a grahical rep. of 6 sided die
def __init__(self, size, pos):

    # Globals      

    #define some values
    self.DIESURF = pygame.Surface((size, size), flags=SRCALPHA, depth=32)
    self.DIESURF.fill((0, 0, 0, 0))

    #Color
    self.white = (255, 255, 255)
    self.BLACK = (40, 40, 40)
    self.RED = (201, 0, 22)

    # Information about the Dice
    self.POS = pos
    self.HEIGHT = size
    self.WIDTH  = size

    self.HOLD = False
    self.VALUE = 0       

    #SIZE and RADIUS
    self.SIZE = size
    self.RADIUS = self.SIZE//10
    HSIZE = self.SIZE//2
    QSIZE = self.SIZE//4

    #create Pips/Dots in standard places

    self.POINT1 = (HSIZE, HSIZE)
    self.POINT2 = (QSIZE, QSIZE)
    self.POINT3 = (HSIZE + QSIZE, HSIZE + QSIZE)
    self.POINT4 = (HSIZE + QSIZE, QSIZE)
    self.POINT5 = (QSIZE, HSIZE + QSIZE)
    self.POINT6 = (HSIZE + QSIZE, HSIZE)
    self.POINT7 = (QSIZE, HSIZE)

def __drawBackground(self):

    #create square with rounded corners for dice     

    if self.HOLD:
        color = self.RED
    else:
        color = self.white 

    pygame.draw.circle(self.DIESURF, color, (self.RADIUS, self.RADIUS), self.RADIUS)
    pygame.draw.circle(self.DIESURF, color, (self.SIZE - self.RADIUS, self.SIZE - self.RADIUS), self.RADIUS)
    pygame.draw.circle(self.DIESURF, color, (self.SIZE - self.RADIUS, self.RADIUS), self.RADIUS)
    pygame.draw.circle(self.DIESURF, color, (self.RADIUS, self.SIZE - self.RADIUS), self.RADIUS)

    pygame.draw.rect(self.DIESURF, color, Rect((self.RADIUS, 0), (self.SIZE - (2 * self.RADIUS), self.SIZE)))
    pygame.draw.rect(self.DIESURF, color, Rect((0, self.RADIUS), (self.SIZE, self.SIZE - (2 * self.RADIUS))))

def __makePip(self, point): 

    # function to make pips  
    if self.HOLD:
        pygame.draw.circle(self.DIESURF, self.white, point, self.RADIUS)
    else:
        pygame.draw.circle(self.DIESURF, self.BLACK, point, self.RADIUS)    


def switch_hold(self):
    #self.HOLD = True
    self.HOLD = not self.HOLD
    return self.HOLD

def get_hold(self):
    return self.HOLD

def clicked(self, MOUSEXY):
    yesORno = False
    P1 = self.POS
    P2 = (P1[0] + self.WIDTH, P1[1] + self.HEIGHT)
    yesORno = (P1[0] <= MOUSEXY[0] <= P2[0] and
               P1[1] <= MOUSEXY[1] <= P2[1])

    return yesORno

def setValue(self, value):

    # create Die Background
    self.__drawBackground()

    self.VALUE = value

    # create PIPs at value
    if value == 1:
        pip1 = self.__makePip(self.POINT1)

    elif value == 2:
        pip2 = self.__makePip(self.POINT2)
        pip3 = self.__makePip(self.POINT3)

    elif value == 3:
        pip1 = self.__makePip(self.POINT1)
        pip2 = self.__makePip(self.POINT2)
        pip3 = self.__makePip(self.POINT3)

    elif value == 4:
        pip2 = self.__makePip(self.POINT2)
        pip3 = self.__makePip(self.POINT3)
        pip4 = self.__makePip(self.POINT4)
        pip5 = self.__makePip(self.POINT5)

    elif value == 5:
        pip1 = self.__makePip(self.POINT1)
        pip2 = self.__makePip(self.POINT2)
        pip3 = self.__makePip(self.POINT3)
        pip4 = self.__makePip(self.POINT4)
        pip5 = self.__makePip(self.POINT5)

    elif value == 6:
        pip2 = self.__makePip(self.POINT2)
        pip3 = self.__makePip(self.POINT3)
        pip4 = self.__makePip(self.POINT4)
        pip5 = self.__makePip(self.POINT5)
        pip6 = self.__makePip(self.POINT6)
        pip7 = self.__makePip(self.POINT7)

def displayDie(self, surf, x, y):
    self.setValue(self.VALUE)
    surf.blit(self.DIESURF, (x, y))
import pygame
from pygame.locals import *

class simpleButton:

# class that creates button objects

def __init__(self, width, height, color, textColor, label, surf, position):

    # define and assign some self values

    self.ACTIVE = True
    self.HILIGHTED = False
    self.LABEL = label
    self.SURF = surf
    self.POS = position
    self.BUTCOLOR  = color
    self.TEXTCOLOR = textColor

    # Generate a greyed-out version of color and a highlighted version of color
    self.BUTGREYED   = (color[0] * .25, color[1] * .25, color[2] * .25)
    self.HIGHLIGHTCOLOR = (color[0] + ((255 - color[0])//2),
                           color[1] + ((255 - color[1])//2),
                           color[2] + ((255 - color[2])//2))

    # Assign and calulate some size values 
    self.HEIGHT   = height
    self.WIDTH    = width
    self.HWIDTH   = self.WIDTH//2
    self.RADIUS   = self.HEIGHT//2
    self.THEIGHT  = int(self.HEIGHT * .60)

    # Create BUTTONSURF
    self.BUTTONSURF = pygame.Surface((self.WIDTH, self.HEIGHT), flags=SRCALPHA, depth=32)
    self.BUTTONSURF.fill((0, 0, 0, 0))


def __buttonBG(self, color):  # draws the button shape

    # Helper Method to create button background
    # create square with rounded corners

    pygame.draw.circle(self.BUTTONSURF, color, (self.RADIUS, self.RADIUS),
                       self.RADIUS)
    pygame.draw.circle(self.BUTTONSURF, color,
                       (self.WIDTH - self.RADIUS, self.RADIUS), self.RADIUS)

    pygame.draw.rect(self.BUTTONSURF, color,
                     Rect((self.RADIUS, 0), (self.WIDTH - 2 * self.RADIUS,
                                             self.HEIGHT)))

def __buttonText(self): # places text surface on the button surface

    # helper funtion to make text surface and blit on BUTTONSURF

    # Set up the Font Object and how to Change Fonts
    BUTFONT = pygame.font.SysFont("Sylfaen", self.THEIGHT)

    # Render a Text Surface
    self.TEXTSURF = BUTFONT.render(self.LABEL, True, self.TEXTCOLOR, None)
    w, h   = self.TEXTSURF.get_size()
    XPOS = (self.WIDTH - w)//2
    YPOS = (self.HEIGHT - h)//2

    # Draw Text
    self.BUTTONSURF.blit(self.TEXTSURF, (XPOS, YPOS))

def clicked(self, MOUSEXY):
    yesORno = False
    P1 = self.POS
    P2 = (P1[0] + self.WIDTH, P1[1] + self.HEIGHT)
    yesORno = (self.ACTIVE and P1[0] <= MOUSEXY[0] <= P2[0] and
               P1[1] <= MOUSEXY[1] <= P2[1])

    return yesORno

def active(self):
    self.ACTIVE = True
    return True

def inActive(self):
    self.ACTIVE = False
    return False

def changePos(self, X, Y):
    self.POS = (X, Y)
    return self.POS

def display_HILIGHTED(self):
    self.__buttonBG(self.HIGHLIGHTCOLOR)
    self.__buttonText()
    self.SURF.blit(self.BUTTONSURF, self.POS)

def displayBut(self):   
    self.__buttonBG(self.BUTCOLOR)
    self.__buttonText()
    self.SURF.blit(self.BUTTONSURF, self.POS)
#gDieClass.py
导入pygame
从pygame.locals导入*
gDie类:
#类,该类显示六面模具的总体代表
定义初始值(自身、大小、位置):
#全球的
#定义一些值
self.DIESURF=pygame.Surface((大小,大小),flags=SRCALPHA,depth=32)
自表面填充((0,0,0,0))
#颜色
self.white=(255、255、255)
self.BLACK=(40,40,40)
self.RED=(201,0,22)
#有关骰子的信息
self.POS=POS
自我高度=尺寸
self.WIDTH=大小
self.HOLD=False
self.VALUE=0
#尺寸和半径
self.SIZE=大小
self.RADIUS=self.SIZE//10
HSIZE=self.SIZE//2
QSIZE=self.SIZE//4
#在标准位置创建点/点
self.POINT1=(HSIZE,HSIZE)
self.POINT2=(QSIZE,QSIZE)
self.POINT3=(HSIZE+QSIZE,HSIZE+QSIZE)
self.POINT4=(HSIZE+QSIZE,QSIZE)
self.POINT5=(QSIZE,HSIZE+QSIZE)
self.POINT6=(HSIZE+QSIZE,HSIZE)
self.POINT7=(QSIZE,HSIZE)
def牵引杆接地(自):
#为骰子创建带有圆角的正方形
如果自我保持:
颜色=self.RED
其他:
颜色=白色
pygame.draw.circle(self.DIESURF,color,(self.RADIUS,self.RADIUS),self.RADIUS)
pygame.draw.circle(self.DIESURF,color,(self.SIZE-self.RADIUS,self.SIZE-self.RADIUS),self.RADIUS)
pygame.draw.circle(self.DIESURF,color,(self.SIZE-self.RADIUS,self.RADIUS),self.RADIUS)
pygame.draw.circle(self.DIESURF,color,(self.RADIUS,self.SIZE-self.RADIUS),self.RADIUS)
pygame.draw.rect(self.DIESURF,color,rect((self.RADIUS,0),(self.SIZE-(2*self.RADIUS),self.SIZE)))
pygame.draw.rect(self.DIESURF,color,rect((0,self.RADIUS),(self.SIZE,self.SIZE-(2*self.RADIUS)))
def__makePip(自身,点):
#制作pip的函数
如果自我保持:
pygame.draw.circle(self.DIESURF、self.white、point、self.RADIUS)
其他:
pygame.draw.circle(self.DIESURF、self.BLACK、point、self.RADIUS)
def开关保持(自):
#self.HOLD=True
self.HOLD=不self.HOLD
自持
def get_hold(自身):
自持
已单击def(自身、鼠标):
yesORno=False
P1=self.POS
P2=(P1[0]+自宽,P1[1]+自高)

yesORno=(P1[0]您调用了两次
score(a)
,因此会添加两次积分

handresult, handscore, PPOINTS = score(a) # first time
print(score(a)) # second time
应该是

handresult, handscore, PPOINTS = score(a)
print(handresult, handscore, PPOINTS)

你说它不起作用,但它在做什么?它会产生错误,每次都返回相同的结果吗?代码为玩家的分数生成错误的结果。解释“错误结果”-显示“错误结果”和“预期结果”两次添加分数如何
handsult,handscore,PPOINTS=score(a);如果点数小于10:keepplay=False