pygame单击按钮时的开始事件 [英] Start event when button is clicked with pygame

查看:144
本文介绍了pygame单击按钮时的开始事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是pygame的新手.我开发了一个简单的游戏,球互相反弹,效果很好.

Hey guys am new to pygame.I have developed a simple game in which the ball bounce each other.It works fine.

我添加了带有按钮的ui,这些按钮的选项类似于new gameloadgameoptions.

I have added a ui with buttons with options like new game,loadgame,options.

我需要的是,当用户单击new game button时,他必须看到球反弹.我的代码是

What I need is that when a user click on new game button he must see the ball bouncing. My code is

import pygame
import math
from itertools import cycle

def magnitude(v):
return math.sqrt(sum(v[i]*v[i] for i in range(len(v))))

def add(u, v):
return [ u[i]+v[i] for i in range(len(u)) ]

def sub(u, v):
return [ u[i]-v[i] for i in range(len(u)) ]    

def dot(u, v):
return sum(u[i]*v[i] for i in range(len(u)))

def normalize(v):
vmag = magnitude(v)
return [ v[i]/vmag  for i in range(len(v)) ]

screen = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()  



class Ball(object):
def __init__(self, path):
    self.x, self.y = (0, 0)
    self.img = pygame.image.load('ball.jpg')
    self.speed = 2.5
    self.color = (200, 200, 200)
    self.path = cycle(path)
    self.set_target(next(self.path))
    self.sound = pygame.mixer.music.load('yeah.mp3')


@property
def pos(self):
    return self.x, self.y

# for drawing, we need the position as tuple of ints
# so lets create a helper property
@property
def int_pos(self):
    return map(int, self.pos)

@property
def target(self):
    return self.t_x, self.t_y

@property
def int_target(self):
    return map(int, self.target)   

def next_target(self):
    self.set_target(self.pos)
    self.set_target(next(self.path))

def set_target(self, pos):
    self.t_x, self.t_y = pos

def update(self):
    # if we won't move, don't calculate new vectors
    if self.int_pos == self.int_target:
        return self.next_target()

    target_vector = sub(self.target, self.pos) 

    # a threshold to stop moving if the distance is to small.
    # it prevents a 'flickering' between two points
    if magnitude(target_vector) < 2: 
        return self.next_target()

    # apply the balls's speed to the vector
    move_vector = [c * self.speed for c in normalize(target_vector)]

    # update position
    self.x, self.y = add(self.pos, move_vector)

def draw(self):
    screen.blit(self.img, self.int_pos)
    pygame.mixer.music.play()

class Option:


hovered = False

def __init__(self, text, pos):
    self.text = text
    self.pos = pos
    self.set_rect()
    self.draw()

def draw(self):
    self.set_rend()
    screen.blit(self.rend, self.rect)

def set_rend(self):
    self.rend = menu_font.render(self.text, True, self.get_color())

def get_color(self):
    if self.hovered:
        return (255, 255, 255)
    else:
        return (100, 100, 100)

def set_rect(self):
    self.set_rend()
    self.rect = self.rend.get_rect()
    self.rect.topleft = self.pos

pygame.init()
quit = False
path = [(26, 43),(105, 110),(45, 225),(145, 295),(266, 211),(178, 134),(250,5)(147,12)] 

path2 = [(26, 43),(105, 10),(45, 125),(150, 134),(150, 26),(107, 12)]


ball = Ball(path)

ball.speed = 1.9

ball2 = Ball(path2)

ball2.color = (200, 200, 0)

balls = [ball, ball2]


screen = pygame.display.set_mode((480, 320))

menu_font = pygame.font.Font(None, 40)

options = [Option("NEW GAME", (140, 105)), Option("LOAD GAME", (135, 155)),
       Option("OPTIONS", (145, 205))]

while not quit:

pygame.event.pump()
screen.fill((0, 0, 0))
for option in options:
    if option.rect.collidepoint(pygame.mouse.get_pos()):
        option.hovered = True
    else:
        option.hovered = False
    option.draw()
pygame.display.update()

quit = pygame.event.get(pygame.QUIT)
pygame.event.poll()

map(Ball.update, balls)

screen.fill((0, 0, 0))

map(Ball.draw, balls)

pygame.display.flip()
clock.tick(60)

当我尝试这段代码时,球会弹起,并且开始的ui可以正常工作,但是当我单击新的按钮图标时,它什么都没有显示.

When I tried this code the the ball bounce and the starting ui works fine but when I click on the new button icon it doesn’t show anything.

我需要的是,当用户单击new game按钮时,它必须重定向到弹跳球屏幕.

What I need is when a user click new game button it must redirect to the ball bouncing screen.

我已经使用pygame.mouse.get_pressed进行了尝试,但是并没有帮助我.

I have tried it with pygame.mouse.get_pressed but it didn’t helped me out.

