Skip to content

Commit

Permalink
Fix some PEP-8 stuff, and document asteroid smasher.
Browse files Browse the repository at this point in the history
  • Loading branch information
pvcraven committed Jan 11, 2016
1 parent 7e2e6da commit 431f47b
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ build/
a_quick_test.py
*.wpr
arcade.wpu
.coverage
2 changes: 2 additions & 0 deletions arcade/draw_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import OpenGL.GLU as GLU
import OpenGL.GLUT as GLUT


class Texture():
"""
Simple class that represents a texture
Expand All @@ -17,6 +18,7 @@ def __init__(self, id, width, height):
self.width = width
self.height = height


def trim_image(image):
"""
Returns an image with extra whitespace cropped out.
Expand Down
3 changes: 2 additions & 1 deletion arcade/sprite.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ def _set_texture(self, texture):
if type(texture) is Texture:
self._texture = texture
else:
raise SystemError("Can't set the texture to something that is not an instance of the Texture class.")
raise SystemError("Can't set the texture to something that is " +
"not an instance of the Texture class.")

texture = property(_get_texture, _set_texture)

Expand Down
2 changes: 1 addition & 1 deletion arcade/version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
VERSION = "0.0.3"
RELEASE = "0.0.4a1"
RELEASE = "0.0.4a2"
3 changes: 2 additions & 1 deletion arcade/window_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,9 @@ def set_background_color(color):

GL.glClearColor(color[0]/255, color[1]/255, color[2]/255, alpha/255)


def set_keyboard_handler_function(function_pointer):
"""
Sets a function to be run everytime a key even occurs.
"""
GLUT.glutKeyboardFunc(function_pointer)
GLUT.glutKeyboardFunc(function_pointer)
2 changes: 1 addition & 1 deletion doc/source/arcade.color.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
arcade.color package
==================
====================

These are colors you can use when drawing. See http://www.colorpicker.com/color-chart/ for visuals on these colors.

Expand Down
6 changes: 3 additions & 3 deletions doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ Arcade is an easy-to-learn Python library for creating 2d video games. The API i

The source is available on GitHub: https://github.com/pvcraven/arcade

API Documentation
-----------------
Documentation
-------------

.. toctree::
:maxdepth: 4

arcade
examples
installation
arcade

.. automodule:: arcade

Expand Down
72 changes: 65 additions & 7 deletions examples/asteroid_smasher.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,58 @@
"""
Asteroid Smasher
Shoot space rocks in this demo program created with
Python and the Arcade library.
Artwork from http://kenney.nl
"""
import random
import math
import arcade

# By default, the coordinate system goes from -1 to 1. If the
# images are 100 pixels wide, we need to scale them down to fit
# on the screen. This is that scaling factor.
SCALE = 0.0015


class ShipSprite(arcade.Sprite):
"""
Sprite that represents our space ship.
Derives from arcade.Sprite.
"""
def __init__(self, filename, scale):
""" Set up the space ship. """

# Call the parent Sprite constructor
super().__init__(filename, scale)

# Info on where we are going.
# Angle comes in automatically from the parent class.
self.thrust = 0
self.speed = 0
self.max_speed = 0.007
self.drag = 0.0001
self.respawning = 1

# Mark that we are respawning.
respawn()

def respawn(self):
"""
Called when we die and need to make a new ship.
'respawning' is an invulnerability timer.
"""
# If we are in the middle of respawning, this is non-zero.
self.respawning = 1
self.center_x = 0
self.center_y = 0
self.angle = 0

def update(self):
"""
Update our position and other particulars.
"""
if self.respawning:
self.respawning += 1
self.alpha = self.respawning / 500.0
Expand All @@ -46,12 +78,15 @@ def update(self):
self.change_x = -math.sin(math.radians(self.angle)) * self.speed
self.change_y = math.cos(math.radians(self.angle)) * self.speed

""" Call the parent class. """
super().update()


class AsteroidSprite(arcade.Sprite):
""" Sprite that represents an asteroid. """

def update(self):
""" Move the asteroid around. """
super().update()
if self.center_x < -2:
self.center_x = 2
Expand All @@ -64,7 +99,12 @@ def update(self):


class BulletSprite(arcade.TurningSprite):
"""
Class that represents a bullet.
Derives from arcade.TurningSprite which is just a Sprite
that aligns to its direction.
"""
def update(self):
super().update()
if self.center_x < -2.5 or self.center_x > 2.5 or self.center_y > 2.5 \
Expand All @@ -75,22 +115,23 @@ def update(self):
class MyApplication(arcade.ArcadeApplication):
""" Main application class. """

