samedi 6 novembre 2021

Redrawing Board Game Pieces in Pygame

Goal: At This point I'm just trying to get the Player 1 piece to move along the top row left to right and as if reaches the end of the row, move down the last column. I'm looking for a way to move the pieces 1 block at a time until it has moved a total of dice times.

Current output: The Player 1 piece is only drawn on the top row and as the dice is rolled, the number of pieces drawn on the row changes, but wont advance further up the board.

I've managed to get the pieces moving on a previous program I made, however it was extremely unorganized so in this game I've learned to create functions. As a result I'm having trouble recreating even the old programs movement.

Current Program Being Worked On:

import pygame
import sys
import random

pygame.init()

def player(window, dice, pos1X,pos1Y,pos2X,pos2Y):
    
    for _ in range(dice):
        if pos1X < 525:
            pos1X += 100
        elif pos1X >= 525:
            pos1Y +=100
        pygame.draw.circle(window,(255,0,0),(pos1X,pos1Y),20)
        pygame.draw.circle(window,(255,0,255),(pos2X,pos2Y),20)
        window.blit(window,(0,0))
        pygame.display.update()

def board(window, window_width, window_length):
    w = 35
    h = 10
    font = pygame.font.SysFont("Times New Roman, Arial", 20)
    
    for y in range (window_length):
        pygame.draw.line(window,(255,255,255),(1, y*100),(window_width,y*100))
        for x in range (window_width):
            pygame.draw.line(window,(255,255,255),(x*100,1),(x*100,window_length))
            
def quit_message():
    big_font = pygame.font.SysFont("Times New Roman, Arial", 100)
    quit_text = big_font.render("You Quit:/", True, (255,0,255))
    window_scrn.blit(quit_text,(window_width//16,window_length//3))

def screen():
    window_width = 600
    window_length = 700
    window = pygame.display.set_mode((window_width,window_length))
    play = True
    pos1X = 25
    pos1Y = 25
    pos2X = 65
    pos2Y = 65
    while(play):
        board(window, window_width, window_length)
        roll = input("Ready To Roll The Dice?")
        if roll.upper() == "YES":
            dice = random.randint(1,5)
            player(window, dice, pos1X,pos1Y,pos2X,pos2Y)
            window.fill((0,0,0))
        elif roll.upper() == "NO":
            play = False
        

def start():
    valid = True
    while(valid):
        play = input("Do You Want To Play A Game?: ")
        if play.upper() == "YES":
            screen()
        elif play.upper() == "NO":
            print("Have A Nice Day. Goodbye")
            valid = False
            exit()
        else:
            print("Invalid Entry, Try Again")

def _main_():
    start()
    
    
if _main_ == _main_:
    _main_()
    
#Exit Game
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

Old Program Where The Pieces Moved Successfully:

import pygame
import random
import sys

pygame.init()

window_length = 475
window_width = 475
window_scrn = pygame.display.set_mode((window_width, window_length))

play = True

pos1X = 7.5
pos1Y = 7.5

pos2X = 17.5
pos2Y = 17.5
player1Active = True
player2Active = False
w = 7
h = 7
font = pygame.font.SysFont("Times New Roman, Arial", 20)
big_font = pygame.font.SysFont("Times New Roman, Arial", 100)


while(play):
    #Set Player Positions
    player1_pos = (pos1X,pos1Y)
    player2_pos = (pos2X,pos2Y)
    
    #Draw Players
    pygame.draw.circle(window_scrn,(255,0,0),player1_pos,5)
    pygame.draw.circle(window_scrn,(0,255,50),player2_pos,5)
    
    #Draw Board
    for y in range (window_length):
        pygame.draw.line(window_scrn,(255,255,255),(1, y*25),(window_width,y*25))
        for x in range (window_width):
            pygame.draw.line(window_scrn,(255,255,255),(x*25,1),(x*25,window_length))
    pygame.draw.polygon(window_scrn,(255,255,0),((25,25),(450,25),(450,450),(25,450)))
    pygame.draw.rect(window_scrn,(255,0,255),(0,25,25,25))
    if (player1_pos == (7.5,32.5)):
        print("Player 1 Wins!")
        play = False
    elif (player2_pos == (17.5,42.5)):
        print("Player 2 Wins!")
        play = False
    
    #Label Squares
    for number in range(1,(window_length//25)+1):
        text = font.render(str(number), True, (255,255,255))
        window_scrn.blit(text,(w,h))
        w += 25
    #Roll Dice
    if player1Active == True:
        cont = input("Player 1! Want To Roll The Dice?: ").upper()
    elif player2Active == True:
        cont = input("Player 2! Want To Roll The Dice?: ").upper()
    
    if cont == "YES":
        dice = random.randint(1,6)
        print(f"Rolling Dice!")
        print(dice)
    elif cont == "NO":
        play = False
        quit_text = big_font.render("You Quit:/", True, (255,0,255))
        window_scrn.blit(quit_text,(window_width//16,window_length//3))
        exit()
    
    

    #Move Players
    if player1Active == True:
        for _ in range(dice):
            if(pos1X < 457.5) and (pos1Y == 7.5):
                pos1X += (25)
                
            elif (pos1Y < 457.5) and (pos1X == 457.5):
                pos1Y += (25)
                
            elif (pos1X > 7.5) and (pos1Y > 450):
                pos1X -= (25)
                
            elif (pos1X == 7.5) and (pos1Y > 7.5):
                pos1Y -= (25)
            
        player1Active = False
        player2Active = True
        
    elif player2Active == True:
        for _ in range(dice):
            if(pos2X < 457.5) and (pos2Y == 17.5):
                pos2X += (25)
                
            elif (pos2Y < 457.5) and (pos2X == 467.5):
                pos2Y += (25)
                
            elif (pos2X > 17.5) and (pos2Y > 450):
                pos2X -= (25)
                
            elif (pos2X == 17.5) and (pos2Y > 17.5):
                pos2Y -= (25)
            
        player1Active = True
        player2Active = False
    
    #Update and re-draw Screen
    pygame.display.update()
    window_scrn.fill((_,_,_))
    window_scrn.blit(window_scrn,(0,0))
    
#Exit Game
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
                



Aucun commentaire:

Enregistrer un commentaire