Mar-22-2018, 05:48 PM
I want to create a game in which an image is shown in the screen and user have to input the correct name of the object. This is the code i have so far:
import sys import pygame import time import random import os import Tkinter as tk WIDTH = 420 HEIGHT = 540 FPS = 360 ORANGE = (255, 165, 0) BLUE = (0,0,255) CYAN = (0,255,255) BLACK = (255, 255, 255) pygame.init() pygame.mixer.init() # Centers the game window os.environ['SDL_VIDEO_CENTERED'] = '1' screen = pygame.display.set_mode((957, 547)) pygame.display.set_caption("My Game") clock = pygame.time.Clock() # Variables house = pygame.image.load(os.path.abspath("house_1.png")) ##rectangle = pygame.rect.Rect(10, 10, WIDTH* 1/5, WIDTH* 1/5) def text_objects(text, font): textSurface = font.render(text, True, BLACK) return textSurface, textSurface.get_rect() def img1(): img1 = pygame.image.load(os.path.abspath("house_2.png")) screen.blit(img1,(0,0)) def img2(): img2 = pygame.image.load(os.path.abspath("house_3.png")) screen.blit(img2,(0,0)) def img3(): img3 = pygame.image.load(os.path.abspath("house_4.png")) screen.blit(img3,(0,0)) def img4(): img4 = pygame.image.load(os.path.abspath("house_5.png")) screen.blit(img4,(0,0)) def img5(): img5 = pygame.image.load(os.path.abspath("house_6.png")) screen.blit(img5,(0,0)) def button (msg, x, y, w, h, ic, ac, action=None ): mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() if (x+w > mouse[0] > x) and (y+h > mouse[1] > y): pygame.draw.rect(house, CYAN, (x, y, w, h)) if (click[0] == 1 and action != None): if (action == "Bedroom"): img1() elif (action == "Bathroom"): img2() elif (action == "Garage"): img3() elif (action == "Livingroom"): img4() elif (action == "Kitchen"): img5() else: pygame.draw.rect(house, BLUE, (x, y, w, h)) smallText = pygame.font.Font("freesansbold.ttf", 20) textSurf, textRect = text_objects(msg, smallText) textRect.center = ( (x+(w/2)), (y+(h/2)) ) house.blit(textSurf, textRect) # Game Loop def game_loop(): running = True while running: # keep loop running at the right speed clock.tick(FPS) # Process input (events) for event in pygame.event.get(): # check for closing window if event.type == pygame.QUIT: running = False #Drawing Graphics screen.blit(house,(0,0)) ## pygame.mixer.music.load('bckgrnd_2.wav') ## pygame.mixer.music.play() ##pygame.draw.rect(screen, ORANGE, rectangle, 10) button ("Bedroom", 750, 120, 120, 25, BLUE, CYAN, "Bedroom" ) button ("Bathroom", 750, 160, 120, 25, BLUE, CYAN, "Bathroom" ) button ("Garage", 750, 200, 120, 25, BLUE, CYAN, "Garage" ) button ("Livingroom",750, 240, 120, 25, BLUE, CYAN, "Livingroom" ) button ("Kitchen", 750, 280, 120, 25, BLUE, CYAN, "Kitchen" ) # after drawing everything, flip the display pygame.display.flip() game_loop() pygame.quit()Thanks for the support!
