弹球

pong.py

 import sys
 import pygame
 from pygame.locals import *

 class MyBallClass(pygame.sprite.Sprite):
     def __init__(self, img_file, location, speed):
         pygame.sprite.Sprite.__init__(self)
         self.image = pygame.image.load(img_file)
         self.rect = self.image.get_rect()
         self.rect.left, self.rect.top = location
         self.speed = speed
     def move(self):
         global points
         self.rect = self.rect.move(self.speed)
         if self.rect.left<0 or self.rect.right>screen.get_width():
             self.speed[0] *= -1
             if self.rect.bottom <= paddle.rect.top+1:
                 points += 1
                 ball_bounce.play()
         if self.rect.top <= 0:
             self.speed[1] *= -1
             points += 1
             ball_bounce.play()
 class MyPaddleClass(pygame.sprite.Sprite):
     def __init__(self, img_file, location):
         pygame.sprite.Sprite.__init__(self)
         self.image = pygame.image.load(img_file)
         self.rect = self.image.get_rect()
         self.rect.left, self.rect.top = location

 pygame.init()
 pygame.mixer.init()
 splat = pygame.mixer.Sound("audio\\ball_bounce.wav")
 ball_bounce = pygame.mixer.Sound("audio\\balloon_pop.wav")
 screen = pygame.display.set_mode([1024, 768])
 pygame.display.set_caption('火龙果2')
 clock = pygame.time.Clock()
 ball_speed = [2, 1]
 ball_img = 'img\\dragon (1).ico'
 myBall = MyBallClass(ball_img, [50,50], ball_speed)
 ballGroup = pygame.sprite.Group(myBall)
 paddle_img = 'img\\apple2.jpg'
 paddle = MyPaddleClass(paddle_img, [400, 550])
 points = 0
 font = pygame.font.Font(None, 100)
 collide = False #是否相撞
 lives = 3
 lives_image = pygame.image.load("img\\pig.ico")
 gameover = False
 while 1:
     clock.tick(150)
     screen.fill([255, 255, 255])
     for event in pygame.event.get():
         if event.type == QUIT:
             sys.exit()
         elif event.type == MOUSEMOTION:
             paddle.rect.centerx = event.pos[0]
     if pygame.sprite.spritecollide(paddle, ballGroup, False):
         if myBall.rect.bottom <= 551:
             myBall.speed[1] = -abs(myBall.speed[1])
         if (not collide) and (myBall.rect.bottom<=paddle.rect.top+1):
             points += 1
             splat.play()
         collide = True
     else:
         collide = False

     if not gameover:
         myBall.move()
         screen.blit(myBall.image, myBall.rect)
         screen.blit(paddle.image, paddle.rect)
         score_str = 'Score:' + str(points)
         score_text = font.render(score_str, 1, (0,0,0))
         score_rect = score_text.get_bounding_rect()
         score_width = score_rect.width
         textpos = [screen.get_rect().width-score_width-10, 20]
         screen.blit(score_text, textpos)
         for i in range(lives-1):
             screen.blit(lives_image, [128*i, 0])
         pygame.display.flip()

     if myBall.rect.top >= screen.get_rect().bottom:
         lives -= 1
         if lives == 0:
             final_text1 = "Game Over"
             final_text2 = "Your final score is: " + str(points)
             font1 = pygame.font.Font(None, 70)
             surf1 = font1.render(final_text1, 1, (0,0,0))
             font2 = pygame.font.Font(None, 50)
             surf2 = font2.render(final_text2, 1, (0,0,0))
             screen.blit(surf1, [screen.get_width()/2 - surf1.get_width()/2, 200])
             screen.blit(surf2, [screen.get_width()/2 - surf2.get_width()/2, 300])
             pygame.display.flip()
             gameover = True
         else:
             pygame.time.delay(1000)
             myBall.rect.topleft = [lives*128, 0]

Python -- 游戏开发 -- PyGame的使用的更多相关文章

  1. 【1】【MOOC】Python游戏开发入门-北京理工大学【第二部分-游戏开发之框架】

    学习地址链接:http://www.icourse163.org/course/0809BIT021E-1001873001?utm_campaign=share&utm_medium=and ...

  2. Python游戏开发:pygame游戏开发常用数据结构

    一.数组与列表 数组可以理解为简化的列表.像我们之前使用的pygame.sprite.Group这样的精灵组,也是一个列表.列表的元素是可变的,它具有添加.删除.搜索.排序等多种方法. 1.一维列表 ...

  3. Python游戏开发——打砖块

    打砖块游戏向来大家也不会很陌生,今天来用python来开发一下这个小游戏 1.引用对应数据库 import pygame from pygame.locals import * import sys, ...

  4. Coco2d-x android win7 Python 游戏开发环境的搭建

    1:我用的电脑配置 win7 3 核 内存8G 桌面.一直想学习Coco2d 游戏开发,所以,一个星期后,需要找到,最终建立了一个良好的环境 2:我使用的版本号版本号,至于建筑android开发环境略 ...

  5. Python游戏开发入门

    Pygame简介与安装 1.Pygame安装 pip install pygame2.检测pygame是否安装成功 python -m pygame.examples.aliens Pygame最小开 ...

  6. python游戏开发:pygame事件与设备轮询

    一.pygame事件 1.简介 pygame事件可以处理游戏中的各种事情.其实在前两节的博客中,我们已经使用过他们了.如下是pygame的完整事件列表: QUIT,ACTIVEEVENT,KEYDOW ...

  7. python游戏开发:pygame中的IO、数据

    一.python输入输出 1.输出 python一次可以打印多个变量,只要用一个逗号将每个变量隔开就可以了.比如: A = 123B = "ABC"C = 456D = " ...

  8. Python游戏编程(Pygame)

    安装Pygame pip install pygame C:\Users> pip install pygame Collecting pygame Downloading https://fi ...

  9. python游戏开发之俄罗斯方块(一):简版

    编程语言:python(3.6.4) 主要应用的模块:pygame (下面有源码,但是拒绝分享完整的源码,下面的代码整合起来就是完整的源码) 首先列出我的核心思路: 1,图像由"核心变量&q ...

随机推荐

  1. [译]Autoprefixer:一个以最好的方式处理浏览器前缀的后处理程序

    Autoprefixer解析CSS文件并且添加浏览器前缀到CSS规则里,使用Can I Use的数据来决定哪些前缀是需要的.   所有你需要做的就是把它添加到你的资源构建工具(例如 Grunt)并且可 ...

  2. 安卓添加USB外置UVC摄像头

    实现的方法有很多种,按步骤来看适合哪一种,网上说什么接采集卡,其实就是把AV转成UVC,现在市面上很多摄像头直接就已经是UVC的了,在windows上面即插即用. 安卓也是Linux,这个就好办了. ...

  3. This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed(在64位模式下运行安装了32位的Oracle客户端组件时,会发生此问题)

    部署win服务时出现下面的问题: 在事件查看器中看到如下错误: 日志名称: Application来源: ***调度服务日期: 2014/5/21 12:53:21事件 ID: 0任务类别: 无级别: ...

  4. 【PAT Advanced Level】1015. Reversible Primes (20)

    转换进制&&逆序可以在一起进行,有一点技巧,不要用十进制数来表示低进制,容易溢出. #include <iostream> #include <vector> ...

  5. Java实现wc部分功能

    GitHub仓库:https://github.com/TaoTaoLv1/WcProject 一.开发前PSP表格预估* PSP2.1 Personal Software Process Stage ...

  6. java实现自动生成四则运算

    Github项目链接:https://github.com/shoulder01/Fouroperation.git 一.项目相关要求 1. 使用 -n 参数控制生成题目的个数(实现) 2.使用 -r ...

  7. swipe js bug

    最近因为要写新的mobile site页面,有好几个页面上面必须用到photo slider. 使用插件: /* * Swipe 2.0 * * Brad Birdsall * Copyright 2 ...

  8. MRP备忘

    mrp自动发放计划 物料:采购员 计划员 自动发放时间栏 提前期 mrp:product profile PO:profile自动生成请求 Auto-Release Planned Orders In ...

  9. MySQL常用功能语句分类总结

    C(创建型功能) 创建数据库:CREATE DATABASE 创建用户:CREATE USER 用户名@主机 IDENTIFIED BY 密码; -- 如果想让该用户可以从任意远程主机登陆,可以使用通 ...

  10. VS2012 中不能安装ARCGIS 10.0 SDK的解决方法

    问题描述 在ARCGIS 安装SDK时 就会出错   原因是ARCGIS SDK 10.0只能在VS2010中安装,因为我本机是安的VS2012所以安装不成功.     解决方法 一,在注册表中HKE ...