Posts: 4
Threads: 1
Joined: Feb 2018
Feb-24-2018, 05:47 PM
(This post was last modified: Feb-24-2018, 05:47 PM by reepio13.)
Hi,(note: I have a general knowledge on programming python and pygame as I do A-Level computer science)
I am working on a project of programming chess, using python and pygame. So far, I have programmed the GUIs needed, such as the menu screen, and have made buttons that functions, such as an exit button. However, I have hit a block, which is how do I actually program all the logic behind it? I wish to blit images representing the different pieces, such as the rook and knight, and I want it so the user clicks on a piece, then click on a valid space, in which the piece will then move there. Any help will be much appreciated!
I have thought of when the mouse cursor is in the range of the images, it will ask an if statement whether or not the mouse button has been pressed, then it will allow the user to click an empty space to move the image. I was thinking of blitting the same image to the new space, but then I have the problem of 'removing' the previous image (if that makes sense). I have tried searching the internet for a solution, but I have had troubles, so here I am.
Please ask for more detail if my post lacks sufficient detail. To put it simply, I want to blit an image, then move it using the mouse. However, if there's a more efficient way, I will also consider that. Thank you.
Posts: 3,458
Threads: 101
Joined: Sep 2016
So you have different squares, which could be in different states (empty, Rook, Pawn, etc). Have you considered using a state machine to handle the rendering?
Here's a pretty good article if you're not familiar with the concept: http://gameprogrammingpatterns.com/state.html
Posts: 4
Threads: 1
Joined: Feb 2018
Thank you for your reply, I shall have a look at the links
Posts: 3,458
Threads: 101
Joined: Sep 2016
Cool. If you need more help, please share some code so we have something more specific we can talk about :)
Posts: 5,151
Threads: 396
Joined: Sep 2016
Feb-24-2018, 07:39 PM
(This post was last modified: Feb-24-2018, 07:39 PM by metulburr.)
Have you done a more simple program yet such as pong? Or is this your very first game?
Recommended Tutorials:
Posts: 4
Threads: 1
Joined: Feb 2018
Hi, I have done simple programs, such as rock, paper, scissors, code that involves the exploration of if and while statements, but the only problem is implementing GUIs to more complicated programs, such as moving a chess piece on a board; specifically utilising pygame. However, to answer your question, this is not the first time I have programmed a game, but the first time programming a complex game, only because I want to try object orientated programming, and with chess there are many opportunities to use classes and inheritance.
Posts: 5,151
Threads: 396
Joined: Sep 2016
Feb-24-2018, 09:52 PM
(This post was last modified: Feb-24-2018, 09:52 PM by metulburr.)
You can use classes and inheritance in pong too.
I was more referring to whether or not you programmed GUI games before? Is this your firstbgame using pygame?
The reason i ask this is because you have not shown any of the code you built already. There is no way to tell if you are utilizing pygame mechanics properly such as rects for positioning and rendering, or whether you have 1 main game loop or spaghetti code of many, utilizing OOP, etc. So its hard to judge where you are experience-wise.
Chess would be a good second or third GUI game. Pong is a decent first game because its small and simple. You can more easily fix OOP issues, spaghetti code, not using pygame rects, etc. In pong. You can code correctly before moving on to more advanced games.
I would code a sample of your intended action but i cant get to a desktop at this moment.
Recommended Tutorials:
Posts: 4
Threads: 1
Joined: Feb 2018
import pygame
pygame.init()
#-- window dimension --#
screen_width = 720
screen_height = 720
#-- colours RGB --#
black = (0,0,0)
red =(255,0,0)
green = (0,255,0)
blue = (0,0,255)
white = (255,255,255)
mediumpurple = (147,112,219)
sgigray96 = (244,244,244)
bright_black = (100,0,0)
bright_mediumpurple = (247,112,219)
bright_sgigray96 = (255,100,255)
bright_red = (155,0,0)
gameScreen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption('CHESS')
clock = pygame.time.Clock()
MENU = pygame.image.load('MENU BOI.png')
w_pawn = pygame.image.load('chess_piece_pawn.png')
##def clik_n_drag():
## x = 30
## y = 575
## x_width = 54
## y_height = 54
##
## mouse = pygame.mouse.get_pos()
## clik = pygame.mouse.get_pressed()
## drag = 0
##
## gameScreen.blit(w_pawn, (x,y))
##
## if clik[0] == 1 and x + x_width > mouse[0] > x and y + y_height > mouse[1] > y:
## drag = 1
## elif clik[0] == 0:
## drag = 0
## elif drag == 1:
## x = mouse[0] - (x_width/2)
## y = mouse[1] - (y_height/2)
def clik_n_drag():
x = 30
y = 575
gameScreen.blit(w_pawn, (x,y,))
clik = pygame.mouse.get_pressed()
(x_new, y_new) = pygame.mouse.get_pos()
drag = False
if drag == False:
if clik[0] == 1:
drag = True
if drag == True:
if clik[0] == 1:
(x, y) = (x_new, y_new)
else:
drag = False
def text_objectives(text,font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
#def result_display(text):
def menu(x,y):
gameScreen.blit(MENU, (x,y))
x = 0
y = 0
def bttn(msg,x,y,w,h,ina,act,action=None):
mouse = pygame.mouse.get_pos()
clik = pygame.mouse.get_pressed()
#print(mouse)
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameScreen,act,(x,y,w,h))
if clik[0] == 1 and action != None:
if action == "exc":
specialquit()
elif action == "ss":
statsPage()
elif action == "gm1":
classicMode()
elif action == "gm2":
mayhemMode()
elif action == "gm3":
dumbAIMode()
elif action == "ins1":
classicInfo()
elif action == "ins2":
mayhemInfo()
elif action == "ins3":
dumbAIInfo()
elif action == "mm":
menuLoop()
else:
pygame.draw.rect(gameScreen,ina,(x,y,w,h))
smallText = pygame.font.Font("freesansbold.ttf",15)
textSurf, textRect = text_objectives(msg,smallText)
textRect.center = ((x+(w/2)),(y+(h/2)))
gameScreen.blit(textSurf, textRect)
def specialquit():
pygame.quit()
quit()
def menuLoop():
game = False
while not game:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game = True
gameScreen.fill(white)
menu(x,y)
bttn("classic", 190,545,280,70, black,bright_black, "gm1")
bttn("mayhem", 30,630,210,60, mediumpurple,bright_mediumpurple, "gm2")
bttn("dumb AI", 260,630,210,60, black,bright_black, "gm3")
bttn("classic", 515,600,180,45, black,bright_black, "ins1")
bttn("mayhem", 515,650,85,45, mediumpurple,bright_mediumpurple, "ins2")
bttn("dumb AI", 610,650,85,45, black,bright_black, "ins3")
bttn("exit", 630,15,75,60, red,bright_red, "exc")
bttn("stats", 630,85,75,60, sgigray96,bright_sgigray96, "ss")
pygame.display.update()
clock.tick(60)
pygame.quit()
quit()
def classicMode():
im = pygame.image.load('classic_gamemode_screen.png')
game = False
while not game:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game = True
gameScreen.fill(white)
gameScreen.blit(im, (x,y))
bttn("main menu", 520,215,185,65, sgigray96,bright_sgigray96, "mm")
clik_n_drag()
pygame.display.update()
clock.tick(60)
def mayhemMode():
im = pygame.image.load('mayhem_gamemode_screen.png')
game = False
while not game:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game = True
gameScreen.fill(white)
gameScreen.blit(im, (x,y))
bttn("main menu", 520,195,185,40, sgigray96,bright_sgigray96, "mm")
pygame.display.update()
clock.tick(60)
def dumbAIMode():
im = pygame.image.load('dumbAI_gamemode_screen.png')
game = False
while not game:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game = True
gameScreen.fill(white)
gameScreen.blit(im, (x,y))
bttn("main menu", 520,215,185,65, sgigray96,bright_sgigray96, "mm")
pygame.display.update()
clock.tick(60)
def statsPage():
im = pygame.image.load('stats_screen.png')
game = False
while not game:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game = True
gameScreen.fill(white)
gameScreen.blit(im, (x,y))
bttn("main menu", 515,15,100,60, sgigray96,bright_sgigray96, "mm")
pygame.display.update()
clock.tick(60)
def classicInfo():
im = pygame.image.load('classic_ins_screen.png')
game = False
while not game:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game = True
gameScreen.fill(white)
gameScreen.blit(im, (x,y))
bttn("main menu", 515,15,100,60, sgigray96,bright_sgigray96, "mm")
pygame.display.update()
def mayhemInfo():
im = pygame.image.load('mayhem_ins_screen.png')
game = False
while not game:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game = True
gameScreen.fill(white)
gameScreen.blit(im, (x,y))
bttn("main menu", 515,15,100,60, sgigray96,bright_sgigray96, "mm")
pygame.display.update()
def dumbAIInfo():
im = pygame.image.load('dumbAI_ins_screen.png')
game = False
while not game:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game = True
gameScreen.fill(white)
gameScreen.blit(im, (x,y))
bttn("main menu", 515,15,100,60, sgigray96,bright_sgigray96, "mm")
pygame.display.update()
menuLoop()
pygame.quit()
quit() This is all what I've got, which are the GUIs, if it helps.
|