0

I am having problem with my python game, it's my first project. I am getting an error (TypeError: integer argument expected, got float). It's starting to come when I insert my user-defined function shoot. How to solve this problem or make a SuperMario image shoot a circular object with a class. Thanks :)

import pygame
import time

pygame.init()

display_height = 600
display_width = 800

white = (255,255,255)

gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Igra1')
clock = pygame.time.Clock()

bg = pygame.image.load('background.jpg')
bg = pygame.transform.scale(bg,(800, 600))

Img_SuperMario = pygame.image.load('SuperMario.png')
SuperMario_width = 611
SuperMario_height = 611
Img_SuperMario = pygame.transform.scale(Img_SuperMario, (100, 100))

def shoot(xshoot):
    while xshoot < display_width:
        pygame.draw.circle(gameDisplay, white, (xshoot,50), 20 ,0)
        xshoot += 10

def SuperMario(x,y):
    gameDisplay.blit(Img_SuperMario,(x,y))

def gameloop():

x = (display_width * 0.1)
y = (display_height * 0.1)

x_change = 0
y_change = 0

game_exit = False

while not game_exit:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

        if event.type == pygame.KEYDOWN:
            if  event.key == pygame.K_LEFT:
                x_change = -5
            elif event.key == pygame.K_RIGHT:
                x_change = 5
            elif event.key == pygame.K_UP:
                y_change = -5
            elif event.key == pygame.K_DOWN:
                y_change = 5

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_DOWN or event.key == pygame.K_UP:
                x_change = 0
                y_change = 0


    if x == display_width - 100 or x == 0:
        x_change = 0

    if y == (display_height - 100) or y == 0:
        y_change = 0


    x += x_change
    y += y_change
    xshoot = x

    gameDisplay.fill(white)
    gameDisplay.blit(bg,(0,0))

    if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                while xshoot < display_width:
                    shoot(x)
    SuperMario(x,y)

    pygame.display.update()
    clock.tick(60)

gameloop()
pygame.quit()
quit()
4
  • 2
    Try to incorporate which line the errors occur next time. It will make it easier to trace the error. Commented Dec 26, 2017 at 16:35
  • 1
    always put full error message (Traceback) in question (as text, not screenshot). There are other useful informations. Commented Dec 26, 2017 at 17:59
  • error message should show you where is problem and then you should use print() and print(type(...)) to check values and types in variables to find variable which has float value, and then you should use int(). Commented Dec 26, 2017 at 18:01
  • You need to search for tutorials or other answers about bullets on SO. Your approach with the other while loops won't work. A very basic solution would be to store the bullet positions in a list and add the bullet velocity to the positions every frame. Use for loops to update and draw the bullets. Commented Dec 26, 2017 at 18:11

2 Answers 2

1

The pygame.draw.circle function only accepts integers, but your x variable is a float. Convert the xshoot variable to an integer in the shoot function, e.g.:

pygame.draw.circle(gameDisplay, white, (int(xshoot), 50), 20 ,0)

It looks like you also need to change the while xshoot < display_width: loops in the main loop and the shoot function, but I'm not sure what you want to achieve.

Sign up to request clarification or add additional context in comments.

Comments

0

x = int((display_width * 0.1)) y = int((display_height * 0.1)) try parse to int, maybe will work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.