Hi everyone! I joined these forums about a week ago so I could try and learn more about Python, and thankfully I can say that I have learned a bit. Enough to try and create a Mastermind game, although I'm having trouble with converting the code into Pygame and give it the graphics I have.
The code for the game is already set up and works, have played it through multiple times without fail. My only problem now is that whenever I attempt to play the game and post the corresponding keys to place the selected jewel in the slot, it instead places the background instead of the jewel.
Here is the code I have so far for the board...
import pygame
from pygame.locals import *
import os, sys
#colorkey=None is the default transparency
def load_image(name, colorkey=None):
#assume resources directory is named "data"
#concatenate with the image file name
#fullname = os.path.join('chad', name)
try:#use try when an error could occur
#load the image from disk to a surface
background = pygame.image.load('mastermind_empty_board.jpg').convert()
except pygame.error, message:
#error message if we can't find the image
raise SystemExit, message
#the convers the bit format for efficent display
images = background.convert()
if colorkey is not None:
if colorkey is -1:
colorkey = background.get_at((0,0))
background.set_colorkey(colorkey, RLEACCEL)
return images, background.get_rect()
def drawBoard(counters):
x=0
y=0
cnt=0
for key in counters:
if cnt == 4:
y+=32
x=0
elif cnt == 8:
y+=32
x=0
elif cnt == 12:
y+=32
x=0
elif cnt == 16:
y+=32
x=0
elif cnt == 20:
y+=32
x=0
elif cnt == 24:
y+=32
x=0
elif cnt == 28:
y+=32
x=0
elif cnt == 32:
y+=32
x=0
if (key == "r") or (key == "R"):
#print "r"
screen.blit(red_image, (x,y))
if (key == "g") or (key == "G"):
screen.blit(green_image, (x,y))
#print "g"
if (key == "b") or (key == "B"):
screen.blit(blue_image, (x,y))
#print "b"
if (key == "y") or (key == "Y"):
screen.blit(yellow_image, (x,y))
#print "y"
# if (key == " "):
#print "nothing"
cnt+=1
x+=45
pygame.init()
screen = pygame.display.set_mode((640, 480), 0, 32)
font = pygame.font.SysFont("arial", 32);
font_height = font.get_linesize()
counterlist=[" "]*32
red_image, red_rect = load_image('red.png', -1)
red_rect.topleft = (10,10)
green_image, green_rect = load_image('green.png', -1)
green_rect.topleft = (0,0)
blue_image, blue_rect = load_image('blue.png', -1)
blue_rect.topleft = (0,0)
yellow_image, yellow_rect = load_image('yellow.png', -1)
yellow_rect.topleft = (0,0)
count=0
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
if event.type == KEYUP:
if event.key == K_r:
counterlist[count] ="r"
count+=1
print red_rect
if event.key == K_g:
counterlist[count] ="g"
count+=1
print green_rect
if event.key == K_b:
counterlist[count] ="b"
count+=1
print blue_rect
if event.key == K_y:
counterlist[count] ="y"
count+=1
print yellow_rect
drawBoard(counterlist)
pygame.display.update()
So basically, the problem I'm having is when I put this in the background is appear for the corresponding keys instead of the red, green, blue, or yellow gems popping up. The background goes 4 over and then one line down to do 4 again, overlapping each time.
Any and all tips/suggestions/help to maybe clean up the code a little more or anything else would be welcome! I'd just like to get this up and running, if you'd like to see my Mastermind game code itself since I would be integrating that in to make the full fledged game, please just ask and I'd be more than happy to provide it.
I will also attach the pictures that I have been using to put together the background/images for the jewels if you wish to download them.