Oct-05-2017, 05:20 PM
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:
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)