0

When I run the program it literally runs for about 1 second then stops but nothing pops up when I run it. I asked a question about this yesterday and people told me to just make classes of each object so i'm trying that but now nothing is popping up. Anyone might know what the problem is here?

import sys
import pygame
import math
import random


class Paddle():
    def __init__(self, background, x_axis, height):
        self.screen = screen
        (sw, sh) = screen.get_size()
        self.x = x - 25
        self.y = sh / 2 - self.h / 2
        self.h = h
        self.w = 20
        self.max_y = sh - height
        self.min_y = 0
        self.speed = 0

    def moving(self, amount):
        self.speed = amount
        self.y = self.y + amount
        if self.y > self.max_y:
            self.y = self.max_y
        elif self.y < self.min_y:
            self.y = self.min_y

    def draw(self):
        pygame.draw.rect(self.screen, (255, 255, 255), (self.x, self.y, self.w, self.h))


class Ball():
    def __init__(self, screen):
        self.screen = screen
        self.w = 50
        self.h = 50
        self.playClick = False
        self.reset()

    def reset(self):
        (sw, sh) = self.screen.get_size()
        self.x = sw / 2 - 25
        self.y = sh / 2 - 25
        angle = random() * math.radians(20)
        speed = 10
        self.dir_x = speed * math.cos(ball_angle)
        self.dir_y = speed * math.sin(ball_angle)
        if random() > 0.5:
            self.dir_x = -self.dir_x
        elif random() > 0.5:
            self.dir_y = -self.dir_y
        self.timer = 2

    def updating(self, time, p1, p2):
        if self.timer > 0:
            self.timer -= time
        else:
            self.x += self.dir_x
            self.y += self.dir_y
        (sw, sh) = self.screen.get_size()

        if self.x_axis + 50 < 0:
            self.reset()
        elif self.x_axis > side_width:
            self.reset()
        else:
            if (self.y < 0 and self.dir_y < 0) or (self.y + self.h > sh and self.dir_y > 0):
                self.dir_y = -self.dir_y
                self.playClick = True

            selfrect = pygame.Rect(self.x_axis, self.y_axis, self.width, self.height)
            p1rect = pygame.Rect(p1.x, p1.y, p1.w, p1.h)
            p2rect = pygame.Rect(p2.x, p2.y, p2.w, p2.h)
            if selfrect.colliderect(p1rect) and self.dir_x < 0:
                self.dir_x = -self.dir_x
                self.dir_y += p1.speed
                self.playClick = True
            if selfrect.colliderect(p2rect) and self.dir_x > 0:
                self.dir_x = -self.dir_x
                self.dir_y += p2.speed
                self.playClick = True
            if self.dir_y > 15:
                self.dir_y = 15
            elif self.dir_y < -15:
                self.dir_y = -15

    def draw(self, click):
        pygame.draw.rect(self.screen, (255, 255, 255), (self.x, self.y, self.w, self.h))
        if self.playclick:
            click.play()
            self.playClick = False



def main():
    pygame.init()
    screen = pygame.display.set_mode((1000, 800))
    clock = pygame.time.Clock()
    p1 = Paddle(screen, 100, 150)
    p2 = Paddle(screen, 900, 150)
    ball = Ball(screen)
    background_image = pygame.image.load("RPGMAP.png").convert()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w:
                p1.moving(-10)
            if event.key == pygame.K_s:
                p1.moving(10)
            if event.key == pygame.K_UP:
                p2.moving(-10)
            if event.key == pygame.K_DOWN:
                p2.moving(10)

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_w:
                p1.moving(0)
            if event.key == pygame.K_s:
                p1.moving(0)
            if event.key == pygame.K_UP:
                p2.moving(0)
            if event.key == pygame.K_DOWN:
                p2.moving(0)

        ball.update(1/60, p1, p2)
        screen.blit(background_image, [0, 0])
        p1.draw()
        p2.draw()
        pygame.display.flip()
        clock.tick(60)
        main()
Slim
  • 5
  • 2
  • If you'd like to see my other code and possibly help me with that i'll put the link here... – Slim May 01 '15 at 14:54
  • http://stackoverflow.com/questions/29978109/pygame-collision-detection-for-pong-and-keeping-the-paddles-on-screen?noredirect=1#comment48075950_29978109 – Slim May 01 '15 at 14:54

1 Answers1

0

You don't declare screen anywhere:

self.screen = screen <- NameError

You also don't pass x or h:

 self.x = x - 25 <- NameError
 self.h = h <- NameError

You also have numerous other variables and attributes you are trying to use that are not declared anywhere.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321