python 3.x - Key Pressing System PyGame -
i'm making basic pong game , want make system button press makes paddle goes or down. i'm new pygame , here's code far.
import pygame, sys pygame.locals import* def pong(): pygame.init() display=pygame.display.set_mode((600,400),0,32) pygame.display.set_caption("pong") black=(0,0,0) white=(255,255,255) red=(255,0,0) display.fill(black) while true: def p(b): pygame.draw.rect(display,white,(50,b,50,10)) xx=150 p(xx) event in pygame.event.get(): if event.type==keyup , event.key==k_w: p(xx+10) xx=xx+10 pygame.display.update() elif event.type==quit: pygame.quit() sys.exit()
please read tutorials and/or docs. basic , if don't right bite later.
anyway, here's how look:
import pygame pygame.init() display = pygame.display.set_mode((600,400)) paddle = pygame.rect(0, 0, 10, 50) paddle.midleft = display.get_rect().midleft speed = 10 clock = pygame.time.clock() while true: event in pygame.event.get(): if event.type == pygame.quit: raise systemexit(0) keystate = pygame.key.get_pressed() dy = keystate[pygame.k_s] - keystate[pygame.k_w] paddle.move_ip(0, dy * speed) display.fill(pygame.color("black")) pygame.draw.rect(display, pygame.color("white"), paddle) pygame.display.update() clock.tick(30)
that ok pong still wouldn't write game this. read tutorials , maybe try codereview if want improve code.
Comments
Post a Comment