Pong

简单的一个Pygame小游戏Pong,使用Python2.7完成,带有一个开始界面,一个名字输入界面,一个游戏界面和一个结束界面。

代码如下:

# -*- coding: utf-8 -*-

import pygame
import random

pygame.init()
screen = pygame.display.set_mode([800,600])
pygame.display.set_caption("PONG")

ballpic = pygame.image.load("ball.png")
paddlepic = pygame.image.load("paddle.png")
bgpic = pygame.image.load("bg.jpg")
titlepic = pygame.image.load("Pong.jpg")

hitWallSound = pygame.mixer.Sound("hitWall.wav")
hitPaddleSound = pygame.mixer.Sound("hitPaddle.wav")
lifeLossSound = pygame.mixer.Sound("lifeLoss.wav")
gameOverSound = pygame.mixer.Sound("gameOver.wav")
gameOverPlayed = False


keepGoing = True
ballStartMove = False
font = pygame.font.SysFont("Stencil",24)

hitCounter = 0

paddlex=300
paddley=550
paddleh=25
paddlew=200

ballRad = 20
speedx = 0
speedy = 0
ballx = paddlex + paddlew/2
bally = paddley - ballRad
lives = 5
points = 0

WHITE = (255,255,255)
BLACK = (0,0,0)

timer = pygame.time.Clock()

state = 0
name = ""
capitalLetter = False
scoreRecord = []

def bubbleSort(listToSort):
    if len(listToSort) >1:
        for i in range(len(listToSort)-1):
            for j in range(len(listToSort)-i-1):
                if listToSort[j] < listToSort[j+1]:
                    temp = listToSort[j]
                    listToSort[j] = listToSort[j+1]
                    listToSort[j+1] = temp
    return listToSort

def binarySearch(myList,item):
    low = 0
    high = len(myList)-1

    while low <= high:
        mid = (low + high)/2
        if myList[mid] < item:
            high = mid - 1
        elif myList[mid] > item:
            low = mid + 1
        else:
            return mid
    return -1

while keepGoing:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            keepGoing = False
        if event.type == pygame.MOUSEBUTTONUP:
            if ballStartMove == False and state == 2:
                ballStartMove = True
                while speedx == 0:
                    speedx = random.randint(-7,7)
                speedy = random.randint(-10,-5)
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LSHIFT or event.key == pygame.K_RSHIFT:
                    capitalLetter = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                if state == 0:
                    state = 1
            if state == 1:
                if event.key == pygame.K_LSHIFT or event.key == pygame.K_RSHIFT:
                    capitalLetter = True
                if event.key == pygame.K_RETURN:
                    state = 2
                if event.key >= 48 and event.key <= 122:
                    if capitalLetter and event.key >= 97:
                        name+= chr(event.key-32)
                    else:
                        name += chr(event.key)
                if event.key == pygame.K_BACKSPACE:
                    name = name[:-1]
            if event.key == pygame.K_F1 and state == 3:
                state=0
                lives=5
                points=0
                speedX=0
                speedY=0
                gameStart=False
                hitCounter=0
                radius=20
                name=""
                rank = 0

    ##########states

    if state == 0:
        screen.fill(BLACK)
        screen.blit(titlepic,(0,0))
        starttext = font.render("Press space to start",True,WHITE)
        screen.blit(starttext,(300,500))
        pygame.display.update()


    elif state == 1:
        screen.fill(BLACK)
        nametext = font.render("Please input your name: " + name, True, WHITE)
        screen.blit(nametext, (50,250))

    elif state == 2:    
        if ballStartMove:
            ballx += speedx
            bally += speedy
            if ballx <= ballRad or ballx >= 800-ballRad:
                speedx *= -1
                hitWallSound.play()
                hitCounter+=1
            if bally <= ballRad:
                speedy *= -1
                hitWallSound.play()
                hitCounter+=1
            if bally >= 600 - ballRad:
                lives -= 1
                ballStartMove = False
                speedx = 0
                speedy = 0
                ballx = paddlex + paddlew/2
                bally = paddley - ballRad
                ballRad = 20
                lifeLossSound.play()

        #和板碰撞
        if bally + ballRad >= paddley and bally + ballRad <= paddley + paddleh and speedy > 0:
            if ballx >= paddlex and ballx <= paddlex + paddlew:
                if ballStartMove:
                    speedy *= -1
                    points+=1
                    hitPaddleSound.play()
                    hitCounter+=1

        if hitCounter >=2 and ballRad > 10:
##            ballRad-=1
            hitCounter = 0

        #bg
        screen.fill(BLACK)
        screen.blit(bgpic,(0,0))

        
        #画板
        paddlex = pygame.mouse.get_pos()[0]
        paddlex -= paddlew / 2
        #pygame.draw.rect(screen,WHITE,(paddlex,paddley,paddlew,paddleh))
        screen.blit(paddlepic,(paddlex,paddley))

                
        #画球
        if ballStartMove == False:
            ballx = paddlex + paddlew / 2
        #pygame.draw.circle(screen,WHITE,(int(ballx),int(bally)),ballRad)
        ballpic = pygame.transform.scale(ballpic,(ballRad*2,ballRad*2))
        screen.blit(ballpic,(ballx-ballRad,bally-ballRad))

        #text
        gamestring = "Hello, "+str(name)+" Lives: "+str(lives)+" Score: "+str(points)
        text = font.render(gamestring,True,WHITE)
        screen.blit(text,(50,50))
        #GAME OVER
        if lives < 1:
            scoreRecord.append(points)
            bubbleSort(scoreRecord)
            rank = binarySearch(scoreRecord, points) + 1
            state = 3

    elif state == 3:
        screen.fill(BLACK)
        speedx=0
        speedy=0
        gamestring = "Game over. You got " + str(points) + " and your rank is " + str(rank) + "."
        gamestring1 = "Press F1 to replay."
        text = font.render(gamestring, True, WHITE)
        screen.blit(text, (100,200))
        text1 = font.render(gamestring1, True, WHITE)
        text1_rect = text1.get_rect()
        screen.blit(text1, (100,300))
        if not gameOverPlayed:点击打开链接
            gameOverSound.play()
            gameOverPlayed = True



    pygame.display.update()
    timer.tick(60)
pygame.quit()
初出茅庐,还望多多指教

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值