— boreal-kiss.net

Archive
September, 2012 Monthly archive

As a pygame exercise, I wrote a scalable Nyan Cat animation from scratch, i.e., everything from Python code.

Example

The simplest implementation is here. To run animation you have to take the following three steps:

  1. Create objects (cat, rainbow, and stars)
  2. Draw the objects
  3. Update the objects’ state.

Note that half of the code is boilerplate for pygame. You of course need pygame to run the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
	import pygame
	from nyancat import Nyancat
	from rainbow import Rainbow
	from star_manager import StarManager
 
	pygame.init()
	DISPLAY_SURFACE = pygame.display.set_mode((320, 240))
	BACKGROUND_COLOR = pygame.Color(15, 77, 143)
	DISPLAY_SURFACE.fill(BACKGROUND_COLOR)
	FPS = 12
	CLOCK = pygame.time.Clock()
 
	#Create objects.
	cat = Nyancat(pygame.Rect(110, 90, 100, 250))
	rainbow = Rainbow(cat.rect, cat.pixelSize)
	stars = StarManager(DISPLAY_SURFACE.get_rect(), cat.pixelSize, 10)
 
	while True:
		DISPLAY_SURFACE.fill(BACKGROUND_COLOR)
 
		for e in pygame.event.get():
			if e.type == pygame.QUIT:
				pygame.quit()
				sys.exit()
 
		#Draw the objects. 
		rainbow.draw(DISPLAY_SURFACE)
		cat.draw(DISPLAY_SURFACE)
		stars.draw(DISPLAY_SURFACE)
 
		#Update the animation state of the objects.
		rainbow.update()
		cat.update()
		stars.update()
 
		pygame.display.update()
		CLOCK.tick(FPS)

Download

You can download the source from https://github.com/borealkiss/nyancat.py.

Read More

JoystickController is an Arduino library that is fully compatible with SparkFun’s Joystick Shield. The library lets you easily fetch the current status of the joystick. Implementation is quite simple;

  JoystickController(int low = DEFAULT_LOW, int high = DEFAULT_HIGH)

is a constructor. The first argument specifies the lower threshold value for the directional key pad, below which the controller is assumed to set left/down in the horizontal/vertical direction. The second argument specifies the upper threshold value, above which the controller is assumed to set right/up in the horizontal/vertical direction. If arguments are not specified, the following default values will be used:

  static const int DEFAULT_LOW  = 490;
  static const int DEFAULT_HIGH  = 530;

(Note these variables are defined as private so that you can not directly access them) For more information about the threshold see the following article: How do I find the current direction of the joystick?

 void update()

must get called in the main loop in order for the library to communicate with the joystick.

  int getDirection()

returns the current direction of the directional keypad as integer values that are defined as class static variables:

  static const int DIRECTION_NONE = 0;
  static const int DIRECTION_UP = 1;
  static const int DIRECTION_UP_RIGHT = 2;
  static const int DIRECTION_RIGHT = 3;
  static const int DIRECTION_DOWN_RIGHT = 4;
  static const int DIRECTION_DOWN = 5;
  static const int DIRECTION_DOWN_LEFT = 6;
  static const int DIRECTION_LEFT = 7;
  static const int DIRECTION_UP_LEFT = 8;

Finally buttons’ status can be fetched from the following functions:

  bool upButtonPressed()
  bool downButtonPressed()
  bool leftButtonPressed()
  bool rightButtonPressed()
  bool selectButtonPressed()

They return true if a button is being pressed, otherwise false. 

Example

Here is a simple example for the library implementation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "JoystickController.h"
 
JoystickController controller;
 
void setup(){
  Serial.begin(9600);
}
 
void loop(){
  controller.update();
 
  if (controller.selectButtonPressed()){
    Serial.println("S"); 
  }
 
  if (controller.upButtonPressed()){
    Serial.println("U"); 
  }
 
  if (controller.downButtonPressed()){
    Serial.println("D"); 
  }
 
  if (controller.leftButtonPressed()){
    Serial.println("L"); 
  }
 
  if (controller.rightButtonPressed()){
    Serial.println("R"); 
  }
 
  int direct = controller.getDirection();
 
  switch (direct){
    case  JoystickController::DIRECTION_UP:
      Serial.println("UP");
      break;
    case  JoystickController::DIRECTION_UP_RIGHT:
      Serial.println("UP-RIGHT");
      break;
    case  JoystickController::DIRECTION_RIGHT:
      Serial.println("RIGHT");
      break;
    case  JoystickController::DIRECTION_DOWN_RIGHT:
      Serial.println("DOWN-RIGHT");
      break;
    case  JoystickController::DIRECTION_DOWN:
      Serial.println("DOWN");
      break;
    case  JoystickController::DIRECTION_DOWN_LEFT:
      Serial.println("DOWN-LEFT");
      break;
    case  JoystickController::DIRECTION_LEFT:
      Serial.println("LEFT");
      break;
    case  JoystickController::DIRECTION_UP_LEFT:
      Serial.println("UP-LEFT");
      break;
  }
 
  delay(100);
}

Download

The library is available from my Github repository: JoystickController

Read More