import pygame
pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption('窗体标题')
while True:
for event in pygame.event.get():
if event.typt == pygame.QUIT:
exit()
import pygame
pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption('显示图片')
sereen.fill((255, 255, 255))
img = pygame.image.load('files/boer.jpg')
screen.blit(img, (0, 0))
w, h = img.get_size()
screen.blit(img, (600-w, 400-h))
new_img1 = pygame.transform.scale(img, (100, 100))
screen.blit(new_img1, (210, 0))
new_img2 = pygame.transfrom.rotozoom(img, 90, 0.5)
screen.blit(new_img2, (0, 200))
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.typt == pygame.QUIT:
exit()
import pygame
pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption('显示文字')
sereen.fill((255, 255, 255))
font = pygame.font.Font('files/a.ttf', 30)
text = font.rander('Pygame 你好!', True, (255, 0, 0), (255, 255, 0))
screen.blit(text, (0, 0))
w, h = text.get_size()
screen.blit(text, (600-w, 400-h))
new_text1 = pygame.transform.scale(text, (200, 50))
screen.blit(new_text1, (400, 50))
new_text2 = pygame.transform.rotozoom(text, 90 ,2)
screen.blit(new_text2, (0, 120))
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.typt == pygame.QUIT:
exit()
import pygame
pygame.init()
screen = pygame.display.set_mode((600, 400))
pygame.display.set_caption('显示图形')
sereen.fill((255, 255, 255))
pygame.draw.line(screen, (255, 0, 0), False, (10, 20), (200, 20))
points = [(10, 300), (100, 160), (180, 260), (300, 100)]
pygame.draw.lins(screen, (0, 255, 0), points, 2)
pygame.draw.circle(screen, (0, 0, 255), (200, 250), 100)
pygame.draw.rect(screen, (120, 120, 60), (30, 70, 200, 100), 3)
ellipse(窗口, 线的颜色, 矩形范围(x,y,宽度,高度), 线宽=0 - 填充)
pygame.draw.ellipse(screen, (255, 0, 0), (30, 70, 200, 100) ,3)
from math import pi
pygame.draw.arc(screen, (0, 0, 0), (30, 70, 200, 100), 0, pi, 5)
'''
--- 弧度 ---
2 * pi = 360°
pi = 180°
90
180 0
270
'''
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.typt == pygame.QUIT:
exit()
import pygame
WIN_WIDTH = 400
WIN_HEIGHT = 600
pygame.init()
screen = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption('动画原理')
sereen.fill((255, 255, 255))
pygame.display.flip()
y = 100
r = 50
pygame.draw.circle(screen, (255, 0, 0), (100, y), 50)
pygame.display.update()
num = 1
while True:
num += 1
if num % 10 == 0:
pygame.draw.circle(screen, (255, 255, 255), (100, y), 50)
y = y + 1
pygame.draw.circle(screen, (255, 0, 0), (100, y), 50)
pygame.display.update()
if num % 10 == 0:
pygame.draw.circle(screen, (255, 255, 255), (100, y), r)
r += 1
pygame.draw.circle(screen, (255, 0, 0), (100, y), r)
pygame.display.update()
if num % 10 == 0:
screen.fill((255, 255, 255))
pygame.draw.circle(screen, (255, 0, 0), (100, y), r)
d += 1
new_image = pygame.transform.rotozoom(img, d, 1)
screen.blit(new_image, (ix, iy))
pygame.display.update
if num % 10 == 0:
pygame.draw.circle(screen, (255, 255, 255), (100, y), r)
y += 10
r += 1
pygame.draw.circle(screen, (255, 0, 0), (100, y), r)
pygame.display.update()
for event in pygame.event.get():
if event.typt == pygame.QUIT:
exit()
import pygame
WIN_WIDTH = 400
WIN_HEIGHT = 600
pygame.init()
screen = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption('动画灵活应用')
sereen.fill((255, 255, 255))
pygame.display.flip()
x, y = 200, 30
r = 30
y_speed = 1
pygame.draw.circle(screen, (0, 255, 0), (x, y), r)
pygame.display.update()
num = 0
while True:
num += 1
if num % 10 == 0:
screen.fill((255, 255, 255))
y += y_speed
if y >= WIN_HEIGHT - r:
y_speed = y_speed * -1
if y <= r:
y_speed = y_speed * -1
pygame.draw.circle(screen, (0, 255, 0), (x, y), r)
pygame.display.update()
for event in pygame.event.get():
if event.typt == pygame.QUIT:
exit()
import pygame
from random import randint
WIN_WIDTH = 400
WIN_HEIGHT = 600
pygame.init()
screen = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption('事件检测')
sereen.fill((255, 255, 255))
pygame.display.flip()
font = pygame.font.Font('files/font.ttf', 30)
count = 0
while True:
for event in pygame.event.get():
count += 1
print(str(count))
'''
鼠标事件
MOUSEBUTTONDOWN - 鼠标按下
MOUSEBUTTONUP - 鼠标弹起
MOUSERMOTION - 鼠标移动
鼠标位置属性(点在哪) - pos
键盘事件
KEYDOWN - 按键按下
KEYUP - 按键弹起
按键值属性(哪个按键) - key
'''
if event.typt == pygame.QUIT:
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
print('鼠标按下', event.pos)
mx, my = event.pos
pygame.drwa.circle(screen, (255, 255, 0), (mx, my), 50)
pygame.display.update()
elif event.type == pygame.MOUSEBUTTONUP:
print('鼠标弹起')
elif event.type == pygame.MOUSERMOTION:
print('鼠标移动', event.pos)
mx, my = event.pos
r = randint(0, 255)
g = randint(0, 255)
b = randint(0, 255)
pygame.drwa.circle(screen, (r, g, b), (mx, my), 50)
pygame.display.update()
elif event.type == pygame.KEYDOWN:
print('按键按下', event.key, chr(event.key))
text = fount.render(chr(event.key), True, (255, 0, 0))
screen.fill((255, 255, 255))
screen.blit(text,(0, 300))
pygame.display.update()
elif event.type == pygame.KEYUP:
print('按键弹起', event.key)
import pygame
WIN_WIDTH = 400
WIN_HEIGHT = 600
pygame.init()
screen = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption('按钮点击')
sereen.fill((255, 255, 255))
pygame.display.flip()
bx, by, bw, bh = 30, 100, 100, 50
font = pygame.font.Font('files/font.ttf', 30)
pygame.draw.rect(screen, (255, 0, 0),(bx, by, bw, bh))
text1 = font.render('确定', True, (255, 255, 255))
tw1, th1 = text1.get_size()
tx1 = bx + bw/2 - tw1/2
ty1 = by + bh/2 - th1/2
screen.blit(text1, (tx1, ty1))
pygame.draw.rect(screen, (0, 255, 0), (bx, by + 100, bw, bh))
text2 = font.render('取消', True, (255, 255, 255))
tw2, th2 = text2.get_size()
tx2 = bx + bw/2 - tw2/2
ty2 = by + 100 + bh/2 - th2/2
screen.blit(text2, (tx2, ty2))
pygame.display.update()
while True:
for event in pygame.event.get():
if event.typt == pygame.QUIT:
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
mx, my = event.pos
if (bx <= mx <= bx + bw) and (by <= my <= by + bh):
pygame.draw.rect(screen, (200, 200, 200),(bx, by, bw, bh))
screen.blit(text1, (tx1, ty1))
pygame.display.update()
print('确定按钮被点击')
elif (bx <= mx <= bx + bw) and (by + 100 <= my <= by + 100 + bh):
pygame.draw.rect(screen, (200, 200, 200), (bx, by + 100, bw, bh))
screen.blit(text2, (tx2, ty2))
print('取消按钮被点击')
if event.type == pygame.MOUSEBUTTONUP:
if (bx <= mx <= bx + bw) and (by <= my <= by + bh):
pygame.draw.rect(screen, (255, 0, 0),(bx, by, bw, bh))
screen.blit(text1, (tx1, ty1))
pygame.display.update()
elif (bx <= mx <= bx + bw) and (by + 100 <= my <= by + 100 + bh):
pygame.draw.rect(screen, (0, 255, 0), (bx, by + 100, bw, bh))
screen.blit(text2, (tx2, ty2))
pygame.display.update()
import pygame
WIN_WIDTH = 400
WIN_HEIGHT = 600
pygame.init()
screen = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
pygame.display.set_caption('角色移动')
sereen.fill((255, 255, 255))
pygame.display.flip()
tank_up = pygame.image.load('files/p1tankU.gif')
tank_down = pygame.image.load('files/p1tankD.gif')
tank_left = pygame.image.load('files/p1tankL.gif')
tank_right = pygame.image.load('files/p1tankR.gif')
tank = tank_up
tank_x, tank_y = 200, 450
screen.blit.(tank, (tank_x, tank_y))
pygame.display.update()
is_move = False
x_speed = 0
y_speed = 0
while True:
if is_move == Ture:
screen.fill((255, 255, 255))
tank_x += x_speed
tank_y += y_speed
screen.blit(tank, (tank_x, tank_y))
pygame.display.update()
for event in pygame.event.get():
if event.typt == pygame.QUIT:
exit()
if event.type == pygame.KEYDOWN:
if chr(event.key) == 'w':
is_move = True
x_speed = 0
y_speed = -2
tank = tank_up
if chr(event.key) == 'a':
is_move = True
x_speed = -2
y_speed = 0
tank = tank_left
if chr(event.key) == 's':
is_move = True
x_speed = 0
y_speed = 2
tank = tank_down
if chr(event.key) == 'd':
is_move = True
x_speed = 2
y_speed = 0
tank = tank_right
if event.type == pygame.KEYUP:
is_move = False