Okay, so I was playing around with my game trying to figure out how to implement cheats. There's probably a more elegant way to do this, but I'm still pretty new to coding, so give me a break.
Basically I created a pause menu function. All my menu's work by implementing a menu with basic functions and attributes. You just feed the menu function some images to use for the screen, and set up a loop to watch for keystrokes or other input events.
Anyway, for the pause menu, I just have a simple background with two options available to choose from. It watches for input from the up, down, left, right, b, a, keys. (went with the konami code for testing) I set up two variables. code and match. code is an array that holds key strokes while match is used to determine if the keys were pressed in the correct order.
def pause(screen, font):
images = ['menu.png', 'menu1.png']
menu = Menu(images)
menu.imgset = [pygame.image.load(images[0]), pygame.image.load(images[1])]
pygame.sprite.RenderPlain(menu)
clock = pygame.time.Clock()
code = []
match = ['u','u','d','d','l','r','l','r','b','a']
while True:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
terminate()
if event.type == KEYDOWN:
if event.type == pygame.QUIT:
terminate()
if event.key == K_UP:
menu.changeup()
code.append('u')
if event.key == K_DOWN:
menu.changedown()
code.append('d')
if event.key == K_LEFT:
code.append('l')
if event.key == K_RIGHT:
code.append('r')
if event.key == K_b:
code.append('b')
if event.key == K_a:
code.append('a')
if event.key == K_RETURN:
if menu.pos == 0:
#shared.PAUSE = False
return False
if menu.pos == 1:
pygame.quit()
sys.exit()
code = code[-10:]
if code == match:
shared.CHEAT = True
code = []
menu.update()
pygame.draw.rect(screen,(0, 0 ,0),(0,0,800,600))
screen.blit(menu.image,menu.rect)
#drawText('Paused', font, screen, 350, 30)
textobj = font.render("Paused", 1, (255,255,255))
textrect = textobj.get_rect()
textrect.topleft = (350, 30)
screen.blit(textobj, textrect)
pygame.display.update()
Here's the bit in the main loop that uses the pause and checks for the cheat variable before changing the background image:
def main():
while 1:
#set up variables, initialize sprites, etc, etc...
.......
for event in pygame.event.get():
if event.type == pygame.QUIT:
terminate()
if event.type == KEYDOWN:
#throw in a pause key
if event.key == K_p:
pause(screen, font)
#check for cheat and blit images to screen surface
if shared.CHEAT:
screen.blit(backimage2,backrect)
if not shared.CHEAT:
screen.blit(backimage,backrect)
screen.blit(attack.image,attack.rect)
screen.blit(player.image,player.rect)
pygame.display.update()
basically pressing p brings up the pause screen, and while in the pause menu, if the two variables match, it sets the CHEAT variable to true.
You could instead, have it add more "cheat" options to the menu, so you could turn them on and off. The way I did it, leaves the cheat on for the duration of the game. Like I said, there are probably better ways to do it, but this works. I'll probably post the whole game source at some point, but if you'd like to see it now, just let me know and I'll shoot you the source.
-newt
No comments:
Post a Comment