|

楼主 |
发表于 2020-7-19 10:44:43
|
显示全部楼层
import pygame
import sys
from pygame.locals import *
pygame.init()
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
size = width, height = 800, 800
screen = pygame.display.set_mode(size)
pygame.display.set_caption("小游戏")
clock = pygame.time.Clock()
pos_x = 150 #设置坐标
pos_y = 550
vel_x = 10 # 设置速度变量
vel_y = 10
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
if event.type == KEYDOWN:
if event.key== K_LEFT:
pos_x -= vel_x
if event.key == K_RIGHT:
pos_x += vel_x
if event.key ==K_UP:
pos_y -= vel_y
if event.key ==K_DOWN:
pos_y += vel_y
if pos_x > 650 : #规定大框架
pos_x = 650
if pos_x < 100:
pos_x = 100
if pos_y > 650 :
pos_y = 650
if pos_y < 100:
pos_y = 100
if 200< pos_y <600:
if 150 <pos_x <300: #设置小框架
pos_x = 150
if 300 < pos_x < 600:
pos_x = 600
if 200 < pos_x < 600:
if 150 < pos_y < 300:
pos_y = 150
if 300 < pos_y < 600:
pos_y = 600
screen.fill(WHITE)
pos = pos_x, pos_y, 50, 50 #设置小车
pygame.draw.rect(screen, BLACK, (100, 100, 600, 600),1)
pygame.draw.rect(screen, BLACK, (200, 200, 400, 400),1)
pygame.draw.rect(screen, RED, pos,0)
pygame.display.flip()
clock.tick(100)
|
|