Why isn't this custom project working?!?! - PYTHON/PYGAME 3.5

homework

#1

Ok so I am making a basic pong game but I made it in the old python & pygame. I recently updated to python and pygame 3.5. Please may you help me convert my code so it works. Thank you!

import pygame
from pygame.locals import *
from sys import exit
import random

pygame.init()

screen=pygame.display.set_mode((640,480),0,32)
pygame.display.set_caption("Pong Pong!")

#Creating 2 bars, a ball and background.
back = pygame.Surface((640,480))
background = back.convert()
background.fill((0,0,0))
bar = pygame.Surface((10,50))
bar1 = bar.convert()
bar1.fill((0,0,255))
bar2 = bar.convert()
bar2.fill((255,0,0))
circ_sur = pygame.Surface((15,15))
circ = pygame.draw.circle(circ_sur,(0,255,0),(15/2,15/2),15/2)
circle = circ_sur.convert()
circle.set_colorkey((0,0,0))

# some definitions
bar1_x, bar2_x = 10. , 620.
bar1_y, bar2_y = 215. , 215.
circle_x, circle_y = 307.5, 232.5
bar1_move, bar2_move = 0. , 0.
speed_x, speed_y, speed_circ = 250., 250., 250.
bar1_score, bar2_score = 0,0
#clock and font objects
clock = pygame.time.Clock()
font = pygame.font.SysFont("calibri",40)

while True:
    
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
        if event.type == KEYDOWN:
            if event.key == K_UP:
                bar1_move = -ai_speed
            elif event.key == K_DOWN:
                bar1_move = ai_speed
        elif event.type == KEYUP:
            if event.key == K_UP:
                bar1_move = 0.
            elif event.key == K_DOWN:
                bar1_move = 0.
    
    score1 = font.render(str(bar1_score), True,(255,255,255))
    score2 = font.render(str(bar2_score), True,(255,255,255))

    screen.blit(background,(0,0))
    frame = pygame.draw.rect(screen,(255,255,255),Rect((5,5),(630,470)),2)
    middle_line = pygame.draw.aaline(screen,(255,255,255),(330,5),(330,475))
    screen.blit(bar1,(bar1_x,bar1_y))
    screen.blit(bar2,(bar2_x,bar2_y))
    screen.blit(circle,(circle_x,circle_y))
    screen.blit(score1,(250.,210.))
    screen.blit(score2,(380.,210.))

    bar1_y += bar1_move
    
# movement of circle
    time_passed = clock.tick(30)
    time_sec = time_passed / 1000.0
    
    circle_x += speed_x * time_sec
    circle_y += speed_y * time_sec
    ai_speed = speed_circ * time_sec
#AI of the computer.
    if circle_x >= 305.:
        if not bar2_y == circle_y + 7.5:
            if bar2_y < circle_y + 7.5:
                bar2_y += ai_speed
            if  bar2_y > circle_y - 42.5:
                bar2_y -= ai_speed
        else:
            bar2_y == circle_y + 7.5
    
    if bar1_y >= 420.: bar1_y = 420.
    elif bar1_y <= 10. : bar1_y = 10.
    if bar2_y >= 420.: bar2_y = 420.
    elif bar2_y <= 10.: bar2_y = 10.
#since i don't know anything about collision, ball hitting bars goes like this.
    if circle_x <= bar1_x + 10.:
        if circle_y >= bar1_y - 7.5 and circle_y <= bar1_y + 42.5:
            circle_x = 20.
            speed_x = -speed_x
    if circle_x >= bar2_x - 15.:
        if circle_y >= bar2_y - 7.5 and circle_y <= bar2_y + 42.5:
            circle_x = 605.
            speed_x = -speed_x
    if circle_x < 5.:
        bar2_score += 1
        circle_x, circle_y = 320., 232.5
        bar1_y,bar_2_y = 215., 215.
    elif circle_x > 620.:
        bar1_score += 1
        circle_x, circle_y = 307.5, 232.5
        bar1_y, bar2_y = 215., 215.
    if circle_y <= 10.:
        speed_y = -speed_y
        circle_y = 10.
    elif circle_y >= 457.5:
        speed_y = -speed_y
        circle_y = 457.5

    pygame.display.update()

#2

Just running it gives an error message that is pretty clear about what needs to change, perhaps start with that before asking for help?

$ python game.py
Traceback (most recent call last):
  File "game.py", line 21, in <module>
    circ = pygame.draw.circle(circ_sur,(0,255,0),(15/2,15/2),15/2)
TypeError: integer argument expected, got float

#3

Please may you help me, it ran fine on the older version.


#4

There's already a very helpful error message from Python itself which I pasted above.

If that doesn't tell you what you need to change for that particular issue, then why would you want to port it to Python3? Just continue using Python 2.7 if you're not going to change it anyway


#5

I want to use Python3 as it is what my teacher wants me to use. I also do not understand the error message, I am not really familiar with drawing things ect.


#6

The exception raised when running your code isn't related to drawing. Do you know what the difference between a float and an integer is? That's about all you need to know to fix that issue.

So either you do know a bit of Python and you're able to fix that bug (perhaps there's something trickier later, but that's another matter) -- or, you don't and this isn't what you should be doing at all right now, you've got more learning of basics to do first


#7

I dont know about that, ill research. If I cannot fix the problem, may you for me? This is only if I cannot fix it myself

  • Thanks

#8

So an integer is a positive or negative number with no decimal point (I already knew this) and a float is a number which has a decimal point and is used for more precision (I dont know this). How would I use this to help me fix the error?


#9

What does the exception say about the two?

And where does it say it's happening? What modification does therefore need to be done?


#10

It expected a whole number (positive or negative) and it got a number with a decimal in it. I need to fix this on line 21, and I need to remove the /2 which makes it a float. Thanks for guiding me and helping me understand it rather than giving it to me like a baby. Its fixed now :smiley:


#11

Didn't the division by two have some purpose?


#12

It did, it was to make the circle 7.5 so it looked like a circle but 7 will do. Do you know how to change the colour of the ball and the speed of the bar. This was an old project and I have revisited it, Im a bit rusty on python.


#13

15 floordiv 2 is 7
15 / 2        is 7.5
15            is 15

Didn't you just turn 7 into 15 then?

As for colors, seems like something you'd read your code for


#14

Yes. I have changed it back to 7. I have looked at line 23 and changed it to 255,255,255 but it is still green. :stuck_out_tongue:


#15

So Im looking to make a pause button and a menu for my pong game but I cant find how to implement the code.


#16

Drawing a button is a start (you're not motivating why you can't and there are obvious things like "draw button", so that leaves me wondering what you want and I'm not gonna guess. So I'll tell you the obvious thing, because you didn't say it yourself and that's the next thing to consider)


#17

I have created a new post, please may you help me on it.


#18

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.