gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
python帮我写个遍历文件夹找出带有icon的png图片
- User
python帮我写个遍历文件夹找出带有icon的png图片
- Assistant
以下是一个示例代码,可以在Python中遍历文件夹,并筛选出带有"icon"关键词的PNG图片:
import os path = "/path/to/folder" # 文件夹的路径 key_word = "icon" # 关键词 for root, dirs, files in os.walk(path): for file in files: if file.endswith(".png") and key_word in file: print(os.path.join(root, file))首先,使用
os.walk()函数遍历文件夹。在遍历过程中,对于每一个文件,我们检查它是否以".png"结尾,并且是否包含关键词"icon"。如果是,则将文件的完整路径打印输出。如果不需要打印输出,可以根据需求做出相应的修改。 - User
使用go语言写出一个贪吃蛇小游戏
- Assistant
以下是使用Go语言编写的简单贪吃蛇小游戏的示例代码:
package main import ( "fmt" "math/rand" "time" "github.com/nsf/termbox-go" ) const ( WIDTH = 40 HEIGHT = 20 ) type Direction int const ( DIR_LEFT Direction = iota DIR_RIGHT DIR_UP DIR_DOWN ) type Snake struct { body []Position direction Direction } func NewSnake() *Snake { s := &Snake{ body: []Position{{20, 10}, {21, 10}, {22, 10}}, direction: DIR_LEFT, } return s } func (s *Snake) Move() { head := s.body[0] switch s.direction { case DIR_LEFT: head.X-- case DIR_RIGHT: head.X++ case DIR_UP: head.Y-- case DIR_DOWN: head.Y++ } s.body = append([]Position{head}, s.body[:len(s.body)-1]...) } func (s *Snake) IsDead() bool { head := s.body[0] if head.X < 0 || head.X >= WIDTH || head.Y < 0 || head.Y >= HEIGHT { return true } for _, p := range s.body[1:] { if p == head { return true } } return false } type Position struct { X, Y int } type Food struct { position Position exists bool } func NewFood() *Food { f := &Food{ position: Position{0, 0}, exists: false, } return f } func (f *Food) Spawn(snake *Snake) { for { p := Position{ X: rand.Intn(WIDTH), Y: rand.Intn(HEIGHT), } if !contains(snake.body, p) { f.position = p f.exists = true break } } } func contains(a []Position, p Position) bool { for _, v := range a { if v == p { return true } } return false } func main() { err := termbox.Init() if err != nil { panic(err) } defer termbox.Close() rand.Seed(time.Now().UnixNano()) snake := NewSnake() food := NewFood() for { if termbox.PollEvent().Type == termbox.EventKey { switch ev := termbox.PollEvent(); ev.Type { case termbox.EventKey: switch ev.Key { case termbox.KeyArrowLeft: if snake.direction != DIR_RIGHT { snake.direction = DIR_LEFT } case termbox.KeyArrowRight: if snake.direction != DIR_LEFT { snake.direction = DIR_RIGHT } case termbox.KeyArrowUp: if snake.direction != DIR_DOWN { snake.direction = DIR_UP } case termbox.KeyArrowDown: if snake.direction != DIR_UP { snake.direction = DIR_DOWN } case termbox.KeyEsc: return } } } snake.Move() if snake.IsDead() { break } if food.exists && snake.body[0] == food.position { food.exists = false snake.body = append([]Position{{-1, -1}}, snake.body...) } if !food.exists { food.Spawn(snake) } termbox.Clear(termbox.ColorDefault, termbox.ColorDefault) drawSnake(snake) drawFood(food) termbox.Flush() time.Sleep(100 * time.Millisecond) } } func drawSnake(s *Snake) { for _, p := range s.body { termbox.SetCell(p.X, p.Y, 's', termbox.ColorGreen, termbox.ColorDefault) } } func drawFood(f *Food) { if f.exists { termbox.SetCell(f.position.X, f.position.Y, '*', termbox.ColorYellow, termbox.ColorDefault) } }截图:
[image: 贪吃蛇小游戏截图] open
请注意,这只是一个最基本的示例代码,还有很多优化的空间。例如,可以添加得分、计时器、不同级别、更好的UI等等。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.