Building 'Pong' with python (codeskulptor) -
i'm trying build arcade game 'pong' in codeskulptor python. need 2 things
- increase ball velocity 10% when hits paddle
- get fix color buttons, not working
here's link code : http://www.codeskulptor.org/#user41_yjmm4nmju5x5zvk.py
# implementation of classic arcade game pong # built by: dejvi zelo import simplegui import random # initialize globals - pos , vel encode vertical info paddles width = 600 height = 400 ball_radius = 20 pad_width = 8 pad_height = 80 half_pad_width = pad_width / 2 half_pad_height = pad_height / 2 left = false right = true score1 = 0 score2 = 0 #paddle position paddle1_pos = height/2 paddle2_pos = height/2 #paddle velocity paddle1_vel = 0 paddle2_vel = 0 # initialize ball_pos , ball_vel new bal in middle of table ball_pos = [width/2 , height/2] ball_vel = [0 , 0] #set color theme color_palette = ['#ff5252','#00bcd4','#536dfe','#8bc34a','#ffc107','#ff9800','#ff4081','#e040fb'] color = random.choice(color_palette) def color_red(): color = color_palette[0] def color_l_blue(): color = color_palette[1] def color_d_blue(): color = color_palette[2] def color_green(): color = color_palette[3] def color_yellow(): color = color_palette[4] def color_orange(): color = color_palette[5] def color_pink(): color = color_palette[6] def color_purple(): color = color_palette[7] def open_game(): global ball_pos, ball_vel ball_pos = [width/2 , height/2] ball_vel[0] = 0 ball_vel[1] = 0 # if direction right, ball's velocity upper right, else upper left def start_game(direction): global ball_pos, ball_vel ball_pos = [width/2 , height/2] if direction == right: ball_vel[0] = random.randrange(120,240)/60.0 ball_vel[1] = -random.randrange(60,180)/60.0 if direction == left: ball_vel[0] = -random.randrange(120,240)/60.0 ball_vel[1] = -random.randrange(60,180)/60.0 # define event handlers def new_game(): global paddle1_pos, paddle2_pos, paddle1_vel, paddle2_vel global score1, score2 paddle1_pos = height/2 paddle2_pos = height/2 open_game() score1 = 0 score2 = 0 def draw(canvas): global score1, score2, paddle1_pos, paddle2_pos, ball_pos, ball_vel global paddle1_vel, paddle2_vel, pad_height, pad_width, ball_radius,half_pad_height, half_pad_width global color_palette, color # draw mid line , gutters canvas.draw_line([width / 2, 0],[width / 2, height], 1, "white") canvas.draw_line([pad_width, 0],[pad_width, height], 1, "white") canvas.draw_line([width - pad_width, 0],[width - pad_width, height], 1, "white") # update ball ball_pos[0] += ball_vel[0] ball_pos[1] += ball_vel[1] #ball upper , lower wall collisions if ball_pos[1] <= ball_radius: ball_vel[0] = ball_vel[0] ball_vel[1] = -ball_vel[1] if ball_pos[1] >= (height-1)-ball_radius: ball_vel[0] = ball_vel[0] ball_vel[1] = -ball_vel[1] #spawn ball left or right if ball_pos[0] <= ball_radius+pad_width: start_game(right) score2 += 1 if ball_pos[0] >= (width-1)-pad_width-ball_radius: start_game(left) score1 += 1 # draw ball canvas.draw_circle(ball_pos, ball_radius, 2, color, color) #keep paddle on screen if paddle1_pos-half_pad_height <= 0 , paddle1_vel < 0: paddle1_vel = 0 if paddle2_pos-half_pad_height <= 0 , paddle2_vel < 0: paddle2_vel = 0 if paddle1_pos+half_pad_height >= height , paddle1_vel > 0: paddle1_vel = 0 if paddle2_pos+half_pad_height >= height , paddle2_vel > 0: paddle2_vel = 0 # update paddle's vertical position paddle1_pos += paddle1_vel paddle2_pos += paddle2_vel # draw paddles canvas.draw_polygon([(0,paddle1_pos-half_pad_height),(pad_width,paddle1_pos-half_pad_height),(pad_width,paddle1_pos+half_pad_height),(0,paddle1_pos+half_pad_height)], 5, color, color) canvas.draw_polygon([(width,paddle2_pos-half_pad_height),(width-pad_width,paddle2_pos-half_pad_height),(width-pad_width,paddle2_pos+half_pad_height),(width,paddle2_pos+half_pad_height)], 5, color, color) # determine whether paddle , ball collide if ball_pos[1] > paddle1_pos-half_pad_height -2 , ball_pos[1]<paddle1_pos +half_pad_height +2 , ball_pos[0] - ball_radius <= pad_width+2: ball_vel[0] = -ball_vel[0] ball_vel[1] = ball_vel[1] if ball_pos[1] > paddle2_pos-half_pad_height -2 , ball_pos[1] < paddle2_pos +half_pad_height+2 , width - (width - (ball_pos[0] + ball_radius)) >= (width - pad_width) -2: ball_vel[0] = -ball_vel[0] ball_vel[1] = ball_vel[1] # draw scores canvas.draw_text(str(score1), (width/2 - 100,80), 60, color, 'sans-serif') canvas.draw_text(str(score2), (width/2 + 60,80), 60, color, 'sans-serif') def keydown(key): global paddle1_vel, paddle2_vel if key == simplegui.key_map['w']: paddle1_vel = -4 elif key == simplegui.key_map['s']: paddle1_vel = 4 elif key == simplegui.key_map['up']: paddle2_vel = -4 elif key == simplegui.key_map['down']: paddle2_vel = 4 elif key == simplegui.key_map['space']: start_game(random.choice([left,right])) def keyup(key): global paddle1_vel, paddle2_vel if key == simplegui.key_map['w']: paddle1_vel = 0 elif key == simplegui.key_map['s']: paddle1_vel = 0 elif key == simplegui.key_map['up']: paddle2_vel = 0 elif key == simplegui.key_map['down']: paddle2_vel = 0 # create frame frame = simplegui.create_frame("pong", width, height) frame.set_canvas_background('#192c36') frame.set_draw_handler(draw) frame.set_keydown_handler(keydown) frame.set_keyup_handler(keyup) frame.add_label('start game pressing space'),frame.add_label(' ') frame.add_button('restart', new_game, 100),frame.add_label(' ') frame.add_label('choose color theme: '),frame.add_label(' ') frame.add_button('red', color_red, 100) frame.add_button('light blue', color_l_blue, 100) frame.add_button('dark blue', color_d_blue, 100) frame.add_button('green', color_green, 100) frame.add_button('yellow', color_yellow, 100) frame.add_button('orange', color_orange, 100) frame.add_button('pink', color_pink, 100) frame.add_button('purple', color_purple, 100) # start frame open_game() frame.start()
for velocity have no idea, i've tried different methods, none of them worked, i'm pretty sure it's silly simple math
for colors, color variable set pick random color list color_palette, but, other that, buttons not doing supposed to.
i'm new python , programming in general , english isn't first language please bare me.
thanks further help
# determine whether paddle , ball collide if ball_pos[1] > paddle1_pos-half_pad_height -2 , ball_pos[1]<paddle1_pos +half_pad_height +2 , ball_pos[0] - ball_radius <= pad_width+2: ball_vel[0] = -1.1*ball_vel[0] ball_vel[1] = ball_vel[1] # line absolutely nothing ... if ball_pos[1] > paddle2_pos-half_pad_height -2 , ball_pos[1] < paddle2_pos +half_pad_height+2 , width - (width - (ball_pos[0] + ball_radius)) >= (width - pad_width) -2: ball_vel[0] = -1.1*ball_vel[0] ball_vel[1] = ball_vel[1] # line absolutely nothing ...
also collisions sort of broken ... times hits paddle , mistakenly restarts ball , scores point ...
you need add global color
top of each of color functions assign global color instead of local color ... there many other ways solve ... require least amount of change , work
def color_red(): global color color = color_palette[0]
Comments
Post a Comment