- *args and **kwargs
- list comprehensions
- f strings
- lambda functions
- decorator functions
I've been teaching my daughter Django as well as JavaScript, HTML, CSS, and bootstrap. Django is really awesome for creating interactive web sites quickly.
class Bullet(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((6,6))
self.rect = self.image.get_rect()
def move(angle, center):
hypotenuse = 10.0
adjacent = math.cos(angle) * hypotenuse
x = int(adjacent + center[0])
opposite = math.sin(angle) * hypotenuse
y = int(center[1] - opposite)
return ((x, y))
At the end of the lesson, he had a working app with 360 degree firing and movement.
import 'dart:html' as html;
import 'package:stagexl/stagexl.dart';
import 'dart:math';
void main() {
makeCanvas("4");
html.querySelector("#button").onClick.listen(grabSize);
}
void makeCanvas(var radius){
var canvas = html.querySelector('#stage');
var stage = new Stage(canvas);
var renderLoop = new RenderLoop();
renderLoop.addStage(stage);
List shapes = new List();
var c_color = Color.Black;
var rad = int.parse(radius);
rad = rad * 4;
print("the radius is $rad");
for (int i = 50; i < 800; i= i + 50){
for (int y = 50; y < 600; y = y +50){
var r = new Random();
int r_dec = r.nextInt(255);
var r_g = new Random();
int g_dec = r_g.nextInt(255);
var b = new Random();
int b_dec = b.nextInt(255);
String a_hex_str = 255.toRadixString(16);
String r_hex_str = r_dec.toRadixString(16);
String g_hex_str = g_dec.toRadixString(16);
String b_hex_str = b_dec.toRadixString(16);
String col_hex = ('0x$a_hex_str$r_hex_str$g_hex_str$b_hex_str');
int col = int.parse(col_hex);
var circ = new Shape();
circ.graphics.circle(i, y, rad);
circ.graphics.fillColor(col);
shapes.add(circ);
}
}
for (var my_shape in shapes){
stage.addChild(my_shape);
}
}
void grabSize(html.MouseEvent event) {
String the_name = (html.querySelector("#name_box") as html.InputElement).value;
the_name = the_name.toUpperCase();
html.querySelector("#name").text = the_name;
String n_radius = (html.querySelector("#name").text);
print(n_radius);
makeCanvas(n_radius);
}
pygame.draw.circle(SCREEN, RED, v_control.center, 50, 2)
pygame.draw.circle(SCREEN, RED, v_control.center, 3)
v_control = pygame.Rect(650, 450, 100, 100)
if v_control.collidepoint(pos):
rad = get_angle(pos, v_control.center)
math.atan, math.sin, and math.cos are in the python math standard library. You'll need to addimport math at the top of your program. opposite = float(center[1] - y)
if x > center[0]:
adjacent = float(x - center[0])
rad = math.atan(opposite/adjacent)
def beam(angle, center):
"""
:param angle: radians calculated from the virtual controller
:return: x,y coordinates of the end-point
Start with the center of the player. The end of the beam is 100 pixels
away from the center. To make a bullet instead of beam, create a class
for bullet and have the hypoteneuse be an attribute that increases
in size. Remember to delete the bullet from the sprite group or list
when it goes off the screen.
"""
hypoteneuse = 30.0
adjacent = math.cos(angle) * hypoteneuse
x = adjacent + center[0]
opposite = math.sin(angle) * hypoteneuse
y = center[1] - opposite
beam_end = (x, y)
return beam_end
class Bullet(pygame.sprite.Sprite):
def __init__(self, angle, p_pos):
YELLOW = (250, 223, 65)
RED = (200, 10, 10)
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((6,6))
pygame.draw.circle(self.image, YELLOW, (3, 3), 3)
pygame.draw.circle(self.image, RED, (3,3), 1)
self.rect = self.image.get_rect()
self.hypotenuse = 30.0
self.angle = angle
self.cent = p_pos
def update(self):
adjacent = math.cos(self.angle) * self.hypotenuse
x = adjacent + self.cent[0]
opposite = math.sin(self.angle) * self.hypotenuse
y = self.cent[1] - opposite
self.rect.center = (x, y)
self.hypotenuse += 5
I/python (21058): Initialize Python for Android I/python (21058): ['/data/data/org.pychildren.accel/files/lib/python2.7/site-packages', '/data/data/org.pychildren.accel/files/lib/site-python'] I/python (21058): Opening APK '/data/app/org.pychildren.accel-1.apk' I/python (21058): (-3.8019924163818359, -0.31603461503982544, 8.7819318771362305) I/python (21058): (-3.8019924163818359, -0.31603461503982544, 8.7819318771362305) I/python (21058): (-3.8019924163818359, -0.31603461503982544, 8.7819318771362305)
craig@ubuntu-desktop:~/Development/dad/screens$ lsdraw.py draw.pyc main.py
adb logcat |grep python
I/python ( 580): Opening APK '/data/app/org.pychildren.surfsc-2.apk'I/python ( 580): Traceback (most recent call last):I/python ( 580): File "main.py", line 323, in <module>I/python ( 580): main()I/python ( 580): File "main.py", line 296, in mainI/python ( 580): pprint.pprint(weather.w_dict)I/python ( 580): NameError: global name 'pprint' is not definedI/python ( 580): Python for android ended.I/ActivityManager( 7163): Process org.pychildren.surfsc:python (pid 580) has died.