Thitorkin Skrevet 6. februar 2011 Del Skrevet 6. februar 2011 Hva feiler denne koden? Følger en tutorial from pygame import * from pygame.locals import * from vec2d import * from math import e, pi, cos, sin, sqrt from random import uniform class Starter(PygameHelper): def __init__(self): self.w, self.h = 800, 600 PygameHelper.__init__(self, size=(self.w, self.h), fill=((255,255,255))) self.pos= vec2d(400, 300) self.target= vec2d(300,300) def update(self): dir= self.target- self.pos if dir.length>3: dir.length= 3 self.pos= self.pos+ dir def keyUp(self, key): pass def mouseUp(self, button, pos): #I messed up a bit here in the video. Old line was #self.target= pos #pos here is a TUPLE. But we want self.target to be a vec2d #so instead first create vec2d object from the tuple, then assign self.target= vec2d(pos) def mouseMotion(self, buttons, pos, rel): pass def draw(self): self.screen.fill((255,255,255)) pygame.draw.circle(self.screen, (255,0,0), self.target, 30, 1) pygame.draw.circle(self.screen, (0,0,0), self.pos, 21) pygame.draw.circle(self.screen, (200,200,255), self.pos, 20) s = Starter() s.mainLoop(40) Inne i folderen ligger også "pygame.py" import pygame from pygame.locals import * #float range. Start=a, End=b, Step=c def frange(a, b, c): t = a while t < b: yield t t += c def drawGraph(screen, arr, step=5): maxy = screen.get_height() for i in range(len(arr)-1): x = i*step p1 = (i*step, maxy-arr[i]) p2 = ((i+1)*step, maxy-arr[i+1]) pygame.draw.line(screen, (0,0,0), p1, p2) class PygameHelper: def __init__(self, size=(640,480), fill=(255,255,255)): pygame.init() self.screen = pygame.display.set_mode(size) self.screen.fill(fill) pygame.display.flip() self.running = False self.clock = pygame.time.Clock() #to track FPS self.size = size self.fps= 0 def handleEvents(self): for event in pygame.event.get(): if event.type == QUIT: self.running = False elif event.type == KEYDOWN: self.keyDown(event.key) elif event.type == KEYUP: if event.key == K_ESCAPE: self.running = False self.keyUp(event.key) elif event.type == MOUSEBUTTONUP: self.mouseUp(event.button, event.pos) elif event.type == MOUSEMOTION: self.mouseMotion(event.buttons, event.pos, event.rel) #wait until a key is pressed, then return def waitForKey(self): press=False while not press: for event in pygame.event.get(): if event.type == KEYUP: press = True #enter the main loop, possibly setting max FPS def mainLoop(self, fps=0): self.running = True self.fps= fps while self.running: pygame.display.set_caption("FPS: %i" % self.clock.get_fps()) self.handleEvents() self.update() self.draw() pygame.display.flip() self.clock.tick(self.fps) def update(self): pass def draw(self): pass def keyDown(self, key): pass def keyUp(self, key): pass def mouseUp(self, button, pos): pass def mouseMotion(self, buttons, pos, rel): pass cmd hopper opp, og legger seg raskt ned igjen... Lenke til kommentar
Thitorkin Skrevet 6. februar 2011 Forfatter Del Skrevet 6. februar 2011 Måtte slette vekk 2.7 versjonen av python og bare beholde python26 folderen. Lenke til kommentar
Anbefalte innlegg
Opprett en konto eller logg inn for å kommentere
Du må være et medlem for å kunne skrive en kommentar
Opprett konto
Det er enkelt å melde seg inn for å starte en ny konto!
Start en kontoLogg inn
Har du allerede en konto? Logg inn her.
Logg inn nå