lewashby 56 Junior Poster

In the following program I'M getting an error with the blit function. The error is invalid destination position.

background_image_filename = 'sushiplate.jpg'
sprite_image_filename = 'fugu.png'

import pygame
from pygame.locals import *
from sys import exit
from gameobjects import Vectory2

pygame.init()

screen = pygame.display.set_mode((640, 480), 0, 32)
background = pygame.image.load(sprite_image_filename).convert_alpha()
sprite = pygame.image.load(sprite_image_filename).convert_alpha()

clock = pygame.time.Clock()

sprite_pos = Vectory2(200, 150)
sprite_speed = 300.

while True:
    
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
            
    pressed_key = pygame.key.get_pressed()
    
    key_direction = Vectory2(1, 0)
    
    if pressed_key[K_LEFT]:
        key_direction.x = -1
    elif pressed_key[K_RIGHT]:
        key_direction.x = + 1
    if pressed_key[K_UP]:
        key_direction.y = - 1
    elif pressed_key[K_DOWN]:
        key_direction.y = + 1
        
    key_direction.normalize()
    
    screen.blit(background, (0,0))
    screen.blit(sprite, sprite_pos)
    
    time_passed = clock.tick(30)
    time_passed_seconds = time_passed / 1000.0
    
    sprite_pos += key_direction * sprite_speed * time_passed_seconds
    
    pygame.display.update()

gameobjects.py

import math

class Vectory2(object):
    
    def __init__(self, x = 0.0, y = 0.0):
        self.x = x
        self.y = y
        
    def __str__(self):
        return "(%s, %s)" % (self.x, self.y)
    
    @staticmethod
    def from_points(P1, P2):
        return Vectory2(P2[0] - P1[0], P2[1] - P1[1])
    
    def get_magnitude(self):
        return math.sqrt(self.x**2 + self.y**2)
    
    def normalize(self):
        magnitude = self.get_magnitude()
        self.x /= magnitude
        self.y /= magnitude
        
    # rhs stands for right hand side
    def __add__(self, rhs):
        return Vectory2(self.x + rhs.x, self.y + rhs.y)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.