1.0
import pygame
import random
pygame.init()
screen = pygame.display.set_mode((700, 600))
pygame.display.set_caption("接小球游戏")
ballx1 = 350
bally1 =300
color1 = (200,200,200)
r1 = 150
ballx2 = 350
bally2 =300
color2 = (200,200,200)
r2 = 150
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
screen.fill((80, 140, 20))
bally1 += 1
ballx1 += 1
if bally1>600:
bally1=0
ballx1=random.randint(0,700)
color1 = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
r1 = random.randint(1,100)
bally2 += 1
ballx2 += 1
if bally2>600:
bally2=0
ballx2=random.randint(0,700)
color2 = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
r2 = random.randint(1,100)
pygame.draw.circle(screen,color1,(ballx1, bally1),r1)
pygame.draw.circle(screen,color2,(ballx2, bally2),r2)
pygame.display.update()
2.0
import pygame
import random
pygame.init()
screen = pygame.display.set_mode((700, 600))
pygame.display.set_caption("接小球游戏")
ballx1 = 350
bally1 =300
color1 = (200,200,200)
r1 = 50
ban_x, ban_y,ban_width,ban_height = 400,550,220,100
score = 0
live = 3
font = pygame.font.Font(None, 24)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.MOUSEMOTION:
ban_x, a = event.pos
screen.fill((80, 140, 20))
bally1 += 1
if bally1>600:
live = live -1
bally1=0
ballx1=random.randint(0,700)
color1 = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
r1 = random.randint(20,40)
if live<=0:
pygame.quit()
if ban_x<ballx1<ban_x+ban_width and ban_y<bally1<ban_y+ban_height:
score = score+1
bally1 = 0
ballx1 = random.randint(0, 700)
score_text = font.render("score:%d"%score,True,(0,0,0) )
screen.blit(score_text,(10,10))
live_text = font.render("live:%d" % live, True, (0, 0, 0))
screen.blit(live_text, (10, 40))
pygame.draw.circle(screen,color1,(ballx1, bally1),r1)
pygame.draw.rect(screen,(240,197,105),(ban_x, ban_y,ban_width,ban_height ) )
pygame.display.update()