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))
Related
I'm trying to make a progress bar with pygame by writing the following code:
import pygame
pygame.init()
window = pygame.display.set_mode((pygame.display.Info().current_w, pygame.display.Info().current_h))
text = ["Reading data ...", "Setting configs ...", "Loading files ...", "Download complete"]
textIndex = 0
font = pygame.font.SysFont("Arial", 30)
y2 = 400
while True:
for event in pygame.event.get( ):
if event.type == pygame.QUIT:
quit()
text = font.render(text[textIndex], False, (0, 255, 0))
length = 30 * len(text[textIndex])
x2 = (pygame.display.Info().current_w / 2) - length / 2
window.blit(text, (x2, y2))
pygame.display.flip()
However, it gives me this pygame error message:
Traceback (most recent call last):
File "", line 16, in <module>
length = 30 * len(text[textIndex])
TypeError: 'pygame.Surface' object is not subscriptable
This message comes with a line that has nothing to do with pygame, can anyone tell me how to correct this?
You cannot use the same variable name for the list of strings and the rendered text Surface. Change the name:
text = font.render(text[textIndex], False, (0, 255, 0))
text_surf = font.render(text[textIndex], False, (0, 255, 0))
window.blit(text, (x2, y2))
window.blit(text_surf, (x2, y2))
I'm making a game called Bunnies and Badgers and this is the code I have so far
import pygame
from pygame.locals import *
pygame.init()
width, height = 640, 480
screen = pygame.display.set_mode((width, height))
player = pygame.image.load("resources/images/dude.png")
grass = pygame.image.load("resources/images/grass.png")
castle = pygame.image.load("resources/images/castle.png")
while 1:
screen.fill(0)
screen.blit(player, (100, 100))
for x in range(width/grass.get_width()+1):
for y in range(height/grass.get_height()+1):
screen.blit(grass,(x*100, y*100))
screen.blit(castle,(0,30))
screen.blit(castle,(0, 135))
screen.blit(castle,(0, 240))
screen.blit(castle,(0, 345))
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit(0)
However, when I run the module, I get this error
Traceback (most recent call last):
File "C:\Users\Ben\Documents\U Game Online\Games\Bunnies and Badgers\game.py", line 12, in <module>
for x in range(width/grass.get_width()+1):
TypeError: 'float' object cannot be interpreted as an integer
I don't see any errors with the code given with the error.
The range function requires an integer argument. Just case the parameter to int when calling range.
for x in range(int(width/grass.get_width()+1)):
for y in range(int(height/grass.get_height()+1)):
screen.blit(grass,(x*100, y*100))
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))
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.
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)