I keep getting a Traceback error that says 'pygame.Surface' object is not callable.
Here's the script that I'm working on (it's mostly made by Meloonatic Melons but I'm a beginner trying to learn)
import pygame, sys, time
from Scripts.Textures import *
pygame.init()
cSec = 0
cFrame = 0
FPS = 0
tile_size = 32
FPS_font = pygame.font.Font("C:\\Windows\\Fonts\\Verdana.ttf", 20)
sky = pygame.image.load("Graphics\\Sky.png")
Sky = pygame.Surface(sky.get_size(), pygame.HWSURFACE)
Sky.blit(sky, (0, 0))
del sky
def showFPS():
fps_overlay = FPS_font.render(str(FPS), True, (104, 98, 7))
window.blit(fps_overlay, (0, 0))
def createWindow():
global window, window_width, window_height, window_title
window_width, window_height = 800, 600
window_title = "Mystic RPG"
pygame.display.set_caption(window_title)
window = pygame.display.set_mode((window_width, window_height), pygame.HWSURFACE|pygame.DOUBLEBUF)
def countFPS():
global cSec, cFrame, FPS
if cSec == time.strftime("%S"):
cFrame += 1
else:
FPS = cFrame
cFrame = 0
cSec = time.strftime("%S")
createWindow()
isRunning = True
while isRunning:
for event in pygame.event.get():
if event.type == pygame.QUIT:
isRunning = False
countFPS()
window.blit(Sky (0, 0))
for x in range(0, 640, tile_size):
for y in range(0, 480, tile_size):
window.blit(Tiles.Grass, (x,y))
showFPS()
pygame.display.update()
pygame.quit()
sys.exit()
The error says
Traceback (most recent call last):
File "C:/Users/Zane/Desktop/Python RPG/Mystic RPG.py", line 52, in <module>
window.blit(Sky (0, 0))
TypeError: 'pygame.Surface' object is not callable
I am using Python 3.6.4
Anyone know whats going wrong and how I can fix it.
I am not familiar with this but in the showFPS() function, you wrote this:
Window.blit(fps_overlay, (0, 0))
While the error line, line 52, you write this:
window.blit(Sky (0, 0))
I believe you need a comma between the parameters.
Related
i am kinda new to python and pygame and i'm currently working on fnf-like game in pygame, but when i was trying to make falling arrows, AttributeError: 'pygame.Surface' object has no attribute 'display' appeared. I looked in other people projects, but i couldn't find what's wrong
import pygame, sys, random
pygame.init()
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("Python Night Funkin'")
upImg = pygame.image.load("up.png")
downImg = pygame.image.load("down.png")
rightImg = pygame.image.load("right.png")
leftImg = pygame.image.load("left.png")
test = 0
x = 0
y = 0
arrX = 0
arrY = 0
height = 500
width = 500
arrHeight = 20
arrWidth = 20
arrSpeed = 5
def drawWindow():
win.fill((0,0,0))
pygame.draw.rect(win, (20,20,20), (70,370, 60, 60))
pygame.draw.rect(win, (20,20,20), (170,370, 60, 60))
pygame.draw.rect(win, (20,20,20), (270,370, 60, 60))
pygame.draw.rect(win, (20,20,20), (370,370, 60, 60))
pygame.draw.rect(win, (255, 0, 0), (x-20, y-20, 40, 40))
if arrX == 100:
win.display.blit(rightImg, (arrX-20,arrY-20))
if arrX == 200:
win.display.blit(downImg, (arrX-20,arrY-20))
if arrX == 300:
win.display.blit(upImg, (arrX-20,arrY-20))
if arrX == 400:
win.display.blit(leftImg, (arrX-20,arrY-20))
pygame.display.update()
clock = pygame.time.Clock();
run = True
arrowX = [100, 200, 300, 400]
Arrow = []
while run:
clock.tick(30);
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
win is a Surface and has no display attribute, but a blit function.
So instead of
win.display.blit(rightImg, (arrX-20,arrY-20))
simply use
win.blit(rightImg, (arrX-20,arrY-20))
etc.
I'm working on the snake game using pygame and python3, and I'm getting this error:
Traceback (most recent call last):
File "snakegame.py", line 39, in <module>
main()
File "snakegame.py", line 36, in main
redrawWindow(win)
File "snakegame.py", line 18, in redrawWindow
surface.fill = ((0,0,0))
AttributeError: 'pygame.Surface' object attribute 'fill' is read-only
I'm following a youtube video: https://www.youtube.com/watch?v=CD4qAhfFuLo and he is able to run the program without errors, but I can't. Hopefully you can help, thank you! Here is my program:
import pygame
def drawGrid(w, rows, surface):
sizeBtnw = w // rows
x = 0
y = 0
for l in range(rows):
x = x + sizeBtnw
y = y + sizeBtnw
pygame.draw.line(surface, (255, 255), (x, 0), (x, w))
pygame.draw.line(surface, (255, 255), (0, y), (w, y))
def redrawWindow(surface):
global rows, width
surface.fill = ((0,0,0))
drawGrid(width, rows, surface)
pygame.display.update()
def main():
global width, rows
width = 500
rows = 20
win = pygame.display.set_mode((width, width))
#s = snake((255, 0, 0), (10, 10))
flag = True;
clock = pygame.time.Clock()
while flag == True:
pygame.time.delay(50)
clock.tick(10)
redrawWindow(win)
pass
main()
fill is a function, not a value--you want surface.fill((0,0,0)) rather than surface.fill = ((0,0,0))
Question
I'm pretty new at Python and Pygame so I figured I'd jump right it head first and work on making a little rpg style game. I keep getting the error message below. I keep checking the code and re-watching the tutorial I'm following and I can't seem to find the problem.
I'm sure it's pretty obvious but I don't see the problem. I'm pretty sure I formatted the code in this post right but I don't know.
Error Message
Traceback (most recent call last):
File "C:\Users\Clayton\PycharmProjects\newgame\main.py", line 42, in <module>
gameDisplay.blit(Tiles.grass(x, y))
TypeError: 'pygame.Surface' object is not callable
Main.py
import pygame
from gamescripts.textures import *
# initialize PyGame
pygame.init()
# display information
# int defines number a an integer (ex1)
display_width = int(800)
display_height = int(600)
tile_size = 32
# color definitions
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
skyblue = (135, 206, 235)
yellow = (255, 255, 0)
# window
gameDisplay = pygame.display.set_mode((display_width, display_height),
pygame.HWSURFACE | pygame.DOUBLEBUF)
pygame.display.set_caption('test game')
clock = pygame.time.Clock()
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
print(event)
# render graphics
gameDisplay.fill(skyblue)
for x in range(0, 620, tile_size):
for y in range(0, 480, tile_size):
gameDisplay.blit(Tiles.grass(x, y))
# draws everything to window
pygame.display.update()
# num entered is game fps
clock.tick(60)
# quit PyGame
pygame.quit()
# quit python
quit()
Textures.py
import pygame
pygame.init()
class Tiles:
Size = 32
def load_texture(file, Size):
bitmap1 = pygame.image.load(file)
bitmap2 = pygame.transform.scale(bitmap1, (Size, Size))
surface = pygame.Surface((Size, Size), pygame.HWSURFACE | pygame.SRCALPHA)
surface.blit(bitmap2, (0, 0))
return surface
grass = load_texture('graphics\\grass.png', Size)
You have to create an instance of your Tiles class.
Your Tiles class cannot be directly blitted without creating an instance first.
#create a instance of `Tiles`
tile=Tiles()
tile.load_texture('graphics\\grass.png',32)
Implemented into your code:
import pygame
from gamescripts.textures import *
# initialize PyGame
pygame.init()
# display information
# int defines number a an integer (ex1)
display_width = int(800)
display_height = int(600)
tile_size = 32
# color definitions
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
skyblue = (135, 206, 235)
yellow = (255, 255, 0)
# window
gameDisplay = pygame.display.set_mode((display_width, display_height),
pygame.HWSURFACE | pygame.DOUBLEBUF)
pygame.display.set_caption('test game')
clock = pygame.time.Clock()
#create an instance of your `Tile` class
tile=Tile()
tile.load_texture('graphics\\grass.png',32)
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
print(event)
# render graphics
gameDisplay.fill(skyblue)
for x in range(0, 620, tile_size):
for y in range(0, 480, tile_size):
#choose example coordinates for x and y
gameDisplay.blit(tile,(x,y))
# draws everything to window
pygame.display.update()
# num entered is game fps
clock.tick(60)
# quit PyGame
pygame.quit()
# quit python
quit()
import pygame
pygame.init()
You should change your Tile class as well.
class Tiles:
#initialize your class
def__init__(self):
pass
#ALWAYS use self as the first parameter
def load_texture(self,file, Size):
bitmap1 = pygame.image.load(file)
bitmap2 = pygame.transform.scale(bitmap1, (Size, Size))
surface = pygame.Surface((Size, Size), pygame.HWSURFACE | pygame.SRCALPHA)
surface.blit(bitmap2, (0, 0))
return surface
I suggest you really take a look at the the pygame documentation and the python documentation.
You should also look at SO if your question was already posted. For example, I found this example.
I have been trying to create movement for squares in pygame without controlling it, but I can't seem to do it. My goal was to make the red squares "bounce" the walls. I have cut out the collision in my game to prevent complications.
My code is:
import pygame
r=(255,0,0)
b=(0,0,255)
sort =(0,0,0)
class Block(pygame.sprite.Sprite):
def __init__(self, color=b, width= 40, height= 40):
super(Block, self).__init__()
self.image = pygame.Surface((width, height))
self.image.fill(color)
self.rect = self.image.get_rect()
self.origin_x=self.rect.centerx
self.origin_y=self.rect.centery
def set_position(self, x, y):
self.rect.x=x-self.origin_x
self.rect.y=y-self.origin_y
pygame.init()
pygame.display.set_caption("EsKappa!")
window_size =window_width, window_height = 400, 400
window = pygame.display.set_mode(window_size)
a=(0,201,0)
clock = pygame.time.Clock()
frames_per_second = 60
block_group = pygame.sprite.Group()
a_block= Block(b)
a_block.set_position(window_width/2 -30, window_height/2)
block1=Block()
block2=Block()
block3=Block()
block4=Block()
wall1 = Block(sort,20, 400 )
wall1.set_position(0,200)
wall2= Block(sort,400, 20 )
wall2.set_position(200,0)
wall3=Block(sort,20, 400 )
wall3.set_position(400,200)
wall4=Block(sort,400, 20 )
wall4.set_position(200,400)
block1= Block(r,50, 50 )#here draw
block1.set_position(80, 70)#here place
block2 = Block(r, 50, 40)
block2.set_position(270, 70)
block3 = Block(r,80, 20)
block3.set_position(280, 300)
block4 = Block(r, 30, 70)
block4.set_position(70, 250)
block_group.add(a_block,block1, block2, block3, block4, wall1,wall2,wall3,wall4)
h=pygame.time.get_ticks()/1000
font = pygame.font.SysFont("Times New Roman", 20)
font2 = pygame.font.SysFont("Times New Roman", 50)
text = font.render("Time", True, sort)
speed=[2,0] # here speed
#lukke funktion
running=True
while running:
event = pygame.event.wait ()
if event.type == pygame.QUIT or (event.type== pygame.KEYDOWN) and (event.key == pygame.K_ESCAPE): # Giver lukke funktion fra ikon og ESCAPE
running = False
if event.type == pygame.MOUSEMOTION :
mouse_position = pygame.mouse.get_pos()
a_block.set_position(mouse_position[0],mouse_position[1])
clock.tick(frames_per_second)
window.fill(a)
block1.move_ip(speed) #here error is here
screen.blit(block1) #here
pygame.display.update()
if targetpos[0]+target.get_width()>width or targetpos[0]<0: #here
speed[0]=-speed[0] #here
if pygame.sprite.collide_rect(a_block, block1) or pygame.sprite.collide_rect(a_block, block2) or pygame.sprite.collide_rect(a_block, block3) or pygame.sprite.collide_rect(a_block, block4)or pygame.sprite.collide_rect(a_block, wall1) or pygame.sprite.collide_rect(a_block, wall2) or pygame.sprite.collide_rect(a_block, wall3) or pygame.sprite.collide_rect(a_block, wall4):
time_string = "Du overlevede {} sekunder".format(pygame.time.get_ticks()/1000)
text = font.render(time_string, True, sort)
window.blit(text, (window_width/2-100, window_height/2-100))
if pygame.time.get_ticks()/1000>10:
time_string1 = "Flot!"
text = font2.render(time_string1, True, sort)
window.blit(text, (20, 20))
else:
time_string2 = "Ikke så godt :c"
text = font2.render(time_string2, True, sort)
window.blit(text, window.blit(text, (20, 20)))
pygame.display.update()
pygame.time.wait(5000)
running = False
block_group.draw(window)
pygame.display.update()#opdater skærmen
pygame.quit ()
And the error is:
Traceback (most recent call last):
File "C:\Python33\eskappa1.py", line 81, in <module>
block1.move_ip(speed) #here error is here
AttributeError: 'Block' object has no attribute 'move_ip'
Your Block subclasses Sprite, which per the documentation, doesn't have a move_ip method; this explains the error you are seeing.
I think what you need to do is move the Rect belonging to the Block, which does have that method. Try:
block1.rect.move_ip(speed)
I'm trying to display the message queue but I don't understand why I'm receiving the type error.
The entire error below.
Traceback (most recent call last):
** IDLE Internal Exception:
File "C:\Python27\lib\idlelib\run.py", line 298, in runcode
exec code in self.locals
File "C:\Documents and Settings\Mohammad Raza\Desktop\Python Scripts\messageQueue.py", line 28, in <module>
screen.blit( font.render(text, True, (0,0,0)) (0, y))
TypeError: 'pygame.Surface' object is not callable
Code:
import pygame
from pygame.locals import *
from sys import exit
pygame.init()
SCREEN_SIZE = (800, 600)
screen = pygame.display.set_mode(SCREEN_SIZE,0,32)
font = pygame.font.SysFont("arial",16);
font_height = font.get_linesize()
event_text= []
while True:
event = pygame.event.wait()
event_text.append(str(event))
event_text = event_text[-SCREEN_SIZE[1]/font_height:]
if event.type == QUIT:
exit()
screen.fill((255,255,255))
y = SCREEN_SIZE[1]-font_height
for text in reversed(event_text):
screen.blit( font.render(text, True, (0,0,0)) (0, y))
y -= font_height
pygame.display.update()
Missing comma:
screen.blit( font.render(text, True, (0,0,0)), (0, y))