0
votes

im making a very basic game to learn pygame and im trying to make it so that the duck (player) has to dodge the rocks but i cant get the rocks to randomly be placed and then scroll to the side

Here is my current code:

import pygame
import os
import random


img_path = os.path.join('C:\Python27', 'player.png')
img_path2 = os.path.join('C:\Python27', 'rock.png')


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

        self.image = pygame.image.load(img_path)
        self.x = 0
        self.y = 0


def handle_keys(self):
    """ Handles Keys """
    key = pygame.key.get_pressed()
    dist = 3 
    if key[pygame.K_DOWN]: 
        self.y += dist
    elif key[pygame.K_UP]: 
        self.y -= dist 
    if key[pygame.K_RIGHT]: 
        self.x += dist
    elif key[pygame.K_LEFT]: 
        self.x -= dist 




    def draw(self, surface):


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

    def background(self, surface):
        bg = os.path.join('C:\Python27', 'bg.png')
        self.image2 = pygame.image.load(bg)
        surface.blit(self.image2, (0,0))

class Rock(object): 
    def __init__(self):
        self.image = pygame.image.load(img_path2)


        self.x = 640
        self.y = 0
    def rock(self):
        dist = 2
        if running == True:
            self.x -=dist


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






pygame.init()
screen = pygame.display.set_mode((640, 400))

bird = Bird() # create an instance
rock = Rock()
clock = pygame.time.Clock()

running = True
while running:

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

    bird.handle_keys() 
    rock.rock()

    screen.fill((255,255,255)) 
    bird.background(screen)
    bird.draw(screen)
    rock.rock_draw(screen)
    pygame.display.update() 

    clock.tick(40)

I want the rock to start on the right side off the screen and scroll and then once the first one is gone or halfway gone spawn another at a different y position

1
So what kind of behavior are you getting currently? - Pace
its just starting in the top right corner and moving left - Serial

1 Answers

4
votes

First you should change your Rock constructor to allow position arguments, and also fix up your rock function a bit:

class Rock(object): 
   def __init__(self, x=640, y=0, dist=2):
       self.image = pygame.image.load(img_path2)
       self.x = x
       self.y = y
       self.dist = dist

   def rock(self):
       # you don't need to check if running is true here, you're doing that in your loop
       self.x -= dist

Then find a random int value for the y coordinate of a rock, and create a rock there:

import random

y = random.randint(0, 400)
rock = Rock(640, y)

In your loop:

if rock.x < 0:
    y = random.randint(0, 400)
    rock = Rock(640, y)

rock.rock()

This checks if the rock has hit the left side of the screen, and if it has, it creates a new rock. Then, either way, it calls rock() on that Rock object.