import pygame import time pygame.init() white = (255, 255, 255) black = (0, 0, 0) red = (255, 0, 0) display_width = 800 display_height = 600 gameDisplay = pygame.display.set_mode((display_width, display_height)) pygame.display.set_caption("Hangman's Revenge") pygame.display.update() font = pygame.font.SysFont(None, 25) #25 is the font size def message_to_screen(msg, color): screen_text = font.render(msg, True, color) gameDisplay.blit(screen_text, [(display_width/2), (display_height/2)]) def GameLoop(): #Created a game loop gameExit = False # lead_x = 300 # Not needed # lead_y = 300 # Not needed while not gameExit: while gameOver == True: gameDisplay.fill(white) message_to_screen("Game over, C to continue, Q to quit") pygame.display.update() # for event in pygame.event.get(): # if event.type == pygame.KEYDOWN for event in pygame.event.get(): if event.type == pygame.QUIT: gameExit = True # If users wants to try again then we would not use this type of exit. if event.pygame == pygame.KEYDOWN: if event.key == pygame.K_LEFT: lead_x -= 10 if event.key == pygame.K_Right: lead_x += 10 #must update graphics after rendering # minght have to use different pics to represent different Hangman phases. gameDisplay.fill(white) # fills color of the background pygame.draw.rect(gameDisplay, black, []) #list contains width and height values #pygame.draw.rect(gameDisplay, black, []) #[0,0] is the top levft of the screen gameDisplay.fill(white) pygame.display.update() #updates the game a fter its rendered print(event) # this simply shows where the cursor is posittioned or which button is pressed message_to_screen('You lose', red) # Message will only be displayed for a millisecond pygame.display.update() time.sleep(2) ## THIS IS Temporary pygame.quit() quit() # if not pygame.font: print ('Warning, fonts disabled') # if not pygame.mixer: print ('Warning, sound disabled') # class PyManMain: # def __init__(self, width=640,height=480): # """Initialize""" # """Initialize PyGame""" # pygame.init() # """Set the window Size""" # self.width = width # self.height = height # """Create the Screen""" # self.screen = pygame.display.set_mode((self.width # , self.height)) # def MainLoop(self): # """This is the Main Loop of the Game""" # """Load All of our Sprites""" # self.LoadSprites(); # """tell pygame to keep sending up keystrokes when they are # held down""" # pygame.key.set_repeat(500, 30) # """Create the background""" # self.background = pygame.Surface(self.screen.get_size()) # self.background = self.background.convert() # self.background.fill((0,0,0)) # while 1: # for event in pygame.event.get(): # if event.type == pygame.QUIT: # sys.exit() # elif event.type == KEYDOWN: # if ((event.key == K_RIGHT) # or (event.key == K_LEFT) # or (event.key == K_UP) # or (event.key == K_DOWN)): # self.snake.move(event.key) # """Check for collision""" # lstCols = pygame.sprite.spritecollide(self.snake # , self.pellet_sprites # , True) # """Update the amount of pellets eaten""" # self.snake.pellets = self.snake.pellets + len(lstCols) # """Do the Drawging""" # self.screen.blit(self.background, (0, 0)) # if pygame.font: # font = pygame.font.Font(None, 36) # text = font.render("Pellets %s" % self.snake.pellets # , 1, (255, 0, 0)) # textpos = text.get_rect(centerx=self.background.get_width()/2) # self.screen.blit(text, textpos) # self.pellet_sprites.draw(self.screen) # self.snake_sprites.draw(self.screen) # pygame.display.flip() # def LoadSprites(self): # """Load the sprites that we need""" # self.snake = Snake() # self.snake_sprites = pygame.sprite.RenderPlain((self.snake)) # """figure out how many pellets we can display""" # nNumHorizontal = int(self.width/64) # nNumVertical = int(self.height/64) # """Create the Pellet group""" # self.pellet_sprites = pygame.sprite.Group() # """Create all of the pellets and add them to the # pellet_sprites group""" # for x in range(nNumHorizontal): # for y in range(nNumVertical): # self.pellet_sprites.add(Pellet(pygame.Rect(x*64, y*64, 64, 64))) # class Snake(pygame.sprite.Sprite): # """This is our snake that will move around the screen""" # def __init__(self): # pygame.sprite.Sprite.__init__(self) # self.image, self.rect = load_image('snake.png',-1) # self.pellets = 0 # """Set the number of Pixels to move each time""" # self.x_dist = 5 # self.y_dist = 5 # def move(self, key): # """Move your self in one of the 4 directions according to key""" # """Key is the pyGame define for either up,down,left, or right key # we will adjust outselfs in that direction""" # xMove = 0; # yMove = 0; # if (key == K_RIGHT): # xMove = self.x_dist # elif (key == K_LEFT): # xMove = -self.x_dist # elif (key == K_UP): # yMove = -self.y_dist # elif (key == K_DOWN): # yMove = self.y_dist # #self.rect = self.rect.move(xMove,yMove); # self.rect.move_ip(xMove,yMove); # class Pellet(pygame.sprite.Sprite): # def __init__(self, rect=None): # pygame.sprite.Sprite.__init__(self) # self.image, self.rect = load_image('pellet.png',-1) # if rect != None: # self.rect = rect # if __name__ == "__main__": # MainWindow = PyManMain() # MainWindow.MainLoop() # # clock = pygame.time.Clock() # # crashed = False # # while not crashed: # # for event in pygame.event.get(): # # if event.type == pygame.QUIT: # # crashed = True # # print(event) # # pygame.display.update() # # clock.tick(60)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question