找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 4266|回复: 1

[转载] 1280*768分辨率的pygame乒乓球小游戏移植

[复制链接]
发表于 2018-3-30 01:14:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
1280*720分辨率16:9。
QQ截图20180330010848.jpg

pong_game.py源码
[RenPy] 纯文本查看 复制代码
define e = Character("艾琳")
define p = Character("玩家")
init:
    image bg pong = "images/pong_bg.png"
    python:
        class PongDisplayable(renpy.Displayable):
            def __init__(self):
                renpy.Displayable.__init__(self)
                # Some displayables we use.
                self.paddle = Image("images/pong.png")
                self.ball = Image("images/pong_ball.png")
                self.player = Text(_("[p]"), size=36)
                self.eileen = Text(_("[e]"), size=36)
                self.ctb = Text(_("点击开始"), size=36)

                # The sizes of some of the images.
                self.PADDLE_WIDTH = 8
                self.PADDLE_HEIGHT = 79
                self.BALL_WIDTH = 25
                self.BALL_HEIGHT = 5
                self.COURT_TOP = 168
                self.COURT_BOTTOM = 600

                # If the ball is stuck to the paddle.
                self.stuck = True

                # The positions of the two paddles.
                self.playery = (self.COURT_BOTTOM - self.COURT_TOP) / 2
                self.computery = self.playery

                # The speed of the computer.
                self.computerspeed = 350.0

                # The position, dental-position, and the speed of the
                # ball.
                self.bx = 326
                self.by = self.playery
                self.bdx = .5
                self.bdy = .5
                self.bspeed = 300.0

                # The time of the past render-frame.
                self.oldst = None

                # The winner.
                self.winner = None

            def visit(self):
                return [ self.paddle, self.ball, self.player, self.eileen, self.ctb ]

            # Recomputes the position of the ball, handles bounces, and
            # draws the screen.
            def render(self, width, height, st, at):

                # The Render object we'll be drawing into.
                r = renpy.Render(width, height)

                # Figure out the time elapsed since the previous frame.
                if self.oldst is None:
                    self.oldst = st

                dtime = st - self.oldst
                self.oldst = st

                # Figure out where we want to move the ball to.
                speed = dtime * self.bspeed
                oldbx = self.bx

                if self.stuck:
                    self.by = self.playery
                else:
                    self.bx += self.bdx * speed
                    self.by += self.bdy * speed

                # Move the computer's paddle. It wants to go to self.by, but
                # may be limited by it's speed limit.
                cspeed = self.computerspeed * dtime
                if abs(self.by - self.computery) <= cspeed:
                    self.computery = self.by
                else:
                    self.computery += cspeed * (self.by - self.computery) / abs(self.by - self.computery)

                # Handle bounces.

                # Bounce off of top.
                ball_top = self.COURT_TOP + self.BALL_HEIGHT / 2
                if self.by < ball_top:
                    self.by = ball_top + (ball_top - self.by)
                    self.bdy = -self.bdy
                    renpy.sound.play("images/sound/pong_beep.wav", channel=0)

                # Bounce off bottom.
                ball_bot = self.COURT_BOTTOM - self.BALL_HEIGHT / 2
                if self.by > ball_bot:
                    self.by = ball_bot - (self.by - ball_bot)
                    self.bdy = -self.bdy
                    renpy.sound.play("images/sound/pong_beep.wav", channel=0)

                # This draws a paddle, and checks for bounces.
                def paddle(px, py, hotside):

                    # Render the paddle image. We give it an 800x600 area
                    # to render into, knowing that images will render smaller.
                    # (This isn't the case with all displayables. Solid, Frame,
                    # and Fixed will expand to fill the space allotted.)
                    # We also pass in st and at.
                    pi = renpy.render(self.paddle, 1280, 768, st, at)

                    # renpy.render returns a Render object, which we can
                    # blit to the Render we're making.
                    r.blit(pi, (int(px), int(py - self.PADDLE_HEIGHT / 2)))

                    if py - self.PADDLE_HEIGHT / 2 <= self.by <= py + self.PADDLE_HEIGHT / 2:

                        hit = False

                        if oldbx >= hotside >= self.bx:
                            self.bx = hotside + (hotside - self.bx)
                            self.bdx = -self.bdx
                            hit = True

                        elif oldbx <= hotside <= self.bx:
                            self.bx = hotside - (self.bx - hotside)
                            self.bdx = -self.bdx
                            hit = True

                        if hit:
                            renpy.sound.play("images/sound/pong_boop.wav", channel=1)
                            self.bspeed *= 1.10

                # Draw the two paddles.
                paddle(304, self.playery, 304 + self.PADDLE_WIDTH)
                paddle(966, self.computery, 966)

                # Draw the ball.
                ball = renpy.render(self.ball, 1280, 720, st, at)
                r.blit(ball, (int(self.bx - self.BALL_WIDTH / 2),
                              int(self.by - self.BALL_HEIGHT / 2)))

                # Show the player names.
                player = renpy.render(self.player, 1280, 720, st, at)
                r.blit(player, (250, 50))

                # Show Eileen's name.
                eileen = renpy.render(self.eileen, 1280, 720, st, at)
                ew, eh = eileen.get_size()
                r.blit(eileen, (1020 - ew, 50))

                # Show the "Click to Begin" label.
                if self.stuck:
                    ctb = renpy.render(self.ctb, 1280, 720, st, at)
                    cw, ch = ctb.get_size()
                    r.blit(ctb, (630 - cw / 2, 60))


                # Check for a winner.
                if self.bx < -200:
                    self.winner = "[e]"

                    # Needed to ensure that event is called, noticing
                    # the winner.
                    renpy.timeout(0)

                elif self.bx > 1000:
                    self.winner = "[p]"
                    renpy.timeout(0)

                # Ask that we be re-rendered ASAP, so we can show the next
                # frame.
                renpy.redraw(self, 0)

                # Return the Render object.
                return r

            # Handles events.
            def event(self, ev, x, y, st):

                import pygame

                # Mousebutton down == start the game by setting stuck to
                # false.
                if ev.type == pygame.MOUSEBUTTONDOWN and ev.button == 1:
                    self.stuck = False

                # Set the position of the player's paddle.
                y = max(y, self.COURT_TOP)
                y = min(y, self.COURT_BOTTOM)
                self.playery = y

                # If we have a winner, return him or her. Otherwise, ignore
                # the current event.
                if self.winner:
                    return self.winner
                else:
                    raise renpy.IgnoreEvent()