希望你们能帮助我.

先感谢

推荐答案

所以您的问题是,首先您总是绘制球,其次您不检查鼠标的点击.一种简单的检查方法是在您检查鼠标位置是否在选项之一上方的位置调用pygame.event.get([pygame.MOUSEBUTTONDOWN]).如果返回的内容不是None,则停止显示选项并开始显示球.

So your problem is that, first you always draw the balls and second you don't check for a mouse click. An easy way to do this check is to call pygame.event.get([pygame.MOUSEBUTTONDOWN]) right where you check if the mouse position is over one of the options. If it returns something else than None stop displaying the options and start displaying the balls.

因此,您将执行以下操作: 导入pygame 导入数学 从itertools导入周期开始

So you would do something like that: import pygame import math from itertools import cycle

OPTIONS = 0
BALLS = 1

def magnitude(v):
    return math.sqrt(sum(v[i]*v[i] for i in range(len(v))))

def add(u, v):
    return [ u[i]+v[i] for i in range(len(u)) ]

def sub(u, v):
    return [ u[i]-v[i] for i in range(len(u)) ]    

def dot(u, v):
    return sum(u[i]*v[i] for i in range(len(u)))

def normalize(v):
    vmag = magnitude(v)
    return [ v[i]/vmag  for i in range(len(v)) ]

screen = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()  



class Ball(object):
    def __init__(self, path):
        self.x, self.y = (0, 0)
        self.img = pygame.image.load('/home/wastl/Documents/DSC_0051.JPG')
        self.speed = 2.5
        self.color = (200, 200, 200)
        self.path = cycle(path)
        self.set_target(next(self.path))
        #self.sound = pygame.mixer.music.load('yeah.mp3')


    @property
    def pos(self):
        return self.x, self.y

# for drawing, we need the position as tuple of ints
# so lets create a helper property
    @property
    def int_pos(self):
        return map(int, self.pos)

    @property
    def target(self):
        return self.t_x, self.t_y

    @property
    def int_target(self):
        return map(int, self.target)   

    def next_target(self):
        self.set_target(self.pos)
        self.set_target(next(self.path))

    def set_target(self, pos):
        self.t_x, self.t_y = pos

    def update(self):
    # if we won't move, don't calculate new vectors
        if self.int_pos == self.int_target:
            return self.next_target()

        target_vector = sub(self.target, self.pos) 

    # a threshold to stop moving if the distance is to small.
    # it prevents a 'flickering' between two points
        if magnitude(target_vector) < 2: 
            return self.next_target()

    # apply the balls's speed to the vector
        move_vector = [c * self.speed for c in normalize(target_vector)]

    # update position
        self.x, self.y = add(self.pos, move_vector)

    def draw(self):
        screen.blit(self.img, self.int_pos)
        #pygame.mixer.music.play()

class Option:



    def __init__(self, text, pos):
        self.hovered = False
        self.text = text
        self.pos = pos
        self.set_rect()
        self.draw()

    def draw(self):
        self.set_rend()
        screen.blit(self.rend, self.rect)

    def set_rend(self):
        self.rend = menu_font.render(self.text, True, self.get_color())

    def get_color(self):
        if self.hovered:
            return (255, 255, 255)
        else:
            return (100, 100, 100)

    def set_rect(self):
        self.set_rend()
        self.rect = self.rend.get_rect()
        self.rect.topleft = self.pos

pygame.init()
quit = False
path = [(26, 43),(105, 110),(45, 225),(145, 295),(266, 211),(178, 134),(250,5),(147,12)] 

path2 = [(26, 43),(105, 10),(45, 125),(150, 134),(150, 26),(107, 12)]


ball = Ball(path)

ball.speed = 1.9

ball2 = Ball(path2)

ball2.color = (200, 200, 0)

balls = [ball, ball2]


screen = pygame.display.set_mode((480, 320))

menu_font = pygame.font.Font(None, 40)

options = [Option("NEW GAME", (140, 105)), Option("LOAD GAME", (135, 155)),
       Option("OPTIONS", (145, 205))]

STATE = OPTIONS

while not quit:

    pygame.event.pump()
    screen.fill((0, 0, 0))

    if STATE == OPTIONS:

        for option in options:
            if option.rect.collidepoint(pygame.mouse.get_pos()):
                option.hovered = True
                if pygame.event.get([pygame.MOUSEBUTTONDOWN]) and option.text == "NEW GAME":
                    STATE = BALLS
            else:
                option.hovered = False
            option.draw()
            pygame.display.update()

    elif STATE == BALLS:
        map(Ball.update, balls)

        screen.fill((0, 0, 0))

        map(Ball.draw, balls)

        pygame.display.flip()


    quit = pygame.event.get(pygame.QUIT)
    pygame.event.poll()

    clock.tick(60)

这篇关于pygame单击按钮时的开始事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