用Python写一个乒乓球类的游戏

最近开始学Python,感觉挺好玩的,既有脚本语言的灵活性,又有丰富的类库与面向对象的特点,开发起来很方便。

游戏的规则和乒乓球一样,如果妙蛙种子掉地上了就算输,你可以用蓝色的跷跷板弹它,使他不落到地面上。

Game Over后可按任意键继续游戏或选择退出。

代码如下:

  1 import sys, pygame
  2 from random import *
  3 from pygame.locals import *
  4 from pygame.font import *
  5 class MyPlayer(pygame.sprite.Sprite):
  6     def __init__(self, image_file, location, speed):
  7         pygame.sprite.Sprite.__init__(self)
  8         self.image = pygame.image.load(image_file)
  9         self.rect = self.image.get_rect()
 10         self.rect.left, self.rect.top = location
 11         self.speed = speed
 12         self.status = True
 13     def move(self):
 14         self.rect = self.rect.move(self.speed)
 15         if self.rect.left < 0 or self.rect.right > width:
 16             self.speed[0] = -self.speed[0]
 17         if self.rect.top < 0:
 18             self.speed[1] = -self.speed[1]
 19         if self.rect.bottom > height:
 20             #Game over
 21             self.status = False
 22 class Reflector(pygame.sprite.Sprite):
 23     def __init__(self, image_file, location, speed):
 24         pygame.sprite.Sprite.__init__(self)
 25         self.image = pygame.image.load(image_file)
 26         self.rect = self.image.get_rect()
 27         self.rect.left, self.rect.top = location
 28         self.speed = speed
 29     def move(self):
 30         self.rect = self.rect.move(self.speed)
 31         if self.rect.left < 0 or self.rect.right > width:
 32             self.speed[0] = -self.speed[0]
 33         if self.rect.top < 0 or self.rect.bottom > height:
 34             self.speed[1] = -self.speed[1]
 35 def animate(players):
 36     screen.fill([255,255,255])
 37     for player in players:
 38         player.move()
 39     for player in players:
 40         players.remove(player)
 41         if pygame.sprite.spritecollide(player,players,False):
 42             player.speed[0] = -player.speed[0]
 43             player.speed[1] = -player.speed[1]
 44         players.add(player)
 45         player.move()
 46         screen.blit(player.image,player.rect)
 47     pygame.display.flip()
 48     pygame.time.delay(10)
 49
 50 pygame.init()
 51 size = width,height = 640,480
 52 screen = pygame.display.set_mode(size)
 53 screen.fill([255,255,255])
 54 pygame.display.set_caption("MiaoWa Game")
 55 def play():
 56     img_player = "C:\Users\dswu\Desktop\player.png"
 57     players = pygame.sprite.Group()
 58     for row in range(0,1):
 59         for column in range(0,1):
 60             playerLocation = [column*250+10,row*250+10]
 61             playerSpeed = [choice([-2,2]), choice([-2,2])]
 62             player = MyPlayer(img_player, playerLocation, playerSpeed)
 63             players.add(player)
 64     img_ref_path = "C:\Users\dswu\Desktop\Reflector.png"
 65     ref_pos = [0,464]
 66     ref_speed = [0,0]
 67     reflector = Reflector(img_ref_path, ref_pos, ref_speed)
 68     players.add(reflector)
 69     running = True
 70     while running:
 71         key_pressed = pygame.key.get_pressed()
 72         for event in pygame.event.get():
 73             if event.type == pygame.QUIT:
 74                 game.quit()
 75             if event.type == KEYDOWN:
 76                 if event.key == K_LEFT:
 77                     ref_speed[0] = -2
 78                 elif event.key == K_RIGHT:
 79                     ref_speed[0] = +2
 80         animate(players)
 81         if player.status == False:
 82             running = False
 83     final_text = "Game Over!"
 84     ft_font = pygame.font.Font(None, 100)
 85     ft_surf = ft_font.render(final_text, 1, (0,0,0))
 86     screen.blit(ft_surf, [screen.get_width()/2 - ft_surf.get_width()/2, 100])
 87     tip_text = "Type any key to continue"
 88     tip_font = pygame.font.Font(None, 50)
 89     tip_surf = tip_font.render(tip_text, 1, (0,0,0))
 90     screen.blit(tip_surf, [screen.get_width()/2 - tip_surf.get_width()/2, 200])
 91     pygame.display.flip()
 92     keepOn = True
 93     while keepOn:
 94         key_pressed = pygame.key.get_pressed()
 95         for event in pygame.event.get():
 96             if event.type == pygame.QUIT:
 97                 pygame.quit()
 98             if event.type == KEYDOWN:
 99                 play()
100 play() 

主要是通过pygame.sprite.Sprite类实现碰撞的监控,通过事件的捕捉及判断实现这种弹力球类的游戏,其中涉及到文字在界面上的显示,游戏中的循环控制,以及重新开始游戏等。

把上面这段代码贴到你的IDLE中,找到下面这两行替换成你机器中的图片路径,按F5就可以运行了。

img_player = "C:\Users\dswu\Desktop\player.png"

img_ref_path = "C:\Users\dswu\Desktop\Reflector.png"

时间: 2024-08-06 19:12:36

用Python写一个乒乓球类的游戏的相关文章

Python中写一个乒乓球类的游戏

最近开始学Python,感觉挺好玩的,既有脚本语言的灵活性,又有丰富的类库与面向对象的特点,开发起来很方便. 游戏的规则和乒乓球一样,如果妙蛙种子掉地上了就算输,你可以用蓝色的跷跷板弹它,使他不落到地面上. Game Over后可按任意键继续游戏或选择退出. 代码如下: 1 import sys, pygame 2 from random import * 3 from pygame.locals import * 4 from pygame.font import * 5 class MyPl

用Python写一个ftp下载脚本

用Python写一个ftp下载脚本 ----基于Red Hat Enterprise Linux Server release 6.4 (Santiago):python 2.6.6 Ps:少侠我接触Python半个月以来接到的第一个需求,虽然如此简单的一个脚本,少侠我磕磕绊绊却用了将近一天半的时间才写出来,但还是很开心,毕竟也粗来了,废话不多说,切入正题.因为一开始没有用过ftplib模块,所以各种谷歌度娘一堆资料杂乱不堪,话不清,理不乱的,本文实现的功能简单,下面介绍一下,以免误导读者. 需

老男孩教育每日一题-2017-04-17:使用Shell或Python写一个脚本,CPU使用率超过80%或硬盘超过85%邮件报警

老男孩教育每日一题-2017-04-17: 使用Shell或Python写一个脚本,CPU使用率超过80%或硬盘超过85%邮件报警. 今天是老男孩教育每日一题陪伴大家的第29天.

python写一个脚本解析文件

Python写一个脚本解析文件 ----基于Red Hat Enterprise Linux Server release 6.4 (Santiago):python 2.6.6 需求: 1.去掉空行 2.去掉空行后输出到一个新文件 附加需求(排版):1.'-'缩进n个字符 '-'缩进2n个字符 以此类推 2.'-'开头的所有句子输出在一行 '-'开头的句子输出在一行 以此类推 --------------------------------------------分隔线------------

python写一个通讯录V2.0

python写一个通讯录step by step V2.0 引用知识 list + dict用于临时存储用户数据信息 cPickle用于格式化文件存取 依旧使用file来进行文件的存储 解决问题 1.操刀开始去做 原始代码 实现功能(可做模板) 1.判断输入内容是否在给出的menu目录内,在的话,返回对应结果,不在就报错 2.调用os模块的exit功能 3.字典配合循环加上函数实现switch的功能 #!/usr/bin/env python #coding:utf8 #Author:zhuim

[py]python写一个通讯录step by step V3.0

python写一个通讯录step by step V3.0 参考: http://blog.51cto.com/lovelace/1631831 更新功能: 数据库进行数据存入和读取操作 字典配合函数调用实现switch功能 其他:函数.字典.模块调用 注意问题: 1.更优美的格式化输出 2.把日期换算成年龄 3.更新操作做的更优雅 准备工作 db准备 - 创建数据库 mysql> create database txl charset utf8; Query OK, 1 row affecte

十行代码--用python写一个USB病毒 (知乎 DeepWeaver)

昨天在上厕所的时候突发奇想,当你把usb插进去的时候,能不能自动执行usb上的程序.查了一下,发现只有windows上可以,具体的大家也可以搜索(搜索关键词usb autorun)到.但是,如果我想,比如,当一个usb插入时,在后台自动把usb里的重要文件神不知鬼不觉地拷贝到本地或者上传到某个服务器,就需要特殊的软件辅助. 于是我心想,能不能用python写一个程序,让它在后台运行.每当有u盘插入的时候,就自动拷贝其中重要文件. 如何判断U盘的插入与否? 首先我们打开电脑终端,进入/Volume

python写一个通讯录

闲着没事,用python写一个模拟通讯录,要求要实现常用的通讯录的功能,基本流程如下 ? 接下来就按照这个流程实现各个模块的功能 1. 定义一个类,并初始化 1 import json 2 import time 3 4 5 class Address(object): 6 def __init__(self): 7 with open("通讯录.txt", 'r', encoding='utf-8') as f: 8 self.data = json.loads(f.read())

用Python写一个小游戏

刚学Python时间不长,但也知道了一点,看别人的参考写了一个猜数字小游戏,也算是禹学于乐吧. #!/usr/bin/env   python #coding=utf-8 import random secret = random.randint(1,100) guess,tries = 0,0 print u"已经给出了一个1-99的数字" while guess != secret and tries < 5: print u"请给出你猜的数字:" pri