Python是一种计算机程序设计语言。是一种面向对象的动态类型语言,最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越来越多被用于独立的、大型项目的开发。
import sys, pygame, easygui
from pygame.color import THECOLORS
class MyBallClass(pygame.sprite.Sprite):
def __init__(self, image_file, location, speed):
super().__init__()
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
self.rect.topleft = location
self.speed = speed
def move(self):
global score, score_sur, score_font
if self.rect.left < 0 or self.rect.right > width:
self.speed[0] = -self.speed[0]
if self.rect.top < 0:
self.speed[1] = -self.speed[1]
score += 1
score_sur = score_font.render(str(score), 1, THECOLORS['black'])
self.rect = self.rect.move(self.speed)
class MyPaddleClass(pygame.sprite.Sprite):
def __init__(self, location):
super().__init__()
self.image = pygame.Surface([100, 20])
self.image.fill([0, 0, 0])
self.rect = self.image.get_rect()
self.rect.topleft = location
def animate():
global lives, running
screen.fill(color)
myBall.move()
screen.blit(myBall.image, myBall.rect)
screen.blit(myPaddle.image, myPaddle.rect)
screen.blit(score_sur, score_pos)
if myBall.rect.top >= screen.get_rect().bottom:
lives -= 1
if lives == 0:
#ret = easygui.msgbox("Game Over!", 'PyPong')
ret = easygui.buttonbox('Game over! Try again?', 'PyPong', choices=['Ok', 'Cancel'])
if ret == 'Ok':
lives = 3
myBall.rect.topleft = [50, 50]
elif ret == 'Cancel':
running = False
else:
pygame.time.delay(2000)
myBall.rect.topleft = [50, 50]
for i in range(lives):
screen.blit(myBall.image, [width - 40*i, 20])
pygame.display.flip()
size = width, height = 640, 480
color = THECOLORS['white']
img = r'E:/PycharmProjects/m_py/ball.png'
ballStartPos = [10, 10]
speed = [5, 5]
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode(size)
hit = pygame.mixer.Sound('Button.wav')
hit.set_volume(0.4)
screen.fill(color)
myBall = MyBallClass(img, ballStartPos, speed)
myPaddle = MyPaddleClass([270, 400])
ballGroup = pygame.sprite.Group(myBall)
score = 0
score_font = pygame.font.Font(None, 50)
score_sur = score_font.render(str(score), 1, THECOLORS['black'])
score_pos = [10, 10]
lives = 3
clock = pygame.time.Clock()
running = True
while running:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEMOTION:
myPaddle.rect.centerx = event.pos[0]
if pygame.sprite.spritecollide(myPaddle, ballGroup, False):
myBall.speed[1] = -myBall.speed[1]
hit.play()
animate()
pygame.quit()
Python是一种解释型脚本语言,可以应用于以下领域:
¥699.00
¥299.00
¥399.00
¥399.00