Please help. I've been searching but i still don't quite grasp how to fix the problem. I don't know if i;m forgetting something or the values of x and y are not considered int's for some reason (i have put in past tries to fix it: int(x))
I want to create the dinosaur game from Google, but the .blit of the "Hidrante" class is failing (but the player one is functioning). I have put it normal values instead of self.x and self.y but still get the problem, i've tried to said it x and y are ints, tried to change the names, the image i'm loading, and replicate the code of the player but doesn't seem to work.
here is all the code
import pygame
pygame.init()
window = pygame.display.set_mode((1000, 1000))
pygame.display.set_caption("Dinosaurio")
clock = pygame.time.Clock()
char = pygame.image.load('dino.png')
bg = pygame.image.load('bg2.jpg')
img = pygame.image.load("H2.jpg")
class Player(object):
#values of player
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.isJump = False
self.jumpCount = 10
self.hitbox = (self.x + 20, self.y, 30, 64)
#putting the player on screem
def draw(self, window):
window.blit(char, (self.x, self.y))
self.hitbox = (self.x + 20, self.y, 30, 64)
pygame.draw.rect(window, (255, 0, 0), self.hitbox, 2)
#Obstacle
class Hidrante(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self. height = height
self.vel = 8
self.hitbox = (self.x + 17, self.y + 2, 31, 57)
self.walkCount = 10
self.path = 0
#putting the obstacle in screem
def draw(self, win):
if self.walkCount + 1 >= 33: #fill code, trying things not actual util
self.walkCount = 0
if self.vel > 0:
window.blit(img, (self.x, self.y)) #here is the problem
self.walkCount += 1
else:
window.blit(img, (self.x, self.y))#these too
self.walkCount += 1
self.hitbox = (self.x + 17, self.y + 2, 30, 60)
pygame.draw.rect(window, (255, 0, 0), self.hitbox, 2)
def gmv():
#putting on creem the objects
window.blit(bg, (0, 0))
dino.draw(window)
hidrante.draw(window)
pygame.display.update()
#creating the objects
dino = Player(110, 620, 64, 64)
hidrante = Hidrante(1100, 620, 64, 64)
run = True
neg = 1
while run:
clock.tick(30) #frames
hidrante.x = (hidrante.x ** 2) * -1 #Function to not jump infinit.
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#Space to jump
keys = pygame.key.get_pressed()
if not (dino.isJump):
if keys[pygame.K_SPACE]:
dino.isJump = True
else:
if dino.jumpCount >= -10:
dino.y -= (dino.jumpCount * abs(dino.jumpCount)) * 0.5
dino.jumpCount -= 1
else:
dino.jumpCount = 10
dino.isJump = False
gmv()
pygame.quit()
i've only want to know the reason why the values of object are interfering with .blit and learn how to fix it.
The coordinates which are passed to pygame.Surface.blit() have to be in a certain limit.
It seems that the limit for a coordinate has to be within the range of the (ctypes) data type int, which is from -2147483648 to 2147483647.
You exceed this limit in the object hidrante, because of the line:
hidrante.x = (hidrante.x ** 2) * -1
Probably this formula doesn't do what you expect it to do.
Anyway, validate if hidrante.x is in bounds of thee screen, to solve the issue:
class Hidrante(object):
# [...]
def draw(self, win):
if self.walkCount + 1 >= 33: #fill code, trying things not actual util
self.walkCount = 0
# validate if object is in the window
if self.x > -30 and self.x < 1000:
if self.vel > 0:
window.blit(img, (self.x, self.y))
self.walkCount += 1
else:
window.blit(img, (self.x, self.y))
self.walkCount += 1
self.hitbox = (self.x + 17, self.y + 2, 30, 60)
pygame.draw.rect(window, (255, 0, 0), self.hitbox, 2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With