Commit 8193fcd6 by BellCodeEditor

auto save

parent dfb5d1b6
Showing with 80 additions and 0 deletions
import pygame, sys # Setup
pygame.init()
screen = pygame.display.set_mode([800, 600]) #窗口大小设置
pygame.display.set_caption("Smiley Pong") #窗口标题
pic = pygame.image.load("CrazySmile.bmp") #加载图片
picx = 0 #图片的x坐标
picy = 0 #图片的坐标
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
timer = pygame.time.Clock()
speedx = 5 #弹球在X坐标方向移动像素
speedy = 5 #弹球在y坐标方向移动像素
paddlew = 200 # 挡板的宽
paddleh = 25 # 挡板的高
paddlex = 300 # 挡板的X坐标
paddley = 550 # 挡板的y坐标
picw = 100 #图片的宽度
pich = 100 #图片的高度
points = 0 #得分
lives = 5 #生命值
font = pygame.font.SysFont("Times", 24)
while True: # Game loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.quit()
if event.type == pygame.KEYDOWN:
# 按下F1键盘,重新开始游戏
if event.key == pygame.K_F1:
points = 0
lives = 5
picx = 0
picy = 0
speedx = 5
speedy = 5
picx += speedx # 移动弹球的X坐标
picy += speedy # 移动弹球的y坐标
# 如果弹球超出窗口左边或右边,将弹球方向翻转
if picx <= 0 or picx + pic.get_width() >= 800:
speedx = -speedx
# 如果弹球超出窗口顶部,将弹球方向翻转
if picy <= 0:
speedy = -speedy
# 如果弹球超出窗口底部,生命数减1,并且将弹球方向翻转
if picy >= 500:
lives -= 1
speedy = -speedy
screen.fill(BLACK) # 用白色填充Surface对象
screen.blit(pic, (picx, picy)) # 复制弹球界面到主界面上
# 绘制和移动挡板
paddlex = pygame.mouse.get_pos()[0] # X坐标随着鼠标移动而变化
paddlex -= paddlew /2
pygame.draw.rect(screen, WHITE, (paddlex, paddley, paddlew, paddleh))
# 如果弹球的底部碰到弹球而且速度大于0,并且一半的弹球的X坐标落在挡板上,弹球就可以反弹
if picy + pich >= paddley and picy + pich <= paddley + paddleh and speedy > 0:
if picx + picw / 2 >= paddlex and picx + picw / 2 <= paddlex + paddlew:
points += 1
speedy = -speedy
# 绘制生命数和得分
draw_string = "Lives: " + str(lives) + " Points: " + str(points)
# 生命数为零,停止游戏
if lives < 1:
speedx = speedy = 0
draw_string = "Game Over. Your score was: " + str(points)
draw_string += ". Press F1 to play again. "
text = font.render(draw_string, True, WHITE)
text_rect = text.get_rect()
text_rect.centerx = screen.get_rect().centerx
text_rect.y = 10
screen.blit(text, text_rect)
pygame.display.update()
timer.tick(60)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment