1.0
import pygame
pygame.init()
screen = pygame.display.set_mode((600, 500))
ball_x = 300
ball_y = 250
while True:
screen.fill((0, 0, 0))
ball_y = ball_y + 1
if ball_y>500:
ball_y=250
pygame.draw.circle(screen, (20, 175, 222), (ball_x,ball_y), 50 )
pygame.display.update()
2.0
import pygame
pygame.init()
screen = pygame.display.set_mode((666, 500))
pygame.display.set_caption("蔡旭坤接小球")
ball_x = 333
ball_y = 166
down = 1
up = 2
left = 3
right = 4
rect_x,rect_y,rect_w,rect_h = 300,460,120,40
while True:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
pygame.quit()
screen.fill((100, 200, 0))
pygame.draw.circle(screen, (20, 40, 60), (ball_x, ball_y), 50)
pygame.draw.rect(screen, (30,90,60), (rect_x,rect_y,rect_w,rect_h))
direction = input("请输入数字1234")
direction = int(direction)
if down == direction:
ball_y += 10
if ball_y > 333:
ball_y = 166
elif up == direction:
pygame.draw.circle(screen, (20, 40, 60), (ball_x, ball_y), 50)
ball_y = ball_y - 10
if ball_y < 0:
ball_y = 166
elif left == direction:
pygame.draw.circle(screen, (20, 40, 60), (ball_x, ball_y), 50)
ball_x = ball_x - 10
if ball_x < 0:
ball_x = 166
elif right == direction:
pygame.draw.circle(screen, (20, 40, 60), (ball_x, ball_y), 50)
ball_x = ball_x + 10
if ball_x > 666:
ball_x = 166
else:
print("系统未升级")
pygame.draw.circle(screen, (255, 255, 255), (ball_x, ball_y), 50)
pygame.display.update()
pygame.quit()
3.0
import random
import pygame
pygame.init()
screen = pygame.display.set_mode((666, 500))
pygame.display.set_caption("蔡旭坤接小球")
ball_x = 333
ball_y = 166
rect_x,rect_y,rect_w,rect_h = 300,460,120,40
score = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
rect_x = rect_x - 50
elif event.key == pygame.K_RIGHT:
rect_x = rect_x + 50
elif event.type == pygame.MOUSEMOTION:
rect_x,_ = event.pos
screen.fill((100, 200, 0))
ball_y = ball_y +1
if ball_y > 500:
ball_y = 0
ball_x = random.randint(1,666)
if rect_x + 120 > 666:
rect_x = 666-120
elif rect_x < 0:
rect_x = 0
if rect_x < ball_x < rect_x+rect_w and rect_y< ball_y<rect_y+rect_h:
score+=1
ball_y = 0
ball_x = random.randint(1, 666)
print("这是第%d次接住"%score)
pygame.draw.rect(screen, (30,90,60), (rect_x,rect_y,rect_w,rect_h))
pygame.draw.circle(screen, (255, 255, 255), (ball_x, ball_y), 25)
pygame.display.update()
pygame.quit()
4.0
import random
import pygame
import time
pygame.init()
screen = pygame.display.set_mode((666, 500))
pygame.display.set_caption("蔡旭坤接小球")
ball_x = 333
ball_y = 166
rect_x,rect_y,rect_w,rect_h = 300,460,120,40
score = 0
font2 = pygame.font.Font("ziti.ttf", 24)
start_time = time.time()
game_time = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
rect_x = rect_x - 50
elif event.key == pygame.K_RIGHT:
rect_x = rect_x + 50
elif event.type == pygame.MOUSEMOTION:
rect_x,_ = event.pos
screen.fill((100, 200, 0))
game_time = 60 - int(time.time() - start_time)
if game_time >= 0:
ball_y = ball_y +1
if ball_y > 500:
ball_y = 0
ball_x = random.randint(1,666)
if rect_x + 120 > 666:
rect_x = 666-120
elif rect_x < 0:
rect_x = 0
if rect_x < ball_x < rect_x+rect_w and rect_y< ball_y<rect_y+rect_h:
score+=1
ball_y = 0
ball_x = random.randint(1, 666)
print("这是第%d次接住"%score)
pygame.draw.rect(screen, (30,90,60), (rect_x,rect_y,rect_w,rect_h))
pygame.draw.circle(screen, (255, 255, 255), (ball_x, ball_y), 25)
imgText = font2.render("分数:%d"%score, True, (0, 0, 0))
screen.blit(imgText, (0,0))
r = int(time.time() - start_time) * 4
SDFSF = font2.render("时间:%d"%game_time,True ,(r,0,0))
screen.blit(SDFSF,(0,20))
pygame.display.update()
else:
print('---------------------%d'%game_time)
screen.fill((100, 200, 0))
end_x = font2.render("时间到,得分%d" % score, True, (0, 0, 0))
screen.blit(end_x,( 333,250))
pygame.display.update()
pygame.quit()
5.0
import random
import pygame
import time
pygame.init()
screen_w,screen_h = 1200,800
screen = pygame.display.set_mode((screen_w,screen_h))
pygame.display.set_caption("蔡旭坤接小球")
ball_x = 333
ball_y = 166
rect_x,rect_y,rect_w,rect_h = 300,screen_h-40,250,40
score = 0
font2 = pygame.font.Font("ziti.ttf", 24)
start_time = time.time()
game_time = 0
ball1 = pygame.image.load("ball1.png")
ball2 = pygame.image.load("ball2.png")
ball3 = pygame.image.load("ball3.png")
cai = pygame.image.load("cai.png")
bg = pygame.image.load("bg2.jpg")
speed = 5
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
rect_x = rect_x - 50
elif event.key == pygame.K_RIGHT:
rect_x = rect_x + 50
elif event.type == pygame.MOUSEMOTION:
rect_x,_ = event.pos
screen.fill((100, 200, 0))
screen.blit(bg, (0, 0))
game_time = 60 - int(time.time() - start_time)
if game_time >= 0:
ball_y = ball_y + speed
if ball_y > screen_h:
ball_y = 0
ball_x = random.randint(1,666)
speed = 1
if rect_x + 120 > 666:
rect_x = 666-120
elif rect_x < 0:
rect_x = 0
if rect_x < ball_x+70 < rect_x+rect_w and rect_y< ball_y + 70 <rect_y+rect_h:
score+=1
ball_y = 0
ball_x = random.randint(1, 666)
rect_w -= 1
speed += 1
print("这是第%d次接住"%score)
pygame.draw.rect(screen, (30,90,60), (rect_x,rect_y,rect_w,rect_h))
if score>60:
screen.blit(cai, (ball_x, ball_y))
elif score>40:
screen.blit(ball3, (ball_x, ball_y))
elif score>20:
screen.blit(ball2, (ball_x, ball_y))
else:
screen.blit(ball1, (ball_x, ball_y))
imgText = font2.render("分数:%d"%score, True, (0, 0, 0))
screen.blit(imgText, (0,0))
r = int(time.time() - start_time) * 4
SDFSF = font2.render("时间:%d"%game_time,True ,(r,0,0))
screen.blit(SDFSF,(0,20))
pygame.display.update()
else:
print('---------------------%d'%game_time)
screen.fill((100, 200, 0))
end_x = font2.render("时间到,得分%d" % score, True, (0, 0, 0))
screen.blit(end_x,( 333,250))
pygame.display.update()
pygame.quit()