1.0
# 1.导入工具包
import pygame
import time
# 2.初始化
pygame.init()
# 3.设置窗口
screen = pygame.display.set_mode((700,600))
# 4,导入图片
ball = pygame.image.load('ball.png')
ball_x = 100
ball_y = 350
# 事件
while True:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
pygame.quit()
screen.fill((230, 230, 230))
if ball_y > 600:
ball_y = 0
# ball_x = ball_x+1 # 修改横轴
ball_y=ball_y+1
# pygame.draw.circle(screen,(100, 210, 250),(300,350),70) # 三原色 red green blue 0-255
screen.blit(ball, (ball_x, ball_y))
pygame.display.update() # 4.刷新
# time.sleep(0.1)
pygame.quit() # 退出游戏
2.0
# 1.导入工具包
import pygame
import random
import time
# 2.初始化
pygame.init()
# 3.设置窗口
screen = pygame.display.set_mode((960,540))
pygame.display.set_caption('接住三级头') # 设置游戏名字
# 4,导入图片
ball = pygame.image.load('ball.png') # 加载图片
bg = pygame.image.load('bg.jpg') # 加载图片
ban = pygame.image.load('ban.png')
ban_x = 100
ban_y = 460
ball_x = 100
ball_y = 350
# 事件
while True:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.MOUSEMOTION:
ban_x, ban_y = event.pos
screen.fill((230, 230, 230))
if ball_y > 600:
ball_y = 0
ball_x = random.randint(0,700)
# ball_x = ball_x+1 # 修改横轴
ball_y=ball_y+5
# pygame.draw.circle(screen,(100, 210, 250),(300,350),70) # 三原色 red green blue 0-255
screen.blit(bg, (0, 0))
screen.blit(ban, (ban_x, ban_y))
screen.blit(ball, (ball_x, ball_y))
pygame.display.update() # 4.刷新
# time.sleep(0.1)
pygame.quit() # 退出游戏
3.0
# 1.导入工具包
import pygame
import random
import time
# 2.初始化
pygame.init()
# 3.设置窗口
screen = pygame.display.set_mode((1280,686))
pygame.display.set_caption('接住三级头') # 设置游戏名字
# 4,导入图片
ball = pygame.image.load('ball.png') # 加载图片
bg = pygame.image.load('bg.jpg') # 加载图片
ban = pygame.image.load('ban.png')
ban_x = 100
ban_y = 606
ball_x = 100
ball_y = 350
score = 0
font = pygame.font.Font('ziti.ttf',24)
# 事件
while True:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.MOUSEMOTION:
ban_x, _ = event.pos
screen.fill((230, 230, 230))
if ball_y > 600:
ball_y = 0
ball_x = random.randint(0,700)
if ban_x < ball_x < ban_x + 418 and ban_y < ball_y <ban_y + 60:
score = score+1
ball_y = 0
ball_x = random.randint(0, 700)
# ball_x = ball_x+1 # 修改横轴
ball_y=ball_y+10
# pygame.draw.circle(screen,(100, 210, 250),(300,350),70) # 三原色 red green blue 0-255
screen.blit(bg, (0, 0))
screen.blit(ban, (ban_x, ban_y))
screen.blit(ball, (ball_x, ball_y))
score += 1
imgtext = font.render('分数:%d'%score,True,(255,255,0))
screen.blit(imgtext, (0, 0))
pygame.display.update() # 4.刷新
# time.sleep(0.1)
pygame.quit() # 退出游戏
4.0
# 1.导入工具包
import pygame
import random
import time
# 2.初始化
pygame.init()
# 3.设置窗口
screen = pygame.display.set_mode((1080,652))
pygame.display.set_caption('接住三级头') # 设置游戏名字
# 4,导入图片
ball = pygame.image.load('ball.png') # 加载图片
bg = pygame.image.load('bg.jpg') # 加载图片
ban = pygame.image.load('ban.png')
ban_x = 100
ban_y = 592
ball_x = 100
ball_y = 0
score = 0
hp = 3
font = pygame.font.Font('ziti.ttf',24)
# 事件
while True:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
pygame.quit() # 退出
elif event.type == pygame.MOUSEMOTION:
ban_x, _ = event.pos
screen.fill((230, 230, 230))
# 判断生命值是否小于0,小于就退出
if hp <= 0:
pygame.quit()
if ban_x < ball_x < ban_x + 418 and ban_y < ball_y <ban_y + 60:
score = score+1
ball_y = 0
ball_x = random.randint(0, 700)
# 判断没接到小球
if ball_y > 652:
hp = hp - 1
ball_y = 0
ball_x = random.randint(0,700)
# ball_x = ball_x+1 # 修改横轴
ball_y=ball_y + score +1
# pygame.draw.circle(screen,(100, 210, 250),(300,350),70) # 三原色 red green blue 0-255
screen.blit(bg, (0, 0))
screen.blit(ban, (ban_x, ban_y))
screen.blit(ball, (ball_x, ball_y))
# 在屏幕显示分数
imgtext = font.render('分数:%d'%score,True,(255,255,0))
screen.blit(imgtext, (0, 0))
# 在屏幕上显示生命值
hptext =font.render("生命值:%d"%hp,True,(255,255,0))
screen.blit(hptext,(900,0))
pygame.display.update() # 4刷新
# time.sleep(0.1)
pygame.quit() # 退出游戏
5.0
预想:
小球速度跟得分进行变化
根据分数改变一级头,二级头,三级头
上下左右(wasd)来控制方向