label demo_minigame_pong:

    window hide None
    # Put up the pong background, in the usual fashion.
    scene bg pong
    # Run the pong minigame, and determine the winner.
    python:
        ui.add(PongDisplayable())
        winner = ui.interact(suppress_overlay=True, suppress_underlay=True)
    scene bg washington
    show eileen vhappy
    window show None


    if winner == "[e]":
        e "我赢了!"
    else:
        e "[p] 你赢了。"
    show eileen happy
    menu:
        e "你想再玩一次吗??"
        "再来一局":
            jump demo_minigame_pong
        "不了,谢谢":
            pass

scripy.rpy
[RenPy] 纯文本查看 复制代码
label start:
    scene black
    menu:
        "开始游戏":
            jump demo_minigame_pong
        "不玩!":
            jump demo_minigame_pong
return



images.rar

82.45 KB, 下载次数: 34, 下载积分: 活力 100

发表于 2020-5-27 01:19:18 | 显示全部楼层
请问这个源码可以修改后直接放在游戏里吗?会涉及侵权之类的吗……
回复 支持 抱歉

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|RenPy中文空间 ( 苏ICP备17067825号|苏公网安备 32092302000068号 )

GMT+8, 2024-12-23 01:58 , Processed in 0.113174 second(s), 27 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表