python - How to make a sprite correct path? -
i trying create small , simple simulation makes meteor appear in middle of screen , move away... until users presses key. while key pressed, path corrected dynamically new path.
i tried 2 different method, first keydown, , pygame.key.get_pressed() function, in both cases, after release key, image "meteor" drops down again orginal path.
i hoping 1 indicate me doing wrong.
here code far:
import pygame,sys pygame.locals import * colors import * import random fps = 30 fpsclock = pygame.time.clock() pygame.init() x_display = 400 y_display = 400 displaysurf = pygame.display.set_mode((x_display, y_display)) displaysurf.fill(silver) pygame.display.set_caption("meteor") x_real_max = x_display/2 y_real_max = y_display/2 x_center = 0 y_center = 0 xmeteor = 40 ymeteor = 20 xcentermeteor = 0 ycentermeteor = 0 xposmeteor = xcentermeteor + (200 - (xmeteor/2)) yposmeteor = ycentermeteor + (200 - (ymeteor/2)) imgmeteor = pygame.image.load("desktop\imgmeteor.png") viseffect = 1.05 while true: xmeteor *= viseffect ymeteor *= viseffect xcentermeteor *= viseffect ycentermeteor *= viseffect xposmeteor = xcentermeteor + (200 - (xmeteor/2)) yposmeteor = ycentermeteor + (200 - (ymeteor/2)) #pressed = pygame.key.get_pressed() #if pressed[pygame.k_w]: #yposmeteor -= 10 if xmeteor > 150: xmeteor = 40 ymeteor = 20 xcentermeteor = random.randrange(-100, 100) ycentermeteor = random.randrange(-100, 100) xposmeteor = xcentermeteor + (200 - (xmeteor/2)) yposmeteor = ycentermeteor + (200 - (ymeteor/2)) meteor = pygame.transform.scale(imgmeteor, (int(xmeteor),int(ymeteor))) event in pygame.event.get(): if event.type == quit: pygame.quit() sys.exit() #if event.type == keydown: #if event.key == k_w: #yposmeteor -= 50 #elif event.type == keyup: #yposmeteor = yposmeteor displaysurf.blit(meteor, (xposmeteor, yposmeteor)) pygame.display.update() fpsclock.tick(fps) displaysurf.fill(silver)
you're updating wrong variable. yposmeteor
clobbered every time through loop, when change variable there no lasting effect. want change either ycentermeteor
or ymeteor
.
Comments
Post a Comment