`
学习做个小游戏 学习内容:
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,"score:"+str(score))
pygame.time.delay(10)
if lives <= 0:
screen.fill((0,0,255))
print_text(screen,font,300,390,"重新开始")
if event.type == pygame.MOUSEBUTTONDOWN:
screen.fill((255, 0, 255))
if event.type == pygame.MOUSEBUTTONUP:
lives = 3
score = 0
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,"score:"+str(score))
pygame.time.delay(10)
pygame.display.update()
List item
文章目录提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
学习目标:学习内容:前言一、pygame是什么?二、使用步骤
1.引入库2.代码解释
前言
自学的一个接球小游戏,应该还有很多问题
`
一、pygame是什么?Pygame是跨平台Python模块,专为电子游戏设计。包含图像、声音。创建在SDL基础上,允许实时电子游戏研发而无需被低级语言,如C语言或是更低级的汇编语言束缚
二、使用步骤 1.引入库代码如下(示例):
import pygame
import sys
import random
from pygame.locals import*
pygame.init()
2.代码解释if event.type == pygame.MOUSEBUTTONDOWN:#鼠标点击
screen.fill((255, 0, 255))#描绘屏幕 但我不知道有没有用放这里
if event.type == pygame.MOUSEBUTTONUP:#鼠标弹起
imgText = font.render(text,True,color)
my_font.render(text, antialias, color, background=None)
(my_font为上一个方法定义的Font对象)
参数text:文本字符串;
参数antialias:为True时文本图像显示更光滑,为False时文本图像显示有锯齿状
参数color:文本的颜色
参数background:为文本背景颜色,默认为小黑屏
src.blit(imgText,(x,y))#图像,图像位置 把文字变成图像显示出来
print_text(screen,font,20,0,"lives:"+str(lives))#左上角显示文字 后面的+str(lives)可以记录数值的变化 print_text(screen,font,500,0,"score:"+str(score))#同上
-–总结
一个能用挡板接球的小游戏,小球掉落的速度是随机的,挡板可以上下左右移动,接到小球加一分,小球掉出屏幕外减一次生命值,生命值为0时会出现重新开始的文字,鼠标随便点击一下游戏可以重新开始。初学如有很多问题可以指出来。
这里写自定义目录标题
学习目标:学习内容:前言一、pygame是什么?二、使用步骤
1.引入库2.代码解释



