[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] moving a dot!



On 1/22/2012 2:32 PM, babaz wrote:
> Hello,
> 
> How can I modify this programm to be able to move the yellow dot with the 
> keys of a keyboard?
> 
> Here is the script :
> 
> # -*- coding: cp1252 -*-
> ## Michael ELBAZ
> ## AE(a)
> 
> full_screen = True
> window_size = (1024, 768)
> 
> import sys, random, pygame, os, platform
> from pygame.locals import *
> from math import *
> 
> delta=2
> delta_step=1
> 
> circles = ((0, delta),)
> 
> circle_scale = 0.3
> circle_radius = 8
> n_grid = 3
> grid_spacing = 0.2
> grid_length = 0.05
> grid_width = 2
> rotation_speed = pi/2
> grid_color = (100, 100, 255)
> fix_radius = 8
> bg_color = (0, 0, 0)
> circle_color = (255, 255, 0)
> disapp_frames = 1
> 
> window_center = (window_size[0]/2.0, window_size[1]/2.0)
> window_scale = min(window_size)/2.0
> def coord(real):
>     """takes real coordinates, returns pixel coordinates"""
>     return (int(round(real[0]*window_scale + window_center[0])),\
>             int(round(-real[1]*window_scale + window_center[1])))
> g_cos, g_sin = 1, 0
> def set_rotation(angle):
>     """sets up rotation"""
>     global g_cos, g_sin
>     g_cos, g_sin = cos(angle), sin(angle)
> def rotate(point):
>     """rotates a 3D point about the Z-axis by given angle set by 
> set_rotation()"""
>     return (g_cos*point[0] + g_sin*point[1], -g_sin*point[0] + 
> g_cos*point[1])
> 
> # graphics initializations
> frames, show = 0, True
> try:
>     pygame.init()
>     if full_screen:
>         surf = pygame.display.set_mode(window_size, HWSURFACE | FULLSCREEN 
> | DOUBLEBUF)
>     else:
>         surf = pygame.display.set_mode(window_size)
> 
>     t0 = pygame.time.get_ticks()
>     while True:
> 
>         
>         for event in pygame.event.get():
>             if event.type == KEYDOWN:
>                     if event.key == K_ESCAPE:
>                         raise Exception()
>             elif event.type == MOUSEBUTTONDOWN: frames, show = 0, False
>             elif event.type == MOUSEBUTTONUP: frames, show = 0, True
> 
>         
>         
>         for circ in circles:
>             c = (circle_scale*circ[0], circle_scale*circ[1])
>             if show:
>                 col = (150, 150, 0)
>                 pygame.draw.circle(surf, col, coord(c), circle_radius, 0)
>             else:
>                 step = 150/disapp_frames
>                 lev = max(0, 150 - frames*step)
>                 col = (lev, lev, 0)
>                 if lev > 0:
>                     pygame.draw.circle(surf, col, coord(c), circle_radius, 
> 0)
> 
>             for event in pygame.event.get():
>       
>                 if event.key == K_LEFT:
>                     delta -= delta_step
>                 if event.key == K_RIGHT:
>                     delta += delta_step
> 
>                     if show:
>                         col = (150, 150, 0)
>                         pygame.draw.circle(surf, col, coord(c), 
> circle_radius, 0)
>                     else:
>                         step = 150/disapp_frames
>                         lev = max(0, 150 - frames*step)
>                         col = (lev, lev, 0)
>                         if lev > 0:
>                             pygame.draw.circle(surf, col, coord(c), 
> circle_radius, 0)
> 
>                     
>         pygame.draw.circle(surf, grid_color, coord((0, 0)), fix_radius)
>         pygame.display.flip()
>         frames += 1
> 
> 
> 
>         
> finally: pygame.quit()
> 
> Thank you very much !
> 
> 
Franc Dernons code works, your issue was that you were calling
pygame.event.get() twice in one frame.
The first pygame.event.get() was stealing the keyboard events from teh
second, so the second WAS working, just not getting events. If you ran
this at an insanely slow rate you could give events separately to each
loop and see the odd effect. He consolidated the loops together so
event.get() was called only once.


-- 
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org