0

I wrote a game with Pygame on my Raspberry Pi. Then, the calculations that it had to do every frame dropped the frame rate significantly (5 fps instead of the intended 60 fps). I have a stronger computer that I want to try running it on, but there isn't a version of pygame that includes pygame.math that I can install on the operating system (Mac OS X 10.7.1). All I need is pygame.math.Vector2, so is there anywhere that I could find the source code for it?

Below is a class that I wrote. I attempted to make it behave exactly like pygame.math.Vector2.

import math

class Vec():
    def __init__(self, x, y=None):
        if y == None:
            x, y = x[0], x[1]
        self.x = float(x)
        self.y = float(y)

    def __iter__(self):
        vals = [self.x, self.y]
        return iter(vals)

    def __add__(self, other):
        x = self.x + other.x
        y = self.y + other.y
        return Vec(x, y)

    def __sub__(self, other):
        x = self.x - other.x
        y = self.y - other.y
        return Vec(x, y)

    def __mul__(self, other):
        if type(other) == type(self):
            x = self.x * other.x
            y = self.y * other.y
            return x + y
        else:
            x = self.x * other
            y = self.y * other
            return Vec(x, y)

    def rotate(self, angle):
        ox, oy = 0, 0
        px, py = self.x, self.y

        qx = ox + math.cos(angle) * (px - ox) - math.sin(angle) * (py - oy)
        qy = oy + math.sin(angle) * (px - ox) + math.cos(angle) * (py - oy)
        return Vec(qx, qy)

    def angle_to(self, other):
        return math.degrees(math.asin((self.x * other.y - self.y * other.x)/(self.length()*other.length())))

    def length(self):
        return math.sqrt(self.x**2 + self.y**2)

However, it did not work and raised errors when the vector was used in mathematical operations and when a rectangle point was assigned to equal the vector.

1 Answers1

0

PyGame is based on SDL and is mainly written in the C programming language. See About - wiki.
The PyGame GitHub repository can be found at pygame / pygame.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174