'builtin_function_or_method' object has no attribute 'tick'
Question:
I’m trying to use Pygame’s Clock.tick, but it’s not there. I’m positive Pygame has it for Python 3, even if I use dir(pygame.time.Clock)
, it’s still not there. I’m not sure if I’m doing something drastically wrong or Pygame doesn’t have it for Python 3.
Here’s my code:
import pygame, random, sys
from pygame.locals import *
class Pong_Ball(object):
def __init__(self, screen, screen_height, screen_width):
self.screen = screen
self.screen_height = screen_height
self.screen_width = screen_width
def update(self):
#pong ball's properties
self.ball_color = 191,191,191
self.pos_x = 100
self.pos_y = 100
self.vel_x = 1
self.vel_y = 1
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
#clear the screen
self.screen.fill((0,50,100))
#move the pong ball
self.pos_x += self.vel_x
self.pos_y += self.vel_y
#keep the ball on the screen
if self.pos_x > self.screen_width or self.pos_x < 0:
self.vel_x = -self.vel_x
if self.pos_y > self.screen_height or self.pos_y < 0:
self.vel_y = -self.vel_y
#draw the pong ball
self.position = self.pos_x, self.pos_y
pygame.draw.circle(self.screen, self.ball_color, self.position, 10)
#update the display
pygame.time.Clock.tick(60)
pygame.display.update()
def main():
pygame.init()
screen = pygame.display.set_mode((800,600))
ball = Pong_Ball(screen, 600, 800)
ball.update()
main()
Answers:
Ah. The documentation, though perfectly standard, could be a little confusing. Even though they write Clock.tick
, they don’t mean that it’s a staticmethod of the class. You call it on an instance:
>>> import pygame
>>> clock = pygame.time.Clock()
>>> clock
<Clock(fps=0.00)>
>>> clock.tick
<built-in method tick of Clock object at 0xb402b408>
>>> clock.tick()
14487
>>> clock.tick()
836
To confirm that it’s behaving:
>>> import time
>>> for i in range(5):
... print clock.tick()
... time.sleep(0.17)
...
22713
172
171
171
172
replace pygame.time.Clock.tick(60)
with pygame.time.Clock().tick(60)
.
replace pygame.time.Clock.tick(60) with pygame.time.Clock().tick(60)
this worked for me
I’m trying to use Pygame’s Clock.tick, but it’s not there. I’m positive Pygame has it for Python 3, even if I use dir(pygame.time.Clock)
, it’s still not there. I’m not sure if I’m doing something drastically wrong or Pygame doesn’t have it for Python 3.
Here’s my code:
import pygame, random, sys
from pygame.locals import *
class Pong_Ball(object):
def __init__(self, screen, screen_height, screen_width):
self.screen = screen
self.screen_height = screen_height
self.screen_width = screen_width
def update(self):
#pong ball's properties
self.ball_color = 191,191,191
self.pos_x = 100
self.pos_y = 100
self.vel_x = 1
self.vel_y = 1
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
#clear the screen
self.screen.fill((0,50,100))
#move the pong ball
self.pos_x += self.vel_x
self.pos_y += self.vel_y
#keep the ball on the screen
if self.pos_x > self.screen_width or self.pos_x < 0:
self.vel_x = -self.vel_x
if self.pos_y > self.screen_height or self.pos_y < 0:
self.vel_y = -self.vel_y
#draw the pong ball
self.position = self.pos_x, self.pos_y
pygame.draw.circle(self.screen, self.ball_color, self.position, 10)
#update the display
pygame.time.Clock.tick(60)
pygame.display.update()
def main():
pygame.init()
screen = pygame.display.set_mode((800,600))
ball = Pong_Ball(screen, 600, 800)
ball.update()
main()
Ah. The documentation, though perfectly standard, could be a little confusing. Even though they write Clock.tick
, they don’t mean that it’s a staticmethod of the class. You call it on an instance:
>>> import pygame
>>> clock = pygame.time.Clock()
>>> clock
<Clock(fps=0.00)>
>>> clock.tick
<built-in method tick of Clock object at 0xb402b408>
>>> clock.tick()
14487
>>> clock.tick()
836
To confirm that it’s behaving:
>>> import time
>>> for i in range(5):
... print clock.tick()
... time.sleep(0.17)
...
22713
172
171
171
172
replace pygame.time.Clock.tick(60)
with pygame.time.Clock().tick(60)
.
replace pygame.time.Clock.tick(60) with pygame.time.Clock().tick(60)
this worked for me