python 2.7 - How to get something stuck to something else? -


i try apple picture on top on white triangle when apple on triangle , press h. i'm stuck , don't know do. when hit h, nothing or bad happens. can move rectangle around, not pick picture. , not know how rectangle leave apple either. here's code:

import pygame import random pygame.locals import *   screensize = (640,480) surface = pygame.display.set_mode(screensize)   running = true clock = pygame.time.clock()  x = 1 y = 1 = random.randint(0,640) b = random.randint(0,480)   while running:      clock.tick(15)      surface.fill((20,150,100))       event in pygame.event.get():         if event.type == pygame.quit:             running = false          if event.type == pygame.keydown:             if event.key == pygame.k_left:                 x -= 10             elif event.key == pygame.k_right:                 x += 10             elif event.key == pygame.k_up:                 y -= 10             elif event.key == pygame.k_down:                 y += 10             elif event.key == pygame.k_h:                 == x , b == y         if event.type == pygame.keyup:             if event.key == pygame.k_left or event.key == pygame.k_right:                 x -= 0             elif event.key == pygame.k_up or event.key == pygame.k_down:                 y -= 0     line1 = pygame.draw.line(surface, (0,0,0), [100,100], [10,150], 4)     line2 = pygame.draw.line(surface, (0,0,0), [100,100], [100,300], 4)     line3 = pygame.draw.line(surface, (10, 20, 39), [100,100], [190,150] , 4)     line4 = pygame.draw.line(surface, (0,0,0), [100,300], [10,350], 4)     line5 = pygame.draw.line(surface, (0,0,0), [100,300], [190,350], 4)     circle = pygame.draw.circle(surface, (0,0,0), [100,60], 40, 4)     rect = pygame.draw.rect(surface, (150, 50, 0), [350, 250, 100, 100])     linerect = pygame.draw.line(surface, (75,25,0), [350, 280], [450,280],3)     rect2 = pygame.draw.rect(surface, (200, 150, 0), [395,275,10,10])     rect3 = pygame.draw.rect(surface, (75,25,0), [360,290,80,50],3)     box = pygame.draw.rect(surface, (255,255,255), [x,y,100,100])      apple = pygame.image.load('apple1.bmp')     surface.blit(apple,(a,b))      pygame.display.flip()    pygame.quit() 

one of lines rather odd looking me:

a == x , b == y 

this should error since line not used in if or elif line. == can used in if or leif line since compares 2 variables. using above line not except raising error or doing nothing. try changing to:

a = x b = y 

you need 2 sections seperate , not use excess code (the = , and). put triangle under apple, need make sure apple on triangle. use collision code figure out. make easier, if both sprites, use if sprite_name.rectcollide() detect collision.

to make apple "stick" triangle, need make center of apple center of triangle. try changing a x coordinate of triangle , b y coordinate of triangle. use variable can enable sticking like:

sticky = true if sticky == true:     = x     b = y else:      pass 

and how change sticky false idea...


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -