python 3.x - Advice on my pong code -
i doing clone of pong. barebone version of pong. , nice if guys me refine it. doing learning exercise, have kept basic functions.
import time import pygame done = false pygame.init() myfont = pygame.font.sysfont("monospace", 15) screen_size = [320,240] white = [255,255,255] black = [0,0,0] gutter = 10 score_1 = 0 score_2 = 0 ball_pos = [160,120] ball_vel = [1,1] paddle_1 = [0,0] paddle_2 = [screen_size[0]-5,0] vel_1 = [0,0] vel_2 = [0,0] p1 = false p2 = false screen = pygame.display.set_mode(screen_size) pygame.display.set_caption("mygame") while not done: time.sleep(0.02) screen.fill(black) event in pygame.event.get(): if event.type == pygame.quit: done = true if event.type == pygame.keydown: if pygame.key.get_pressed()[pygame.k_down]: vel_2[1] += 2 p1 = true if pygame.key.get_pressed()[pygame.k_up]: p1 = true vel_2[1] -= 2 if pygame.key.get_pressed()[pygame.k_s]: p2 = true vel_1[1] += 2 if pygame.key.get_pressed()[pygame.k_w]: p2 = true vel_1[1] -= 2 if event.type == pygame.keyup: if(p1): vel_2 = [0,0] p1 = false if (p2): vel_1 = [0,0] p2 = false if ball_pos[1] in range(paddle_1[1],paddle_1[1]+30) , ball_pos[0] in range(0,gutter+10) : ball_vel[0] *= -1 if ball_pos[1] in range(paddle_2[1],paddle_2[1]+30) , ball_pos[0] in range(310,320): ball_vel[0] *= -1 if ball_pos[0] > 320: score_1 += 1 ball_pos = [160,120] if ball_pos[0] < 5: score_2 += 1 ball_pos = [160,120] if ball_pos[1] > 230 or ball_pos[1] < 5: ball_vel[1] *= -1 paddle_1[0] += vel_1[0] paddle_1[1] += vel_1[1] paddle_2[0] += vel_2[0] paddle_2[1] += vel_2[1] ball_pos[0] += ball_vel[0] ball_pos[1] += ball_vel[1] ## pygame.draw.line(screen,white,(gutter,0),(gutter,screen_size[1])) ## pygame.draw.line(screen,white,(screen_size[0]-gutter,0),(screen_size[0]-gutter,screen_size[1])) pygame.draw.line(screen,white,(screen_size[0]/2,0),(screen_size[0]/2,screen_size[1]),1) pygame.draw.circle(screen,white,ball_pos,10,0) pygame.draw.line(screen,white,[paddle_1[0]+5,paddle_1[1]],[paddle_1[0]+5,paddle_1[1]+50],gutter) pygame.draw.line(screen,white,paddle_2,[paddle_2[0],paddle_2[1]+50],gutter) label_1 = myfont.render(str(score_1), 1, (255,255,0)) label_2 = myfont.render(str(score_2), 1, (255,255,0)) screen.blit(label_1, (100, 100)) screen.blit(label_2, (220, 100)) pygame.display.flip() pygame.quit()
there few things should improve in code:
1) constants vs variables
when define
white = [255,255,255] black = [0,0,0]
you should instead do:
white = (255,255,255) black = ( 0, 0, 0)
constants should created tuples
not mistakenly change values. also, since ever read constant , never change value tuple
more appropriate. runs faster
2) creating classes
whenever make game, recommended create objects classes. in case, have classes such player
, ball
. in these classes can define functions such update
, draw
. allow draw object or update location, speed etc single line of code. eg:
player.update()
player.draw()
ball.draw()
3) time vs timer
instead of using import time
, recommend use pygame built-in timer
option controlling fps of game. here's example:
timer = pygame.time.clock()
then @ start of while loop, can do:
timer.tick(60) #if want 60 number of frames per second
i know lot take in in end effort more worth , make future game making pygame cleaner.
happy pygaming!
Comments
Post a Comment