jeudi 20 avril 2017

How can I get more balls than one from the class Ball in motion at the same time, using the same Ball class?

The Game? Its all in the description!

import pygame
import time
import os
import random

img_path_self = os.path.join('C:\pygrafik\gy.png')
img_path_contender = os.path.join('C:\pygrafik\car.jpg')

windowheight = 400

windowwidth = 640
linewidth = 5
black = 0,0,0
white = 255,255,255

This is the Class im trying to call? for more balls!

class Ball(object):
    def __init__(self):
        self.speed = 1
        self.x = 100
        self.y = 100
        self.dirx = 1        
        self.diry = 1


def coll(self): #collision
    colxl = 0
    colxr = windowwidth - 10
    colyu = 0
    colyd = windowheight - 10

    if self.x == colxr : self.dirx = -1
    if self.x == colxl : self.dirx = 1
    if self.y == colyd : self.diry = -1
    if self.y == colyu : self.diry = 1
    if krock ==1 :
        self.dirx = self.dirx * -1 
        self.diry = self.diry * -1

def handle_ball(self):

    self.speed = 5
    colxl = 0
    colxr = windowwidth - 10
    colyu = 0
    colyd = windowheight - 10
    self.x += self.speed * self.dirx            
    self.y += self.speed * self.diry

def draw(self):
    a = random.randint(10,10) 
    b = random.randint(1,14)
    pygame.draw.circle(screen, (black), (self.x,self.y), 5+a, 1+b)

ai controlled muppet

class Contender(object):
def __init__(self):



self.image = pygame.image.load(img_path_contender)
self.x = 300
self.y = 10

def handle_ai(self): # shaky little weirdo

    colxl = 0
    colxr = windowwidth - 65
    colyu = 50
    colyd = windowheight - 48

    self.dir = random.randint(0,1000) # random direction
    speed = 10

    speedup = 1
    if self.dir > 0 and self.dir <50: #moving right
        if self.x >= colxr: self.x += 0
        else :self.x += speed
    if self.dir > 50 and self.dir < 100: #moving left
        if self.x <= colxl : self.x -=0
        else: self.x -= speed
    if self.dir > 100 and self.dir < 150: #moving down
        if self.y >= colyd : self.y +=0
        else : self.y += speed
    if self.dir > 150 and self.dir < 200: #moving up
        if self.y <= colyu: self.y -=0
        else : self.y -= speed
    if self.x == windowwidth or self.x == 0:
        speed== 0

def draw(self, surface):
    surface.blit(self.image, (self.x, self.y))

ME

class Bird(object):  # more like a car at the moment
    def __init__(self):


    self.image = pygame.image.load(img_path_self)
    # the bird's position
    self.x = 0
    self.y = 0

def handle_keys(self):

    key = pygame.key.get_pressed()
    dist = 10 # distance moved in 1 frame, try changing it to 5
    colxl = 0
    colxr = windowwidth - 65
    colyu = 0
    colyd = windowheight - 48

    if self.x > 150:
        colxr == 0
    if key[pygame.K_DOWN]: # down key
         if self.y >= colyd : self.y +=0
         else: self.y += dist # move down
    elif key[pygame.K_UP]: # up key
         if self.y <= colyu: self.y -=0
         else :self.y -= dist # move up
    if key[pygame.K_RIGHT]: # right key
        if self.x >= colxr: self.x += 0
        else : self.x += dist # move right
    elif key[pygame.K_LEFT]: # left key
         if self.x <= colxl : self.x -=0
         else: self.x -= dist # move left

    def draw(self, surface):

# blit you rself at your current position



    surface.blit(self.image, (self.x, self.y))

callingz all the funktionsz?

pygame.init()
screen = pygame.display.set_mode((windowwidth, windowheight))
ball = Ball()
bird = Bird() 
cont = Contender()
ball = Ball()
clock = pygame.time.Clock()

running = True
while running:
    key = pygame.key.get_pressed()

handle every event since the last frame.

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        pygame.quit() # quit the screen
        running = False

if key[pygame.K_q]:
     pygame.quit() # quit the screen

checking for collision. Could be in a class?

c = pygame.Rect((ball.x, ball.y), (10, 10))
a = pygame.Rect((bird.x, bird.y), (35, 45))
b = pygame.Rect((cont.x, cont.y), (25, 35))
krock = a.colliderect(b) or a.colliderect(c)
if krock == 1:
    print ("BANG BANG")

bird.handle_keys() # handle the keys
cont.handle_ai()
ball.coll()
ball.handle_ball()

screen.fill((white)) # fill the screen with white

bird.draw(screen)# draw the bird to the screen
cont.draw(screen)
ball.draw()

pygame.display.update() # update the screen

pygame.draw.rect(screen, black,(0,0,windowwidth,windowheight),linewidth)
pygame.display.update() # update the screen

clock.tick(30)






Comments
0 Comments

0 commentaires:

Enregistrer un commentaire