首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python游戏Pong游戏问题

Python游戏Pong游戏问题
EN

Stack Overflow用户
提问于 2013-02-05 21:52:18
回答 1查看 2.4K关注 0票数 0

因此,基本上,我在这里试图编写的是一个Pong游戏,它使用Python编程语言和Pygame模块。

我的问题如下:

  1. 游戏有时被困在一个循环中,AI桨一次又一次地停在那里。
  2. 有时候球会跳来跳去?我不知道为什么..。
  3. 球就这样来来回回?我不明白它为什么不像我想的那样随随便便.
  4. 而我最大的问题,似乎当球击中顶部或底部,它只是消失了,游戏只是继续,好像它还在反弹。

我的代码

代码语言:javascript
复制
# Pong
import random, pygame, sys
from pygame.locals import *

FPS = 300
WINWID = 640
WINHEI = 480

BROWN = (102, 51, 0)
LIGHTBLUE = (0, 204, 204)
GREEN = (0, 153, 0)
PADDLECOLOR = (255, 255, 255)
BALLCOLOR = PADDLECOLOR
BACKGROUNDCOLOR = (0, 0, 0)

def main():
    global FPSCLOCK, DISPLAYSURF
    SPEED = 1
    DIFF = 1.0 # AI paddle speed handicap
    ball_speed = 1
    AISIGHT = WINWID/6
    FONT = "Times New Roman"
    pygame.init()
    DEFAULTFONT = pygame.font.SysFont(FONT, 16)
    BIGGERFONT = pygame.font.SysFont(FONT, 32)
    FPSCLOCK = pygame.time.Clock()
    DISPLAYSURF = pygame.display.set_mode((WINWID, WINHEI))
    DISPLAYSURF.fill(BACKGROUNDCOLOR)
    BACKGROUND = pygame.Rect(0, 0, WINWID, WINHEI)
    pygame.display.set_caption('Pong')
    paddleUP, paddleDOWN = False, False    
    ballpos = (WINWID/2, WINHEI/2)
    AIpoint, PLAYERpoint = 0, 0
    score = "Player:%2f || Computer:%2f" % (AIpoint, PLAYERpoint)
    paddle = WINHEI/2
    AIpaddle = paddle
    pygame.draw.rect(DISPLAYSURF, BACKGROUNDCOLOR, BACKGROUND)
    pygame.draw.line(DISPLAYSURF, PADDLECOLOR, (9, paddle-20), (9, paddle+20), 3)
    pygame.draw.line(DISPLAYSURF, PADDLECOLOR, (629, AIpaddle-20), (629, AIpaddle+20), 3)
    pygame.draw.circle(DISPLAYSURF, BALLCOLOR, ballpos, 5)
    slope, y_int = ballpath(None, ballpos[0], ballpos[1])
    while True:
        for event in pygame.event.get():
            direction = None
            if event.type == QUIT:
                terminate()
            elif event.type == KEYDOWN:
                if (event.key == K_UP or event.key == K_w):
                    paddleUP = True
                elif (event.key == K_DOWN or event.key == K_s):
                    paddleDOWN = True
            elif event.type == KEYUP:
                if (event.key == K_UP or event.key == K_w):
                    paddleUP = False
                elif (event.key == K_DOWN or event.key == K_s):
                    paddleDOWN = False
        # move player paddle
        if paddleUP and paddle >= 20:
            paddle-=SPEED
        elif paddleDOWN and paddle <= 460:
            paddle+=SPEED
        # move AI paddle
        if ballpos[0] > (AISIGHT):
            if AIpaddle-20 < ballpos[1] and AIpaddle <= 460:
                AIpaddle+=SPEED/DIFF
            elif AIpaddle+20 > ballpos[1] and AIpaddle >= 20:
                AIpaddle-=SPEED/DIFF
        # move ball
        x = ballpos[0] + ball_speed
        y = (slope*x) + y_int
        ballpos = (x, y)
        # if it hits a paddle
        if 9 < ballpos[0] < 11:
            if paddle-20 < ballpos[1] < paddle+20:
                slope, y_int = ballpath(None, ballpos[0], ballpos[1])
                ball_speed*=-1
        elif 629 < ballpos[0] < 631:
            if AIpaddle-20 < ballpos[1] < AIpaddle+20:
                slope, y_int = ballpath(None, ballpos[0], ballpos[1])
                ball_speed*=-1
        # if it hits the top or bottom
        if WINHEI-5 > ballpos[1] or ballpos[1] < 5:
            slope, y_int = ballpath(slope, ballpos[0], ballpos[1])
        # if paddle misses ball
        if ballpos[0] < 5:
            AIpoint+=1
            score = "Player:%2f || Computer:%2f" % (PLAYERpoint, AIpoint)
            ballpos = (WINWID/2, WINHEI/2)
            pygame.draw.rect(DISPLAYSURF, (0, 0, 0, 100), pygame.Rect(0, 0, 640, 480))
            DISPLAYSURF.blit(BIGGERFONT.render("Point: Computer", 1, PADDLECOLOR), (WINWID/3, WINHEI/3))
            paddle, AIpaddle = WINWID/2, WINWID/2
            for i in range(5):
                FPSCLOCK.tick(FPS)
        elif ballpos[0] > 635 and AIpaddle-20 < ballpos[1] < AIpaddle+20:
            PLAYERpoint+=1
            score = "Player:%2f || Computer:%2f" % (PLAYERpoint, AIpoint)
            ballpos = (WINWID/2, WINHEI/2)
            pygame.draw.rect(DISPLAYSURF, (0, 0, 0, 100), pygame.Rect(0, 0, 640, 480))
            DISPLAYSURF.blit(BIGGERFONT.render("Point: Player", 1, PADDLECOLOR), (WINWID/3, WINHEI/3))
            for i in range(5):
                FPSCLOCK.tick(FPS)


        pygame.draw.rect(DISPLAYSURF, BACKGROUNDCOLOR, BACKGROUND)
        pygame.draw.line(DISPLAYSURF, PADDLECOLOR, (9, paddle-20), (9, paddle+20), 3)
        pygame.draw.line(DISPLAYSURF, PADDLECOLOR, (629, AIpaddle-20), (629, AIpaddle+20), 3)
        pygame.draw.circle(DISPLAYSURF, BALLCOLOR, ballpos, 5)
        DISPLAYSURF.blit(DEFAULTFONT.render(score, 1, PADDLECOLOR), (WINWID/3, 20))
        pygame.display.update()
        FPSCLOCK.tick(FPS)


def ballpath(old_slope, ball_x, ball_y):
    if old_slope == None:
        slope = int(random.random() * 2)
    else:
        slope = old_slope * -1
    y_int = ball_y - (slope * ball_x)
    return slope, y_int

def terminate():
    pygame.quit()
    sys.exit()

if __name__ == '__main__':
    main()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-05 23:07:42

我清理了你的代码很多次了。对于如何使用这些类,它应该有一点帮助。http://pastebin.com/rZF3Gr0D

要注意的事情:

  • 评分事件发生时打印
  • 使用keystate和"on events“的划桨运动。它使这种输入更容易。
  • 雪碧集团允许您在批中“绘制”或检查冲突。
  • 删除全局关键字。游戏拥有大多数东西。
  • 在各地使用Rect函数和属性。比如ball.rect.center = screen.get_rect().center
  • 随机角度球产卵通过三角。
  • 盖子破了。接下来,你将进入基于时间的运动。为了简单起见,我把它忘了。
  • Color()让你做很多事情,包括Color("red")Color("gray40")等等.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14717653

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档