Python Forum
[PyGame] TypeError: unsupported operand type(s) for -: 'int' and 'instancemethod'
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] TypeError: unsupported operand type(s) for -: 'int' and 'instancemethod'
#1
I'm trying to program a snake that's controlled by WASD keys and has collision detection. I also want it to turn in the direction it's moving. If you see any bugs or ways to optimize my code, please tell me. I'm getting this error:
File "C:\Users\asay_915876\Desktop\python\good\main.py", line 73, in <module>
x = width/2-snek.snek_width
TypeError: unsupported operand type(s) for -: 'int' and 'instancemethod'

Here's the code:
# -*- coding: utf-8 -*-
import pygame
from pygame.locals import *
from pygame.time import *
import os, sys
import time
import math


image_resources = "C:/Users/asay_915876/Desktop/Python/good/images/"

width,height = 680,512
size = (width,height)
FPS = 70

class GetSource:
    def snek(self,image):
        return pygame.image.load(image_resources + image).convert_alpha()
    def wall(self,image):
        return pygame.image.load(image_resources + image).convert()

class border(pygame.sprite.Sprite):
    def __init__(self,color,x,y,width,height):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((width,height))
        self.image.fill(pygame.color.Color(color))
        self.image = self.image.get_width()
        self.image = self.image.get_height()
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

class snek(pygame.sprite.Sprite):

    def __init__(self,image):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('snek.jpg')
        snek_width = get_width
        snek_height = get_height
        self.rect = self.image.get_rect()
        self.x = 340
        self.y = 256

    def snek_width(self, width):
        snek_width = get_width

    def snek_height(self, width):
        snek_height = get_height


    def handle_keys(self):
        key = pygame.key.get_pressed()
        speed = 4
        if key[pygame.K_w]:
            self.y -= speed
        if key[pygame.K_s]:
            self.y += speed
        if key[pygame.K_d]:
            self.x += speed
        if key[pygame.K_a]:
            self.x -= speed

    def draw(self, Surface):
        self.image.blit(snek, (self.x, self.y))

    def update(self, time):
        self.angle = math.atan2(-self.dx, -self.dy)/math.pi*180.0
        self.image = pygame.transform.rotozoom(self.image0,self.angle,1.0)

pygame.init()

screen = pygame.display.set_mode(size)
x = width/2-snek.snek_width
y = height/2-snek.snek_height
snek = snek()
Clock = pygame.time.Clock()
pygame.display.set_caption('lit')


running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            running = False

    snek.handle_keys()
    screen.fill((255,255,255))
    snek(x,y)
    snek.draw(screen)
    pygame.display.update()

    pygame.display.update()
    Clock.tick(FPS)
Reply


Messages In This Thread
TypeError: unsupported operand type(s) for -: 'int' and 'instancemethod' - by SnekLover123 - Oct-05-2017, 05:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] PLEASE HELP! TypeError: unsupported operand type(s) for +: 'pygame.Surface' and 'int' keyfive 1 5,314 Jun-19-2018, 01:20 PM
Last Post: volcano63

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020