
As part of a learning exercise for Python I've been porting an old Asteroids clone from Java that I wrote 10 years ago for a digital TV set-top-box.
Here's the first cut of the VectorSprite class and the Stage class. The later sets up the PyGame environment. The VectorSprite class is the basis for all the spites in the game, it handles all the trig functions and currently supports transformation, rotation and scaling.
I'll post the whole game when it's complete as well as posting it on the PyGame site.
# Pythentic Asteroids - vectorsprites.py # # Copyright (C) 2008 Nick Redshaw # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . import pygame, sys, os, math from pygame.locals import * from math import * class VectorSprite: def __init__(self, x, y, vx, vy, pointlist, stage): self.x = x self.y = y self.vx = vx self.vy = vy self.angle = 0 self.pointlist = pointlist # raw pointlist self.rotateAndTransform() self.stage = stage self.color = (255,255,255) self.draw() # roatate each x,y coord by the angle, then translate it to the x,y position def rotateAndTransform(self): newPointList = [self.rotatePoint(point) for point in self.pointlist] self.transformedPointlist = [self.translatePoint(point) for point in newPointList] # draw the sprite def draw(self): self.boundingRect = pygame.draw.aalines(self.stage.screen, self.color, True, self.transformedPointlist) # comment in to see bounding rects #pygame.draw.rect(self.stage.screen, self.color, self.boundingRect, 1) # translate each point to the current x, y position def translatePoint(self, point): newPoint = [] newPoint.append(point[0] + self.x) newPoint.append(point[1] + self.y) return newPoint # Move the sprite by the velocity def move(self): if self.x <> self.x = self.stage.dimensions[0] if self.x > self.stage.dimensions[0]: self.x = 0 if self.y <> self.y = self.stage.dimensions[1] if self.y > self.stage.dimensions[1]: self.y = 0 self.x = self.x + self.vx self.y = self.y + self.vy self.rotateAndTransform() # Rotate a point by the given angle def rotatePoint(self, point): newPoint = [] cosVal = math.cos(radians(self.angle)) sinVal = math.sin(radians(self.angle)) newPoint.append(point[0] * cosVal + point[1] * sinVal) newPoint.append(point[1] * cosVal - point[0] * sinVal) return newPoint # Scale a point def scale(self, point, scale): newPoint = [] newPoint.append(point[0] * scale) newPoint.append(point[1] * scale) return newPoint def collidesWith(self, target): if self.boundingRect.colliderect(target.boundingRect): return True else: return False # Pythentic Asteroids - stage.py # Copyright (C) 2008 Nick Redshaw # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . import pygame, sys, os class Stage: def __init__(self, dimensions, caption): pygame.init() pygame.display.set_mode(dimensions) pygame.display.set_caption(caption) self.screen = pygame.display.get_surface() self.spriteList = [] self.dimensions = dimensions def addSprite(self, sprite): self.spriteList.append(sprite) def removeSprite(self, sprite): self.spriteList.remove(sprite) def drawSprites(self): for sprite in self.spriteList: sprite.draw() def moveSprites(self): for sprite in self.spriteList: sprite.move()
0 comments:
Post a Comment