为什么我的精灵Sprite并不总是在我的Sprite栏上反弹?!

时间:2014-04-06 23:04:40

标签: python tkinter

我有一些问题,我无法弄清楚如何修复它们...... 这是非常简单的游戏。由鼠标移动的杆和一个球(或一些球)弹跳。 用户只需要让球保持弹跳。

用户可以选择他想要的球数(1,2,3),杆的大小(小,中,大)和球的速度(慢,正常,快)。

问题: - 有时一切都很好,有时球(或球)只是通过酒吧。就像碰撞功能不起作用一样。我还有其他办法吗?

  • 每当发生碰撞时,分数应该为屏幕顶部显示的总数增加10分,但这个分数一直在被覆盖。

对于这个游戏运行,我只需要从其他文件(设置)调用函数(startGame),它还会发送球的数量,条的大小和球的速度。

如果有人可以帮助我欣赏。

谢谢

from livewires import games, color
from tkinter import*
import random


games.init(screen_width = 735, screen_height = 350, fps = 60)

class Bounce(games.Sprite):

    global total_score
    total_score = 0



    def update(self):

        global total_score
        if self.bottom == 315 and self.overlapping_sprites:
            self.dy = -self.dy
            total_score += 10

            the_score = games.Text(value = 0, size = 25, 
                                         color = color.gray,x = 700, y = 20)
            games.screen.add(the_score)

        if self.right > games.screen.width or self.left < 0: 
            self.dx = -self.dx

        if self.top < 0:
            self.dy = -self.dy

        if self.bottom == 400:
            lose_message = games.Message(value = " - GAME OVER -",
                                         size = 50,
                                         color = color.gray,
                                         x = games.screen.width/2,
                                         y = games.screen.height/2,
                                         lifetime = 300,
                                         after_death = games.screen.quit)
            games.screen.add(lose_message)


class Bar_moving(games.Sprite):
    def update(self):   

        self.x = games.mouse.x  
        self.y = 315

        if self.left < 0:
            self.left = 0

        if self.right > games.screen.width:
            self.right = games.screen.width


class have_settings():

    def __init__(self):

        global the_ball_speed
        global the_ball_number
        global the_bar_size

        if "the_ball_speed" not in globals():
            the_ball_speed = "normal"
        if "the_bar_size" not in globals():
            the_bar_size = "medium"
        if "the_ball_number" not in globals():
            the_ball_number = 1

    def set_all(self, number, size, speed):

        global the_ball_speed
        global the_bar_size
        global the_ball_number

        if speed in ("slow","normal","fast"):
            the_ball_speed = speed

        if size in ("small","medium","large"):
            the_bar_size = size

        if number in (1,2,3):
            the_ball_number = number



def startGame():

    call = have_settings()

    background = games.load_image("BG.jpg", transparent = False)
    games.screen.background = background

#-------------------------------------SPEED
    sp_is = 0

    if the_ball_speed == "slow":
        sp_is = 2

    elif the_ball_speed == "normal":
        sp_is = 3

    elif the_ball_speed == "fast":    
        sp_is = 4

#-------------------------------------BALL NUMBER    
    if the_ball_number in (1,2,3):

        for n in range(the_ball_number):

            position_x_list =(50,150,250,350,400,450,500,550)
            position_x = random.choice(position_x_list)

            position_y_list =(50,100,150,200,225,250)
            position_y = random.choice(position_y_list)

            vert_speed_list = (-2,2)
            vert_speed = random.choice(vert_speed_list)

            ball_img = games.load_image("ball.bmp")
            ball = Bounce(image = ball_img,
                               x = position_x,
                               y = position_y,
                               dx = vert_speed,
                               dy = - sp_is)
            games.screen.add(ball)

#-------------------------------------BAR SIZE   
    if the_bar_size in ("small","medium","large"):
        if the_bar_size == "small":
            bar_pic = "bar_small.jpg"
        elif the_bar_size == "medium":
            bar_pic = "bar_medium.jpg"
        elif the_bar_size == "large":
            bar_pic = "bar_large.jpg"


    bar = games.load_image(bar_pic, transparent = False)
    the_bar = Bar_moving(image = bar, x = games.mouse.x)
    games.screen.add(the_bar)
    games.mouse.is_visible = False
    games.screen.event_grab = True 


    games.screen.mainloop()

1 个答案:

答案 0 :(得分:1)

您应该执行大于检查而不是等于以下检查:

if self.bottom >= 315 and self.overlapping_sprites:
               ^^

而不是

if self.bottom == 315 and self.overlapping_sprites:

这是因为球的y位置很少与底部完美对齐。在某些情况下,它可能会从y==314转到y==316。在这种情况下,上面的方法不起作用。因此,你应该大于测试而不是平等测试。

您可以在其他任何地方应用类似的更改,它应该可以正常工作。