#pygame Test by KNovak import pygame, sys, os, math from pygame.locals import * pygame.init() pygame.key.set_repeat(1) fullscreen = False; #startup settings for arg in sys.argv: if arg == "-f": fullscreen = True #Video Display information vidinfo = pygame.display.Info() if fullscreen == True: vidsize = (vidinfo.current_w, vidinfo.current_h) window = pygame.display.set_mode(vidsize, pygame.FULLSCREEN) else: vidsize = (640,480) window = pygame.display.set_mode(vidsize) pygame.display.set_caption('KNova Test Window') screen = pygame.display.get_surface() background = pygame.Surface((screen.get_width(), screen.get_height())) #colors WHITE = (255,255,255) BLUE = (0,0,150) GREEN = (0,150,0) #text def drawText (text, color, xloc, yloc, size): fontObj = pygame.font.Font('freesansbold.ttf', size) textSurface = fontObj.render(text, True, color) textRectObj = textSurface.get_rect() textRectObj.center = (xloc, yloc) return textSurface, textRectObj #Sprite Class for the Ship Image class Ship(pygame.sprite.Sprite): image = pygame.image.load("./resources/ship.png") image = image.convert() #initialization function def __init__(self, position=(100,100)): pygame.sprite.Sprite.__init__(self,self.groups) self.pos = position self.orig_image = Ship.image self.image = self.orig_image self.rect = self.image.get_rect() self.rect.center = self.pos self.angle = 0 self.moveangle = 0 self.vx = 0 self.vy = 0 self.maxvel = 3 #move function def move(self, newpos): oldx, oldy = self.pos dx, dy = newpos self.pos = (oldx - dx, oldy - dy) self.rect.center = self.pos #update function which will replace both move and rotate def update(self): #handle rotations if self.angle >= 360: self.angle = 0 if self.angle <= -360: self.angle = 0 loc = self.rect.center self.image = pygame.transform.rotate(self.orig_image, self.angle) self.rect = self.image.get_rect() self.rect.center = loc #handle 2D movement self.move( (self.vx, self.vy) ) #boundaries if self.pos[0] > vidsize[0]: #right self.vx = -self.vx if self.pos[0] < 0: #left self.vx = -self.vx if self.pos[1] > vidsize[1]: #bottom self.vy = -self.vy if self.pos[1] < 0: #top self.vy = -self.vy spriteGroup = pygame.sprite.Group() Ship.groups = spriteGroup newShip = Ship() #sprite group update def spriteUpdate(group, screen, background): group.clear(screen, background) group.update() group.draw(screen) #trig functions for updating position def getDirection(angle,velx, vely): x = math.sin(math.radians(angle)) * velx y = math.cos(math.radians(angle)) * vely return (x,y) #get keyboard events def pyin(events): for event in events: if event.type == QUIT: sys.exit(0) if event.type == KEYDOWN: key = pygame.key.get_pressed() if (key[pygame.K_ESCAPE]): sys.exit(0) if (key[K_UP]): newShip.vx += math.sin( math.radians( newShip.angle ) ) newShip.vy += math.cos( math.radians( newShip.angle ) ) if newShip.vx >= newShip.maxvel: newShip.vx = newShip.maxvel if newShip.vy >= newShip.maxvel: newShip.vy = newShip.maxvel if (key[K_DOWN]): newShip.vx -= math.sin( math.radians( newShip.angle ) ) newShip.vy -= math.cos( math.radians( newShip.angle ) ) if newShip.vx <= (0-newShip.maxvel): newShip.vx = (0-newShip.maxvel) if newShip.vy <= (0-newShip.maxvel): newShip.vy = (0-newShip.maxvel) if (key[K_LEFT]): newShip.angle += 4 if (key[K_RIGHT]): newShip.angle -= 4 while True: pyin(pygame.event.get()) # pykey(pygame.key.get_pressed()) pygame.display.update() spriteUpdate(spriteGroup, screen, background)