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

时间:2023-02-14 22:01:58

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

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

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

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

代码如下:

 import sys, pygame
from random import *
from pygame.locals import *
from pygame.font import *
class MyPlayer(pygame.sprite.Sprite):
def __init__(self, image_file, location, speed):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = location
self.speed = speed
self.status = True
def move(self):
self.rect = self.rect.move(self.speed)
if self.rect.left < or self.rect.right > width:
self.speed[] = -self.speed[]
if self.rect.top < :
self.speed[] = -self.speed[]
if self.rect.bottom > height:
#Game over
self.status = False
class Reflector(pygame.sprite.Sprite):
def __init__(self, image_file, location, speed):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = location
self.speed = speed
def move(self):
self.rect = self.rect.move(self.speed)
if self.rect.left < or self.rect.right > width:
self.speed[] = -self.speed[]
if self.rect.top < or self.rect.bottom > height:
self.speed[] = -self.speed[]
def animate(players):
screen.fill([,,])
for player in players:
player.move()
for player in players:
players.remove(player)
if pygame.sprite.spritecollide(player,players,False):
player.speed[] = -player.speed[]
player.speed[] = -player.speed[]
players.add(player)
player.move()
screen.blit(player.image,player.rect)
pygame.display.flip()
pygame.time.delay() pygame.init()
size = width,height = ,
screen = pygame.display.set_mode(size)
screen.fill([,,])
pygame.display.set_caption("MiaoWa Game")
def play():
img_player = "C:\Users\dswu\Desktop\player.png"
players = pygame.sprite.Group()
for row in range(,):
for column in range(,):
playerLocation = [column*+,row*+]
playerSpeed = [choice([-,]), choice([-,])]
player = MyPlayer(img_player, playerLocation, playerSpeed)
players.add(player)
img_ref_path = "C:\Users\dswu\Desktop\Reflector.png"
ref_pos = [,]
ref_speed = [,]
reflector = Reflector(img_ref_path, ref_pos, ref_speed)
players.add(reflector)
running = True
while running:
key_pressed = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
game.quit()
if event.type == KEYDOWN:
if event.key == K_LEFT:
ref_speed[] = -
elif event.key == K_RIGHT:
ref_speed[] = +
animate(players)
if player.status == False:
running = False
final_text = "Game Over!"
ft_font = pygame.font.Font(None, )
ft_surf = ft_font.render(final_text, , (,,))
screen.blit(ft_surf, [screen.get_width()/ - ft_surf.get_width()/, ])
tip_text = "Type any key to continue"
tip_font = pygame.font.Font(None, )
tip_surf = tip_font.render(tip_text, , (,,))
screen.blit(tip_surf, [screen.get_width()/ - tip_surf.get_width()/, ])
pygame.display.flip()
keepOn = True
while keepOn:
key_pressed = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == KEYDOWN:
play()
play()

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

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

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

由于比较简单,看看代码运行一下应该就差不多明白了,如果有不明白的地方请给我留言,方便一起学习与进步。

平时都在线,欢迎交流。

Version2,增加score计分功能,结构有改动:

import sys, pygame
from random import *
from pygame.locals import *
from pygame.font import *
class MyPlayer(pygame.sprite.Sprite):
def __init__(self, image_file, location, speed):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = location
self.speed = speed
self.status = True
def move(self):
global score, score_font, score_surf
self.rect = self.rect.move(self.speed)
if self.rect.left < or self.rect.right > width:
self.speed[] = -self.speed[]
if self.rect.top < :
self.speed[] = -self.speed[]
score = score +
score_surf = score_font.render(str(score),,(,,))
if self.rect.bottom > height:
#Game over
self.status = False
class Reflector(pygame.sprite.Sprite):
def __init__(self, image_file, location, speed):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = location
self.speed = speed
def move(self):
self.rect = self.rect.move(self.speed)
if self.rect.left < or self.rect.right > width:
self.speed[] = -self.speed[]
if self.rect.top < or self.rect.bottom > height:
self.speed[] = -self.speed[]
def animate(players):
screen.fill([,,])
for player in players:
player.move()
for player in players:
players.remove(player)
if pygame.sprite.spritecollide(player,players,False):
player.speed[] = -player.speed[]
player.speed[] = -player.speed[]
players.add(player)
player.move()
screen.blit(player.image,player.rect)
pygame.display.flip()
pygame.time.delay()
def end():
#
final_text = "Game Over!"
ft_font = pygame.font.Font(None, )
ft_surf = ft_font.render(final_text, , (,,))
screen.blit(ft_surf, [screen.get_width()/ - ft_surf.get_width()/, ])
#
tip_text = "Type any key to continue"
tip_font = pygame.font.Font(None, )
tip_surf = tip_font.render(tip_text, , (,,))
screen.blit(tip_surf, [screen.get_width()/ - tip_surf.get_width()/, ])
#
sc_text = "Your score is "+ str(score)
sc_font = pygame.font.Font(None, )
sc_surf = sc_font.render(sc_text, , (,,))
screen.blit(sc_surf, [screen.get_width()/ - sc_surf.get_width()/, ])
pygame.display.flip()
pygame.init()
size = width,height = ,
screen = pygame.display.set_mode(size)
screen.fill([,,])
pygame.display.set_caption("MiaoWa Game")
score =
score_pos = [, ]
score_font = pygame.font.Font(None, )
score_surf = score_font.render(str(score),,(,,))
img_player = "C:\Users\dswu\Desktop\player.png"
playerSpeed = [choice([-,]), choice([-,])]
player = MyPlayer(img_player, [,], playerSpeed)
players = pygame.sprite.Group()
players.add(player)
img_ref_path = "C:\Users\dswu\Desktop\Reflector.png"
ref_pos = [,]
ref_speed = [,]
reflector = Reflector(img_ref_path, ref_pos, ref_speed)
players.add(reflector)
running = True
while running:
key_pressed = pygame.key.get_pressed()
for event in pygame.event.get():
if player.status == True:
if event.type == pygame.QUIT:
game.quit()
if event.type == KEYDOWN:
if event.key == K_LEFT:
ref_speed[] = -
elif event.key == K_RIGHT:
ref_speed[] = +
if player.status == False:
if event.type == pygame.QUIT:
pygame.quit()
if event.type == KEYDOWN:
player.rect.topleft = [,]
player.status = True
score =
score_surf = score_font.render(str(score),,(,,))
if player.status == True:
animate(players)
screen.blit(score_surf, score_pos)
pygame.display.flip()
if player.status == False:
end()

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