挡板接球小游戏

学习目标:

`

  • 学习做个小游戏

学习内容:

import pygame
import sys
import random
from pygame.locals import*

pygame.init()

screen = pygame.display.set_mode((640,780),0,32)

ball_x,ball_y = random.randint(30,320),-50
vel_y = random.randint(0,10)
vel_x = random.randint(0,10)
rect_x,rect_y,rect_w,rect_h = 300,740,220,40

lives = 3
score = 0
white = 250,255,250

def print_text(src,font,x,y,text,color=white):
imgText = font.render(text,True,color)
src.blit(imgText,(x,y))

font = pygame.font.Font(“c:/windows/Fonts/Stzhongs.ttf”,32)

while True:

for event in pygame.event.get():

    if event.type == pygame.QUIT:
     
        pygame.quit()
        sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
    rect_x -= 5
if keys[pygame.K_d]:
    rect_x += 5
if keys[pygame.K_w]:
    rect_y -= 5
if keys[pygame.K_s]:
    rect_y += 5
if ball_x > 640 or ball_y > 780 :
    lives-=1
    ball_x,ball_y = random.randint(30,320),-50
if rect_x < 0 or rect_x > 640 or rect_y > 780 or rect_y < 0:
    rect_x,rect_y,rect_w,rect_h = 300,740,220,40
elif (rect_y-ball_y)<30 and ball_x>rect_x and ball_x<(rect_x+rect_w):
    score+=1
    ball_x,ball_y = random.randint(30,320),-50

if lives <= 0:
    print_text(screen,font,100,100,"重新开始")
     
    
    
screen.fill((255, 0, 255))
ball_y += vel_y
ball_x += vel_x
pygame.draw.rect(screen,(30,0,0),(rect_x,rect_y,rect_w,rect_h),0)
pygame.draw.circle(screen, (0, 250, 100), (ball_x, ball_y), 50)

print_text(screen,font,20,0,"lives:"+str(lives))
print_text(screen,font,500,0,
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值