def __init__(self):
pass

def setup_game(self):
""" Set up the game and initialize the variables. """
self.game_over = False

# Sprite lists
self.all_sprites_list = arcade.SpriteList()
self.asteroid_list = arcade.SpriteList()
self.bullet_list = arcade.SpriteList()
self.ship_life_list = arcade.SpriteList()

# Set up the player
self.score = 0
self.player_sprite = ShipSprite("images/playerShip1_orange.png", SCALE)
self.all_sprites_list.append(self.player_sprite)
self.lives = 3

# Set up the little icons that represent the player lives.
cur_pos = -1
for i in range(self.lives):
life = arcade.Sprite("images/playerLife1_orange.png", SCALE)
Expand All @@ -100,6 +141,7 @@ def setup_game(self):
self.all_sprites_list.append(life)
self.ship_life_list.append(life)

# Make the asteroids
image_list = ("images/meteorGrey_big1.png",
"images/meteorGrey_big2.png",
"images/meteorGrey_big3.png",
Expand All @@ -120,17 +162,30 @@ def setup_game(self):
self.asteroid_list.append(enemy_sprite)

def render(self):
"""
Render the screen.
"""

# This command has to happen before we start drawing
arcade.start_render()

# Draw all the sprites.
self.all_sprites_list.draw()

# Put the text on the screen.
output = "Score: {}".format(self.score)
arcade.draw_text(output, -1.0, 0.95, arcade.color.WHITE)

output = "Asteroid Count: {}".format(len(self.asteroid_list))
arcade.draw_text(output, -1.0, 0.9, arcade.color.WHITE)

# This command 'flips' the screen buffer and shows what we have drawn.
arcade.finish_render()

def key_pressed(self, key, x, y):
""" Called whenever a 'normal' key is pressed. """

# Shoot if the player hit the space bar and we aren't respawning.
if not self.player_sprite.respawning and key == b' ':
bullet_sprite = BulletSprite("images/laserBlue01.png", SCALE)

Expand All @@ -149,6 +204,7 @@ def key_pressed(self, key, x, y):
self.bullet_list.append(bullet_sprite)

def special_pressed(self, key, x, y):
""" Called whenever a 'special' key is hit. """
if key == arcade.key.LEFT:
self.player_sprite.change_angle = 3
elif key == arcade.key.RIGHT:
Expand All @@ -159,6 +215,7 @@ def special_pressed(self, key, x, y):
self.player_sprite.thrust = -.0002

def special_released(self, key, x, y):
""" Called whenever a 'special' key is released. """
if key == arcade.key.LEFT:
self.player_sprite.change_angle = 0
elif key == arcade.key.RIGHT:
Expand All @@ -169,10 +226,10 @@ def special_released(self, key, x, y):
self.player_sprite.thrust = 0

def split_asteroid(self, asteroid):
""" Split an asteroid into chunks. """
x = asteroid.center_x
y = asteroid.center_y
self.score += 1
print(self.score)

if asteroid.size == 4:
for i in range(3):
Expand Down Expand Up @@ -236,7 +293,7 @@ def split_asteroid(self, asteroid):
self.asteroid_list.append(enemy_sprite)

def animate(self):

""" Move everything """
if not self.game_over:
self.all_sprites_list.update()

Expand Down Expand Up @@ -268,11 +325,12 @@ def animate(self):
arcade.redisplay()

def run(self):

""" Open the window, set up the game, and run it. """
self.open_window(1400, 1000)
self.setup_game()

arcade.run()

# This is where we start. Make the app and run it.
app = MyApplication()
app.run()
6 changes: 4 additions & 2 deletions examples/drawing_primitives.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,10 @@
arcade.draw_text("draw_bitmap", 0.61, -0.99, arcade.color.BLACK)
texture = arcade.load_texture("images/playerShip1_orange.png")
scale = 0.002
arcade.draw_texture_rect(0.8, -0.6, scale * texture.width, scale * texture.height, texture, 0)
arcade.draw_texture_rect(0.8, -0.8, scale * texture.width, scale * texture.height, texture, 90)
arcade.draw_texture_rect(0.8, -0.6, scale * texture.width,
scale * texture.height, texture, 0)
arcade.draw_texture_rect(0.8, -0.8, scale * texture.width,
scale * texture.height, texture, 90)

# Finish the render.
# Nothing will be drawn without this.
Expand Down

0 comments on commit 431f47b

Please sign in to comment.