如何使用pygame让精灵在随机位置繁殖?

2024-09-24 22:31:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试改变一个道奇式的游戏,有些方块让你成长,有些方块让你缩小。我还计划添加一个红色的,其中一个应该会让你失去一个生命(你将有3个生命开始),以及其他各种具有自己属性的生命。不过,我遇到了一个障碍,让下降块随机产生,这是一个需要在广泛的游戏。在

我的计划基本上是,我想让块在随机的位置重新生成每次,在某个点上,我希望下降的块的数量增加,以及进一步增加难度。在

这是我目前的进展。非常感谢您的任何意见:

import pygame
import random

pygame.init()

win_width = 800
win_height = 600
window = pygame.display.set_mode((win_width,win_height))

pygame.display.set_caption("Roger Dodger")

black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
orange = (255,127,0)
yellow = (255,255,0)
blue = (0,0,255)


clock = pygame.time.Clock()

class Collisions:
    def __init__(self, x1,y1,w1,h1,x2,y2,w2,h2):
        self.x1 = x1
        self.y1 = y1
        self.w1 = w1
        self.h1 = h1
        self.x2 = x2
        self.y2 = y2
        self.w2 = w2
        self.h2 = h2

    def checkCol(self):
        if (self.x2 + self.w2 >= self.x1 >= self.x2 and self.y2 + self.h2 >= self.y1 >= self.y2):

            return True

        elif (self.x2 + self.w2 >= self.x1 + self.w1 >= self.x2 and self.y2 + self.h2 >= self.y1 >= self.y2):

            return True

        elif (self.x2 + self.w2 >= self.x1 >= self.x2 and self.y2 + self.h2 >= self.y1 + self.h1 >= self.y2):

            return True

        elif (self.x2 + self.w2 >= self.x1 + self.w1 >= self.x2 and self.y2 + self.h2 >= self.y1 + self.h1 >= self.y2):

            return True

        else:

            return False

    def yel_col(self):
        if Collisions.checkCol(self):
            return True

    def ora_col(self):
        if Collisions.checkCol(self):
            return True


class Sprite:

    def __init__(self,x,y,width,height, color):

        self.x = x

        self.y = y

        self.width = width

        self.height = height

        self.color = color

    def render(self,):
              pygame.draw.rect(window,self.color,(self.x,self.y,self.width,self.height))



Sprite1=Sprite(win_width/2 ,win_height-60,30,30, blue)


moveX = 0

sprite2x = random.randrange(30, win_width, 30)
sprite3x = random.randrange(30, win_width, 30)

falling_pos = 0


gameLoop=True
while gameLoop:

    for event in pygame.event.get():

        if (event.type==pygame.QUIT):

            gameLoop=False

        if (event.type==pygame.KEYDOWN):

            if (event.key==pygame.K_LEFT):

                moveX = -3

            if (event.key==pygame.K_RIGHT):

                moveX = 3




    window.fill(white)

    ground = pygame.draw.rect(window, black, ((0, win_height-30), (win_width, 30)))

    Sprite1.x+=moveX

    falling_pos += 3

    Sprite2=Sprite(sprite2x,falling_pos,30,30, orange)
    Sprite3=Sprite(sprite3x, falling_pos, 30, 30, yellow)

    collisions1=Collisions(Sprite1.x,Sprite1.y,Sprite1.width,Sprite1.height,Sprite2.x,Sprite2.y,Sprite2.width,Sprite2.height)
    collisions2=Collisions(Sprite1.x,Sprite1.y,Sprite1.width,Sprite1.height,Sprite3.x,Sprite3.y,Sprite3.width,Sprite3.height)


    Sprite1.render()
    Sprite2.render()
    Sprite3.render()

    if collisions2.checkCol() and collisions2.yel_col():
       if Sprite1.width and Sprite1.height > 30:
           Sprite1.width -= 5
           Sprite1.height -= 5
           Sprite1.y += 5

    if collisions1.checkCol() and collisions1.ora_col():
       if Sprite1.width and Sprite1.height < 300:
           Sprite1.width += 5
           Sprite1.height += 5
           Sprite1.y -= 5

    if Sprite1.x < 0:
        Sprite1.x = win_width

    elif Sprite1.x > win_width:
        Sprite1.x = 0



    if falling_pos > win_height:
        falling_pos = 0

    pygame.display.flip()

    clock.tick(120)

pygame.quit()

Tags: andselfreturnifh2widthpygamewin