简体   繁体   中英

Blitting in new x position Pygame

Im writing a basic mouse dodge game and i need to blit little stars to dodge. i made it so that once one star leaves the screen it blits it again at the top> that works but it blits it in the same x position

Here is the update code that moves the star:

def update(self):
    self.mouse_pos = pygame.mouse.get_pos()
    (self.player_r.x, self.player_r.y) = self.mouse_pos

    self.star_r.x = ran_x
    self.star_r.y += 2
    if self.star_r.y > 640:
        self.star_r.y = 0

Here is where the star gets blitted:

def blitPlayer(self, screen):
    screen.blit(background,(0,0))
    screen.blit(self.player,(self.mouse_pos))
    screen.blit(self.star,(ran_x, self.star_r.y))

I define ran_x outside the class at the top like this:

ran_x = random.randint(10,470)

What i think is happening is that when i run it ran_x is being defined by one random number and then keeping that number but i want it to change each time the star leaves the screen

Thanks in Advance!!

-ChristianCareaga

Then why not recompute ran_x here:

if self.star_r.y > 640:
    self.star_r.y = 0
    ran_x = new random number

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM