2015-08-31 26 views
-1

我正在制作一款基本的pong游戏,并且想要制作一个系统,按下按钮可以让游戏杆上升或下降。我对PyGame相当陌生,至今为止我的代码。按键系统PyGame

import pygame, sys 
from 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) 
    for event in pygame.event.get(): 
    if event.type==KEYUP and event.key==K_W: 
    P(xx+10) 
    xx=xx+10 
    pygame.display.update() 
    elif event.type==QUIT: 
    pygame.quit() 
    sys.exit() 
+0

那么你的确切问题在哪里,你需要帮助? – sloth

回答

2

read一些教程和/或docs。这是非常基本的,如果你没有做到这一点,它会在稍后咬你。

不管怎么说,这是它的可看:

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: 
    for 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) 

这是傍确定但我仍然不会写我这样的比赛。所以请阅读一些教程,如果你真的想改善你的代码,可以试试CodeReview

+1

我并不是经常在pygame标签中提出一个答案,但是这个方法确实正确。 – sloth

+0

感谢您的解释,你知道任何优秀的教程网站,因为我还是这个新手 –

+0

@RutvikMarathe [如何移动图片?](http://www.pygame.org/docs/tut/MoveIt。 html),[黑猩猩教程,逐行](http://www.pygame.org/docs/tut/chimp/ChimpLineByLine.html),[Sprite模块介绍](http://www.pygame.org/docs /tut/SpriteIntro.html),[新手指南](http://www.pygame.org/docs/tut/newbieguide.html)。我发现这些特别有用。 –