Parts of the house - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: Parts of the house (/thread-9134.html) |
Parts of the house - abscorpy - Mar-22-2018 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! RE: Parts of the house - metulburr - Mar-22-2018 and your questions is? Quote: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)) Dont do this. You are creating a bottleneck that loads your resources before it draws, multiple times per second when you only ever need to load it once per game. For more info check the resources loading tutorial here https://python-forum.io/Thread-PyGame-Loading-images-transparency-handling-spritesheets-part-2 Quote:This is also an inefficient method as python rect have coordinates in them that can do this. The button should have a rect of its position and draw to that same rect. Then all you have to do isif (x+w > mouse[0] > x) and (y+h > mouse[1] > y): if btn_rect.collidepoint(pg.mouse.get_pos()): to test if the mouse is within the btn_rect.
RE: Parts of the house - abscorpy - Mar-24-2018 so my question is, maybe you can help me writing the code for creating the interface for the game, so my idea is as follows: I want to create an interface in which an image of an object appears and the user have to input the word corresponding to the object shown on screen, thanks for the support. |