import random
import pygame
from pygame import *
import time
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("接小球游戏")
rect_x, rect_y, rect_w, rect_h = 0, 460, 120, 40
ball_x = 100
ball_y = 100
down = True
while True:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
pygame.quit()
elif event.type==pygame.MOUSEMOTION:
rect_x,_ = event.pos
screen.fill((255,255,255))
ball_y+=1
if ball_y >500:
ball_y=0
ball_x = random.randint(0,600)
pygame.draw.circle(screen, (0,255,255),(ball_x ,ball_y),15,0)
pygame.draw.rect(screen, (30, 0, 0), (rect_x, rect_y, rect_w, rect_h ), 0)
pygame.display.update()
pygame.quit()
```pythom
import random
import pygame
from pygame import *
import time
pygame.init()
screen = pygame.display.set_mode((1200,600))
pygame.display.set_caption("接小球游戏")
rect_x, rect_y, rect_w, rect_h = 0, 540, 220, 40
ball_x = 100
ball_y = 100
speed = 1
down = True
font = pygame.font.Font('ziti.ttf', 24)
lives = 3
score = 0
ball = pygame.image.load("cai.png")
bg = pygame.image.load('bg.jpg')
while True:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
pygame.quit()
elif event.type==pygame.MOUSEMOTION:
rect_x,_ = event.pos
screen.blit(bg,(-500,-300))
ball_y+=speed
if ball_y > 600:
lives -= 1
ball_y=0
ball_x = random.randint(0,600)
speed-= 1
imgText = font.render("score%d"%score,True,(0,0,0))
screen.blit(imgText,(500,0))
if rect_x < ball_x < rect_x + 120 and ball_y>rect_y:
ball_y = 0
ball_x = random.randint(0, 600)
score+=1
speed += 1
imgText = font.render("生命值:%d"%lives, True, (0,0,0))
screen.blit(imgText,(0,0))
screen.blit(ball, (ball_x,ball_y))
pygame.draw.rect(screen, (30, 0, 0), (rect_x, rect_y, rect_w, rect_h ), 0)
pygame.display.update()
if lives<=0:
pygame.quit()
3.0
import random
import pygame
from pygame import *
import time
def figure():
pass
def myFont(screen, x, y, text, size=24, color=(0, 0, 0)):
font = pygame.font.Font('ziti.ttf', size)
imgText3 = font.render(text, True, color)
screen.blit(imgText3, (x, y))
def game():
screen_w, screen_h = 1080, 652
title = "接小球游戏"
rect_x, rect_y, rect_w, rect_h = 0, 612, 220, 40
ball_x = 100
ball_y = 100
speed = 10
lives = 3
score = 0
game_over = True
ball_image = "cai.png"
bg_image = 'bg1.jpg'
screen = pygame.display.set_mode((screen_w, screen_h))
pygame.display.set_caption(title)
ball = pygame.image.load(ball_image)
bg = pygame.image.load(bg_image)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.MOUSEBUTTONUP:
if game_over:
game_over = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
rect_x -= 10
elif event.key == pygame.K_RIGHT:
rect_x += 10
elif event.type == pygame.MOUSEMOTION:
rect_x, _ = event.pos
screen.fill((255, 255, 255))
if game_over:
myFont(screen, screen_w / 2, screen_h / 2, "点击屏幕开始游戏")
else:
screen.blit(bg, (0, 0))
ball_y += speed
if ball_y > screen_h:
lives -= 1
ball_y = 0
ball_x = random.randint(0, screen_w)
speed -= 1
imgText = font.render("score%d" % score, True, (0, 0, 0))
screen.blit(imgText, (screen_w - 100, 0))
if rect_x < ball_x + 82 < rect_x + 120 and ball_y + 50 > rect_y:
ball_y = 0
ball_x = random.randint(0, screen_w)
score += 1
speed += 1
imgText = font.render("生命值:%d" % lives, True, (0, 0, 0))
screen.blit(imgText, (0, 0))
screen.blit(ball, (ball_x, ball_y))
pygame.draw.rect(screen, (30, 0, 0), (rect_x, rect_y, rect_w, rect_h), 0)
pygame.display.update()
if lives <= 0:
pygame.quit()
pygame.display.update()
def main():
pygame.init()
game()
if __name__ == '__main__':
main()