python - Pygame screen width -


i made program displays word , plays sound effect everytime click space bar. problem when word blited screen either blits halfway on screen or halfway off screen. tried using statement says if x > 800 know there has better way , don't know appreciated!

import pygame, random, sys pygame.locals import * pygame.init() screen_size = ((800,600)) pygame.mixer.music.load("derp.wav") screen = pygame.display.set_mode(screen_size) while true:     newsat = random.randint(1,100)     r = random.randint(1,255)     g = random.randint(1,255)     b = random.randint(1,255)     newsize = random.randint(1,50)     myfont = pygame.font.sysfont("ubuntu", newsize)     derp = myfont.render("derp",newsat,(r,g,b))     newx = random.randint(1,800)     newy = random.randint(1,600)     newspot = random.randint(1,800)     event in pygame.event.get():         if event.type == quit:             sys.exit()         if event.type == keydown:             if event.key == k_space:                 screen.blit(derp,(newx,newy))                 pygame.mixer.music.play(0)             if event.key == k_escape:                 sys.exit()     pygame.display.update() 

right! so, make sure blitted surface (in case rendered text) inside screen need put restrictions in random generations!

derp = myfont.render("derp",newsat,(r,g,b)) newx = random.randint(1,800 - derp.get_width()) newy = random.randint(1,600 - derp.get_height()) 

that way irrespectively of text's size, random position inside screen :) , can safely blit text:

screen.blit(derp,(newx,newy)) 

hope helped!,

cheers, alex


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -