如何在游戏中不停地移动图片?

2024-05-16 12:55:14 发布

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

我在做游戏,我有“橙色”“女巫”“我的游戏”类。橘子只是在屏幕上画画,然后从屏幕上的某个位置到橘子的位置。用户通过点击来杀死女巫,例如,当活女巫的数量==2时,一些新的女巫应该出现在旧的(第一个)位置并转到橙色。现在我不能让新的女巫出现。在

class Witch(object):
def __init__(self, position, image):

    self.image = image
    self.speed = 5

    self.position = [random.randint(0, 760), random.randint(0, 200)]       
    self.destination = (random.randint(350, 500), random.randint(350, 550))  
    self.play = True 


def draw(self, surface):
    if self.destination != self.position:   
        v = (self.destination[0] - self.position[0], self.destination[1]-self.position[1])
        n = math.sqrt(v[0]**2 + v[1]**2)
        uv = v[0]/n, v[1]/n

        self.position[0] += uv[0]*self.speed
        self.position[1] += uv[1]*self.speed

    if self.destination == self.position:
        self.position = self.position

    surface.blit(self.image, self.position)

class MyGame(object):
def __init__(self):
    """Initialize a new game"""
    pygame.mixer.init()
    pygame.mixer.pre_init(44100, -16, 2, 2048)
    pygame.init()        

self.oranges = []  
    for x in xrange(25):
        position = self.width//2, self.height//2
        self.oranges.append(Orange(position, self.orange))

    self.witches = []  
    for x in xrange(4):
        position = self.width//2, self.height//2
        self.witches.append(Witch(position, self.witch))

    self.pos = 0, 0
        if self.timer > 30:
        for i in self.oranges:
            i.draw(self.screen)

        if len(self.witches) == 2:
            for witch in self.new_witches:
                self.new_witches.append(witch)
                witch.draw(self.screen)

        for witch in self.witches:
            witch.draw(self.screen)
            witch_x = witch.position[0]
            witch_y = witch.position[1]                



            if int(witch_y) in range(350,550):
                for o in self.oranges:
                    self.oranges.remove(o)
                    if len(self.oranges) == 0:
                        self.lives -= 1    

            player_click_x = witch_x-35 <= self.pos[0] <= witch_x + 35
            player_click_y = witch_y-40 <= self.pos[1] <= witch_x + 40
            if player_click_x == True and player_click_y == True:
                self.witches.remove(witch)

Tags: inimageselfforifinitpositionrandom
1条回答
网友
1楼 · 发布于 2024-05-16 12:55:14

女巫必须时刻记住起始位置。在

 self.start_position = self.position = [random.randint(...), random.randint(...)]

当女巫死了,你把它从witches列表移到unused_witches列表,并将其position更改为{}

^{pr2}$

以后你可以再用它了

if len(self.witches) <= 2:
    for witch in self.unused_witches:
        self.witches.append(witch)
        self.unused_witches.remove(witch)

顺便说一句:

你可以的

if int(witch_y) in range(350,550):

写为

if 350 <= int(witch_y) < 550 :

range(350,550)给出列表350…549

你可以的

witch_x-35 <= self.pos[0] <= witch_x + 35

写为

-35 <= self.pos[0]-witch_x <= 35

或者

abs(self.pos[0]-witch_x) <= 35

相关问题 更多 >