使用IDLE学习python,input、print、运算符,基本函数、import

上一篇文章讲安装了python,pycharm,发现python附带的轻量级IDLE还是挺好玩的,之前都用的交互模式,也可以新建脚本

一、IDLE使用

点击IDLE-file-new file,就可以新建一个python脚本,写点内容然后ctrl+s保存得到.py文件,双击就可以运行:

//保存,选位置并重命名

//双击name.py文件

 二、知识点

知识点还是要使用才行

1、input、print

name=input("What's your name?\n")    #注释,定义并显示输入变量name
print("Hello "+name+"!")                        #输出方式1,用+连接字符串变量
input(&#

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的贪吃蛇代码实现,你可以在IDLE软件运行它: ```python import pygame import random # 定义颜色 BLACK = (0, 0, 0) WHITE = (255, 255, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) # 设置屏幕大小 SCREEN_WIDTH = 600 SCREEN_HEIGHT = 400 # 设置方格大小 BLOCK_SIZE = 10 class Snake: def __init__(self): self.length = 1 self.positions = [(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)] self.direction = random.choice([UP, DOWN, LEFT, RIGHT]) self.color = GREEN def get_head_position(self): return self.positions[0] def turn(self, point): if self.length > 1 and (point[0] * -1, point[1] * -1) == self.direction: return else: self.direction = point def move(self): cur = self.get_head_position() x, y = self.direction new = ((cur[0] + (x * BLOCK_SIZE)), (cur[1] + (y * BLOCK_SIZE))) if new in self.positions[2:]: self.reset() else: self.positions.insert(0, new) if len(self.positions) > self.length: self.positions.pop() def reset(self): self.length = 1 self.positions = [(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)] self.direction = random.choice([UP, DOWN, LEFT, RIGHT]) def draw(self, surface): for p in self.positions: r = pygame.Rect((p[0], p[1]), (BLOCK_SIZE, BLOCK_SIZE)) pygame.draw.rect(surface, self.color, r) pygame.draw.rect(surface, BLACK, r, 1) def handle_keys(self): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: self.turn(UP) elif event.key == pygame.K_DOWN: self.turn(DOWN) elif event.key == pygame.K_LEFT: self.turn(LEFT) elif event.key == pygame.K_RIGHT: self.turn(RIGHT) class Food: def __init__(self): x = random.randrange(0, SCREEN_WIDTH, BLOCK_SIZE) y = random.randrange(0, SCREEN_HEIGHT, BLOCK_SIZE) self.position = (x, y) self.color = RED def draw(self, surface): r = pygame.Rect((self.position[0], self.position[1]), (BLOCK_SIZE, BLOCK_SIZE)) pygame.draw.rect(surface, self.color, r) pygame.draw.rect(surface, BLACK, r, 1) def draw_grid(surface): for y in range(0, int(SCREEN_HEIGHT / BLOCK_SIZE)): for x in range(0, int(SCREEN_WIDTH / BLOCK_SIZE)): if (x + y) % 2 == 0: r = pygame.Rect((x * BLOCK_SIZE, y * BLOCK_SIZE), (BLOCK_SIZE, BLOCK_SIZE)) pygame.draw.rect(surface, WHITE, r) else: rr = pygame.Rect((x * BLOCK_SIZE, y * BLOCK_SIZE), (BLOCK_SIZE, BLOCK_SIZE)) pygame.draw.rect(surface, BLACK, rr) UP = (0, -1) DOWN = (0, 1) LEFT = (-1, 0) RIGHT = (1, 0) def main(): pygame.init() clock = pygame.time.Clock() screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Snake") snake = Snake() food = Food() running = True while running: clock.tick(10) snake.handle_keys() snake.move() if snake.get_head_position() == food.position: snake.length += 1 food = Food() screen.fill(BLACK) draw_grid(screen) snake.draw(screen) food.draw(screen) pygame.display.update() pygame.quit() if __name__ == "__main__": main() ``` 注意,需要在代码安装 pygame 模块,命令为 `pip install pygame`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值