On Sun, Jan 22, 2012 at 23:52, Michaël ELBAZ <[email protected]> wrote:

> Hello,
>
> How can we modify this script to be able to move the yellow dot with the
> keys of a keyboard?
>
>  # -*- coding: cp1252 -*-
>
> 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 !
>
> --
> M.E.
>



Hi Michaël,

I quickly edited your code, here is a working version below (which still
needs some clean up).

In short:
1) Only loop once in pygame.event.get()
2) Always check event.type == KEYDOWN  before accessing event.key
3) Call surf.fill() in each iteration


###########################################
# -*- coding: cp1252 -*-

full_screen = False
window_size = (1024, 768)

import sys, random, pygame, os, platform
from pygame.locals import *
from math import *

delta=3
delta_step=0.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:
        surf.fill((0, 0, 0))
        circles = ((delta, 0),)
        for circ in circles:

            c = coord((circle_scale*circ[0], circle_scale*circ[1]))
            if show:
                col = (150, 150, 0)
                pygame.draw.circle(surf, col, 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, c, circle_radius, 0)

            for event in pygame.event.get():
                if event.type == KEYDOWN and event.key == K_ESCAPE:
                    raise Exception()
                elif event.type == MOUSEBUTTONDOWN: frames, show = 0, False
                elif event.type == MOUSEBUTTONUP: frames, show = 0, True
                if event.type == KEYDOWN and event.key == K_LEFT:
                    delta -= delta_step
                if event.type == KEYDOWN and event.key == K_RIGHT:
                    delta += delta_step

                    if show:
                        col = (150, 150, 0)
                        pygame.draw.circle(surf, col, 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, c, circle_radius,
0)

        # blue circle
        pygame.draw.circle(surf, grid_color, coord((0, 0)), fix_radius)
        pygame.display.flip()
        frames += 1




finally: pygame.quit()
###########################################


Best,
F

----
Franck Dernoncourt
[email protected]
http://franck-dernoncourt.com

Reply via email to