python - Pygame - How to stop an image from leaving the edge of the screen? -


a section of jetfighterx leaves screen when mouse hovers on edge of window, causes tarantula explode time time respawns top of window, how can stop happening (without use of classes)?

code:

import pygame, sys, pygame.mixer pygame.locals import * import random  pygame.init()  bif = "space.jpg" jf = "spacefightersprite.png" enemy = "tarantulaspacefighter.png"  laser = pygame.mixer.sound("laserblast.wav") explosionsound = pygame.mixer.sound("explosion.wav")  screen = pygame.display.set_mode((1000,900),0,32) caption = pygame.display.set_caption("jet fighter x")  background = pygame.image.load(bif).convert()  jetfighterx = pygame.image.load(jf) jetfighterx = pygame.transform.scale(jetfighterx, (400,400))  tarantula = pygame.image.load(enemy) tarantula = pygame.transform.scale(tarantula, (100,100))  laserblast = pygame.image.load("c:\python27\laser.png") explosion=pygame.image.load("c:\python27\explosion.png") explosion=pygame.transform.scale(explosion, (150,150))  ex,ey = 450,0 movex,movey = 0,0 clock = pygame.time.clock() speed = 300 shoot_y = 0 laser_fired = false collision = false alive = true  explo_timer = 25  while true:     pygame.mouse.set_visible(false)      mx,my = pygame.mouse.get_pos()      jetfighterx_rect = jetfighterx.get_rect(center=(mx, my))     jetfighterx_rect = jetfighterx_rect.inflate(-200,-200)      tarantula_rect = tarantula.get_rect(center=(ex, ey))     tarantula_rect = tarantula_rect.inflate(-180,-200)      # check player inputs     event in pygame.event.get():         if event.type == quit:             pygame.quit()             sys.exit()         if event.type == keydown:             if event.key == k_escape or event.key == k_q:                 sys.exit()          if event.type == mousebuttondown:             laser_fired = true             laser.play()             shoot_y = my-200             shoot_x = mx-16      # update game      milli = clock.tick()     seconds = milli/1000.      dmy = seconds * speed     ey += dmy      if ey > 900:         explo_timer = 25          collision = false         alive = true          ey = 0         ex = random.randint(50,900)     if laser_fired:         shoot_y -= 10         if shoot_y < 0:             laser_fired = false         else:             laserblast_rect = laserblast.get_rect(center=(shoot_x, shoot_y))              if laserblast_rect.colliderect(tarantula_rect):                 explosionsound.play()                 collision = true                 alive = false       if jetfighterx_rect.colliderect(tarantula_rect) , alive:         explosionsound.play()         collision = true         alive = false     # draw on screen     screen.blit(background, (0,0))     screen.blit(jetfighterx,(mx-200,my-200))     if not collision:         screen.blit(tarantula, (ex, ey))     elif collision:         explo_timer-=2         if explo_timer > 0 , alive == false:             screen.blit(explosion, (ex, ey-50))     if laser_fired:         screen.blit(laserblast, (shoot_x, shoot_y))      pygame.display.update() 

just add limit not allow fighter move within x pixels of border.

assuming x,y coordinates of centre of fighter jetfighter_x, jetfighter_y (you need change variable names whatever code has) write this:

lbuffer = 16 rbuffer = 1000 - 16 tbuffer = 900 - 16 bbuffer = 16  if jetfighter_x > rbuffer:     jetfighter_x = rbuffer  if jetfighter_x < lbuffer:     jetfighter_x = lbuffer  if jetfighter_y > tbuffer:     jetfighter_y = tbuffer  if jetfighter_y < bbuffer:     jetfighter_y = bbuffer 

this should prevent center of ship getting closer 16 pixels edge. need tweak accommodate size of ship. (the buffer sides width of image/2 .respectively buffer top , bottom height of image/2).


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -