Python Forum
Animation with polygon points not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Animation with polygon points not working
#1
I created a sin equation that I want to use to translate a point on a polygon, but it isn't doing anything. I used print statements for both my equation and coordinate to find out if anything is happening, and they both are fully functional. But the animation is just not playing.

Here's my entire code:

import pygame
import random
import math

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)

# cube drawing variables
# --- red plane
rPlane_p13_x = 640
rPlane_p2_x  = 550
rPlane_p4_x  = 730

rPlane_p1_y  = 230
rPlane_p24_y = 255
rPlane_p3_y  = 290

# --- green plane
gPlane_p1_x  = 550
gPlane_p2_x  = 550
gPlane_p34_x = 640

gPlane_p1_y  = 255
gPlane_p2_y  = 360
gPlane_p3_y  = 410
gPlane_p4_y  = 290

# --- blue plane
bPlane_p12_x = 640
bPlane_p3_x  = 730
bPlane_p4_x  = 730

bPlane_p1_y  = 290
bPlane_p2_y  = 410
bPlane_p3_y  = 360
bPlane_p4_y  = 255

pi = 3.14159265359  

pygame.init()

size = (1280, 720)
screen = pygame.display.set_mode(size)

pygame.display.set_caption("Rotating Cube")

done = False

clock = pygame.time.Clock()

while not done:

   for event in pygame.event.get():
       if event.type == pygame.QUIT:
           done = True

   screen.fill(WHITE)
   
   
   
   def draw_cube():
           
       pygame.draw.polygon(screen, RED,   [[rPlane_p13_x, rPlane_p1_y], [rPlane_p2_x, rPlane_p24_y], [rPlane_p13_x, rPlane_p3_y], [rPlane_p4_x, rPlane_p24_y]], 0)
       pygame.draw.polygon(screen, GREEN, [[gPlane_p1_x, gPlane_p1_y],  [gPlane_p2_x, gPlane_p2_y],  [gPlane_p34_x, gPlane_p3_y], [gPlane_p34_x, gPlane_p4_y]], 0)
       pygame.draw.polygon(screen, BLUE,  [[bPlane_p12_x, bPlane_p1_y], [bPlane_p12_x, bPlane_p2_y], [bPlane_p3_x, bPlane_p3_y],  [bPlane_p4_x, bPlane_p4_y]],  0)

   draw_cube()
   
   for degree in range(361):
   
       y_value = 50 * math.sin(pi / 180 * degree)
       gPlane_p2_y += y_value


   
   pygame.display.flip()

   clock.tick(60)

pygame.quit()
If you've got a solution, please let me know. Thank you.
Reply
#2
In your loop where you apply the sine, add a before/after print, like so:
    before = gPlane_p2_y
    for degree in range(361):
        y_value = math.sin(pi / 180 * degree)
        gPlane_p2_y += y_value
    print("{0} => {1}".format(before, gPlane_p2_y))
You'll quickly see that gPlane_p2_y (the only value you change) is pretty much always 360, which explains why you don't see any movement... because none of the numbers are changing.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  using python with QGIS to disseminate CSV data on a polygon wissam1974 0 1,483 Jun-10-2020, 10:13 AM
Last Post: wissam1974
  Looking for algorithm to tranverse a polygon dspal 0 2,240 May-07-2018, 08:46 PM
Last Post: dspal

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020