python - Combine effects to menu in pygame -
hey guys developing game pygame. idea of game when user click on start
button on menu(which appear on first before starting game) must see 2 balls bouncing on pygame window.
for have 2 python files.
bounceball.py
this python file makes 2 ball bounce on pygame window made me.the code of bounceball.py here.(sorry pasting on pastebin since long code)
menu.py
this python file creates menu have found internet , works fine.the code of menu.py here
but problem when user clicks on start
button menu didnt .the thing want when user clicks on start
button menu must see ball boucing have coded in bounceball.py
on pygame window.how can link bounceball.py
menu.
i have tried acheive many methods didnt helped me ..
hope guys can me in acheieving ..any appreciated ..thanks in advance
it done better @ least works.
menu.py
#!/usr/bin/python import sys import pygame import bounceball #---------------------------------------------------------------------- white = (255, 255, 255) red = (255, 0, 0) black = ( 0, 0, 0) #---------------------------------------------------------------------- class menuitem(pygame.font.font): def __init__(self, text, font=none, font_size=30, font_color=white, (pos_x, pos_y)=(0, 0)): pygame.font.font.__init__(self, font, font_size) self.text = text self.font_size = font_size self.font_color = font_color self.label = self.render(self.text, 1, self.font_color) self.width = self.label.get_rect().width self.height = self.label.get_rect().height self.dimensions = (self.width, self.height) self.pos_x = pos_x self.pos_y = pos_y self.position = pos_x, pos_y def is_mouse_selection(self, (posx, posy)): if (posx >= self.pos_x , posx <= self.pos_x + self.width) , \ (posy >= self.pos_y , posy <= self.pos_y + self.height): return true return false def set_position(self, x, y): self.position = (x, y) self.pos_x = x self.pos_y = y def set_font_color(self, rgb_tuple): self.font_color = rgb_tuple self.label = self.render(self.text, 1, self.font_color) #---------------------------------------------------------------------- class gamemenu(): def __init__(self, screen, items, funcs, bg_color=black, font=none, font_size=30, font_color=white): self.screen = screen self.scr_width = self.screen.get_rect().width self.scr_height = self.screen.get_rect().height self.bg_color = bg_color self.clock = pygame.time.clock() self.funcs = funcs self.items = [] index, item in enumerate(items): menu_item = menuitem(item, font, font_size, font_color) # t_h: total height of text block t_h = len(items) * menu_item.height pos_x = (self.scr_width / 2) - (menu_item.width / 2) pos_y = (self.scr_height / 2) - (t_h / 2) + (index * menu_item.height) menu_item.set_position(pos_x, pos_y) self.items.append(menu_item) self.mouse_is_visible = true self.cur_item = none def set_mouse_visibility(self): if self.mouse_is_visible: pygame.mouse.set_visible(true) else: pygame.mouse.set_visible(false) def set_keyboard_selection(self, key): """ marks menuitem chosen via , down keys. """ item in self.items: # return neutral item.set_italic(false) item.set_font_color(white) if self.cur_item none: self.cur_item = 0 else: # find chosen item if key == pygame.k_up , \ self.cur_item > 0: self.cur_item -= 1 elif key == pygame.k_up , \ self.cur_item == 0: self.cur_item = len(self.items) - 1 elif key == pygame.k_down , \ self.cur_item < len(self.items) - 1: self.cur_item += 1 elif key == pygame.k_down , \ self.cur_item == len(self.items) - 1: self.cur_item = 0 self.items[self.cur_item].set_italic(true) self.items[self.cur_item].set_font_color(red) # check if enter or space pressed if key == pygame.k_space or key == pygame.k_return: text = self.items[self.cur_item].text self.funcs[text]() def set_mouse_selection(self, item, mpos): """marks menuitem mouse cursor hovers on.""" if item.is_mouse_selection(mpos): item.set_font_color(red) item.set_italic(true) else: item.set_font_color(white) item.set_italic(false) def run(self): mainloop = true while mainloop: # limit frame speed 50 fps self.clock.tick(50) mpos = pygame.mouse.get_pos() event in pygame.event.get(): if event.type == pygame.quit: mainloop = false if event.type == pygame.keydown: self.mouse_is_visible = false self.set_keyboard_selection(event.key) if event.type == pygame.mousebuttondown: item in self.items: if item.is_mouse_selection(mpos): self.funcs[item.text]() if pygame.mouse.get_rel() != (0, 0): self.mouse_is_visible = true self.cur_item = none self.set_mouse_visibility() # redraw background self.screen.fill(self.bg_color) item in self.items: if self.mouse_is_visible: self.set_mouse_selection(item, mpos) self.screen.blit(item.label, item.position) pygame.display.flip() #---------------------------------------------------------------------- def run_bounceball(): print "run bounceball" bounceball.run(screen) #---------------------------------------------------------------------- if __name__ == "__main__": pygame.init() # creating screen screen = pygame.display.set_mode((300, 300), 0, 32) menu_items = ('start', 'quit') funcs = {'start': run_bounceball, 'quit': sys.exit} pygame.display.set_caption('game menu') gm = gamemenu(screen, funcs.keys(), funcs) gm.run()
bounceball.py
import pygame import math itertools import cycle #---------------------------------------------------------------------- # simple vector helper functions, stolen http://stackoverflow.com/a/4114962/142637 def magnitude(v): return math.sqrt(sum(v[i]*v[i] in range(len(v)))) def add(u, v): return [ u[i]+v[i] in range(len(u)) ] def sub(u, v): return [ u[i]-v[i] in range(len(u)) ] def dot(u, v): return sum(u[i]*v[i] in range(len(u))) def normalize(v): vmag = magnitude(v) return [ v[i]/vmag in range(len(v)) ] #---------------------------------------------------------------------- class ball(object): def __init__(self, path, screen): self.x, self.y = (0, 0) self.speed = 2.5 self.color = (200, 200, 200) self.path = cycle(path) self.set_target(next(self.path)) self.screen = screen @property def pos(self): return self.x, self.y # drawing, need position tuple of ints # lets create 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 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) # threshold stop moving if distance small. # prevents 'flickering' between 2 points if magnitude(target_vector) < 2: return self.next_target() # apply balls's speed vector move_vector = [c * self.speed c in normalize(target_vector)] # update position self.x, self.y = add(self.pos, move_vector) def draw(self): pygame.draw.circle(self.screen, self.color, self.int_pos, 4) #---------------------------------------------------------------------- def run(screen): #pygame.init() # no need - inited in menu.py #screen = pygame.display.set_mode((300, 300)) # no need - created in menu.py clock = pygame.time.clock() quit = false path = [(26, 43), (105, 110), (45, 225), (145, 295), (266, 211), (178, 134), (250, 56), (147, 12)] path2 = [(26, 43), (105, 10), (45, 125), (150, 134), (150, 26), (107, 12)] ball = ball(path, screen) ball.speed = 1.9 ball2 = ball(path2, screen) ball2.color = (200, 200, 0) balls = [ball, ball2] while not quit: 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)
Comments
Post a Comment