Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Smiley Pong Help
#1
Hello, I am a STEM teacher in an elementary school. My student have been learning python. We have read the Smiley Pong code, from Teach Your Kids to Code, Chapter 9. We wrote the code exactly from the book, yet the program will run, just we get a blank gray screen. when we click to close the window, we get a momentary flash of what the game looks like. We work on MacBook Air and have the most recent version of Python, 3.7. Our Macs run high Sierra. I attached the code below, can anyone please help? Thank you

SmileyPong1.py

import pygame       # Setup

pygame.init()

screen = pygame.display.set_mode([800,600])

pygame.display.set_caption("Smiley Pong")

keepGoing = True

pic = pygame.image.load("CrazySmile.bmp")

colorkey = pic.get_at((0,0))

pic.set_colorkey(colorkey)

picx = 0

picy = 0

BLACK = (0,0,0)

WHITE = (255,255,255)

timer = pygame.time.Clock()

speedx = 5

speedy = 5

paddlew = 200

paddleh = 25

paddlex = 300

paddley = 550

picw = 100

pich = 100

points = 0

lives = 5

font = pygame.font.SysFont("Times", 24)

while keepGoing:    # Game loop

    for event in pygame.event.get():

        if event.type == pygame.QUIT:

            keepGoing = False

    picx += speedx

    picy += speedy

    if picx <= 0 or picx + pic.get_width() >= 800:

        speedx = -speedx

    if picy <= 0:

        speedy = -speedy

    if picy >= 500:

        lives -= 1

        speedy = -speedy

    screen.fill(BLACK)

    screen.blit(pic, (picx, picy))

    # Draw paddle

    paddlex = pygame.mouse.get_pos()[0]

    paddlex -= paddlew/2

    pygame.draw.rect(screen, WHITE, (paddlex, paddley, paddlew, paddleh))

    # Check for paddle bounce

    if picy + pich >= paddley and picy + pich <= paddley + paddleh \

       and speedy > 0:

        if picx + picw / 2 >= paddlex and picx + picw / 2 <= paddlex + \

           paddlew:

            points += 1

            speedy = -speedy

    # Draw text on screen

    draw_string = "Lives: " + str(lives) + " Points: " + str(points)

    text = font.render(draw_string, True, WHITE)

    text_rect = text.get_rect()

    text_rect.centerx = screen.get_rect().centerx

    text_rect.y = 10

    screen.blit(text, text_rect)

    pygame.display.update()

    timer.tick(60)

pygame.quit()      # Exit
Reply
#2
Please put everything into a code block and properly indented. Thanyou
Reply
#3
hey thank very helpful
Reply
#4
I use pygame a lot so I will try my best to help you. I'm at school right now so it may take longer than it would usually for me

I don't see what's wrong with it, when I get home in a few hours I'll test it, unless someone else solves it before then

I don't know for sure and this probably isn't the problem but try using
screen = pygame.display.set_mode((800,600))
rather than this
screen = pygame.display.set_mode([800,600])
Reply
#5
Errors are from double spacing. Python has strict rules with white space.
line 93 and 97 can't be double space.

SimplyPong should been commented out.
# SmileyPong1.py
99 percent of computer problems exists between chair and keyboard.
Reply
#6
used my own image(change it to use your own) and added the end if you reach zero.
best of luck...
#SmileyPong1.py
 
import pygame       # Setup 
pygame.init() 
screen = pygame.display.set_mode([800,600]) 
pygame.display.set_caption("Smiley Pong") 
keepGoing = True 
pic = pygame.image.load("pacMan01.png") 
colorkey = pic.get_at((0,0)) 
pic.set_colorkey(colorkey) 
picx = 0 
picy = 0 
BLACK = (0,0,0) 
WHITE = (255,255,255) 
timer = pygame.time.Clock() 
speedx = 5 
speedy = 5 
paddlew = 200 
paddleh = 25 
paddlex = 300 
paddley = 550 
picw = 100 
pich = 100 
points = 0 
lives = 5
gameOver= False
font = pygame.font.SysFont("Times", 24) 
while keepGoing:   # Game loop
    if lives == 0:
        keepGoing = False
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            keepGoing = False
        
 
    picx += speedx 
    picy += speedy
 
    if picx <= 0 or picx + pic.get_width() >= 800: 
        speedx = -speedx
 
    if picy <= 0: 
        speedy = -speedy
 
    if picy >= 500: 
        lives -= 1 
        speedy = -speedy
 
    screen.fill(BLACK) 
    screen.blit(pic, (picx, picy))
 
    # Draw paddle
 
    paddlex = pygame.mouse.get_pos()[0] 
    paddlex -= paddlew/2 
    pygame.draw.rect(screen, WHITE, (paddlex, paddley, paddlew, paddleh))
 
    # Check for paddle bounce
 
    if picy + pich >= paddley and picy + pich <= paddley + paddleh and speedy > 0: 
        if picx + picw / 2 >= paddlex and picx + picw / 2 <= paddlex +  paddlew:
            points += 1 
            speedy = -speedy
 
    # Draw text on screen
 
    draw_string = "Lives: " + str(lives) + " Points: " + str(points) 
    text = font.render(draw_string, True, WHITE) 
    text_rect = text.get_rect() 
    text_rect.centerx = screen.get_rect().centerx 
    text_rect.y = 10 
    screen.blit(text, text_rect) 
    pygame.display.update() 
    timer.tick(60)
    
 
pygame.quit()      # Exit
Reply
#7
(May-12-2019, 01:47 AM)Jasmineleroy Wrote: if picy + pich >= paddley and picy + pich <= paddley + paddleh \

and speedy > 0:
In addition to Windspar's suggestions, you could alternatively add parenthesis to extend any expression down to the next line as stated in this tutorial. So if you have a long if condition you can prettify it via
    if (picy + pich >= paddley and 
        picy + pich <= paddley + paddleh and
        speedy > 0):
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation pong paddles wont move skullkat232 5 2,286 Feb-13-2023, 03:53 PM
Last Post: Vadanane
  Problem with my pong code Than999 3 3,371 Oct-22-2021, 08:50 AM
Last Post: Qanima
  Game “Pong” I have problems with the code BenBach18 2 3,452 Jan-10-2021, 05:16 PM
Last Post: michael1789
  [PyGame] Creating Pong in Pygame Russ_CW 2 2,777 Oct-11-2020, 11:56 AM
Last Post: Russ_CW
  Trying to make a simple pong game. kevindadmun 1 3,871 Aug-05-2019, 06:39 AM
Last Post: kevindadmun
  [PyGame] How do I add more balls to basic Pong game? JUtah 2 4,372 Apr-18-2019, 08:40 PM
Last Post: JUtah
  [PyGame] Pong game key.event problem erickDarko 2 4,132 Dec-12-2018, 03:17 PM
Last Post: erickDarko

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020