Views
No views yet
Generate the model for more details.1/llama-cli -hf Intel/Qwen3-Coder-480B-A35B-Instruct-gguf-q2ks-mixed-AutoRound:q2_k_s --conversation
21> code a flappy bird in python
2Here's a complete Flappy Bird implementation using Pygame:
3
4```python
5import pygame
6import random
7import sys
8
9# Initialize pygame
10pygame.init()
11
12# Game constants
13WIDTH, HEIGHT = 400, 600
14FPS = 60
15GRAVITY = 0.5
16JUMP = -10
17PIPE_SPEED = 3
18PIPE_GAP = 150
19PIPE_FREQUENCY = 1500 # milliseconds
20
21# Colors
22WHITE = (255, 255, 255)
23BLACK = (0, 0, 0)
24GREEN = (0, 255, 0)
25BLUE = (0, 100, 255)
26
27# Create game window
28screen = pygame.display.set_mode((WIDTH, HEIGHT))
29pygame.display.set_caption("Flappy Bird")
30clock = pygame.time.Clock()
31
32# Font
33font = pygame.font.SysFont(None, 36)
34
35class Bird:
36 def __init__(self):
37 self.x = 50
38 self.y = HEIGHT // 2
39 self.velocity = 0
40 self.radius = 15
41
42 def jump(self):
43 self.velocity = JUMP
44
45 def update(self):
46 self.velocity += GRAVITY
47 self.y += self.velocity
48
49 def draw(self):
50 pygame.draw.circle(screen, BLUE, (self.x, int(self.y)), self.radius)
51
52 def get_rect(self):
53 return pygame.Rect(
54 self.x - self.radius,
55 self.y - self.radius,
56 self.radius * 2,
57 self.radius * 2
58 )
59
60class Pipe:
61 def __init__(self):
62 self.x = WIDTH
63 self.height = random.randint(50, HEIGHT - PIPE_GAP - 50)
64 self.width = 50
65 self.passed = False
66
67 def update(self):
68 self.x -= PIPE_SPEED
69
70 def draw(self):
71 # Top pipe
72 pygame.draw.rect(screen, GREEN, (self.x, 0, self.width, self.height))
73 # Bottom pipe
74 pygame.draw.rect(screen, GREEN, (self.x, self.height + PIPE_GAP, self.width, HEIGHT))
75
76 def collide(self, bird):
77 bird_rect = bird.get_rect()
78 top_pipe = pygame.Rect(self.x, 0, self.width, self.height)
79 bottom_pipe = pygame.Rect(self.x, self.height + PIPE_GAP, self.width, HEIGHT)
80 return bird_rect.colliderect(top_pipe) or bird_rect.colliderect(bottom_pipe)
81
82 def off_screen(self):
83 return self.x < -self.width
84
85def draw_ground():
86 pygame.draw.line(screen, BLACK, (0, HEIGHT - 50), (WIDTH, HEIGHT - 50), 5)
87
88def main():
89 bird = Bird()
90 pipes = []
91 score = 0
92 last_pipe = pygame.time.get_ticks()
93 game_over = False
94
95 while True:
96 # Event handling
97 for event in pygame.event.get():
98 if event.type == pygame.QUIT:
99 pygame.quit()
100 sys.exit()
101 if event.type == pygame.KEYDOWN:
102 if event.key == pygame.K_SPACE:
103 if game_over:
104 # Restart game
105 bird = Bird()
106 pipes = []
107 score = 0
108 last_pipe = pygame.time.get_ticks()
109 game_over = False
110 else:
111 bird.jump()
112
113 if not game_over:
114 # Update bird
115 bird.update()
116
117 # Generate new pipes
118 current_time = pygame.time.get_ticks()
119 if current_time - last_pipe > PIPE_FREQUENCY:
120 pipes.append(Pipe())
121 last_pipe = current_time
122
123
124 # Update pipes and check for score
125 for pipe in pipes:
126 pipe.update()
127 if not pipe.passed and pipe.x < bird.x:
128 pipe.passed = True
129 score += 1
130
131 # Remove off-screen pipes
132 pipes = [pipe for pipe in pipes if not pipe.off_screen()]
133
134 # Check collisions
135 if bird.y > HEIGHT - 50 - bird.radius or bird.y < bird.radius:
136 game_over = True
137 for pipe in pipes:
138 if pipe.collide(bird):
139 game_over = True
140
141 # Draw everything
142 screen.fill(WHITE)
143 for pipe in pipes:
144 pipe.draw()
145 bird.draw()
146 draw_ground()
147
148 # Draw score
149 score_text = font.render(f"Score: {score}", True, BLACK)
150 screen.blit(score_text, (10, 10))
151
152 # Draw game over message
153 if game_over:
154 game_over_text = font.render("Game Over! Press SPACE to restart", True, BLACK)
155 screen.blit(game_over_text, (WIDTH//2 - game_over_text.get_width()//2, HEIGHT//2))
156
157 pygame.display.flip()
158 clock.tick(FPS)
159
160if __name__ == "__main__":
161 main()
162```
163
164### Features:
1651. **Bird Physics**:
166 - Gravity pulls the bird down
167 - Space bar makes the bird jump upward
168
1692. **Pipe System**:
170 - Pipes generate randomly with gaps
171 - Pipes move from right to left
172 - Collision detection with bird
173
1743. **Scoring**:
175 - Score increases when bird passes through pipes
176 - Displayed at top-left of screen
177
1784. **Game States**:
179 - Active gameplay
180 - Game over screen with restart option
181
182### Controls:
183- **SPACE**: Jump (during gameplay) / Restart (after game over)
184
185### Requirements:
186- Python 3.x
187- Pygame (`pip install pygame`)
188
189### Game Mechanics:
1901. The bird automatically falls due to gravity
1912. Players must press SPACE to make the bird jump
1923. Avoid hitting pipes or the ground
1934. Each pipe passed increases the score by 1
1945. When game over occurs, press SPACE to restart
195
196This implementation includes all core Flappy Bird mechanics in a single file with minimal dependencies. The game features smooth animations, collision detection, and score tracking.
197
1981import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3from auto_round import AutoRound
4
5model_name = "Qwen/Qwen3-Coder-480B-A35B-Instruct"
6
7model = AutoModelForCausalLM.from_pretrained(model_name,
8 device_map="cpu", torch_dtype="auto")
9tokenizer = AutoTokenizer.from_pretrained(model_name)
10layer_config = {}
11for n, m in model.named_modules():
12 if n == "lm_head" or isinstance(m, torch.nn.Embedding):
13 layer_config[n] = {"bits": 8}
14 elif isinstance(m, torch.nn.Linear) and (not "expert" in n or "shared_experts" in n) and n != "lm_head":
15 layer_config[n] = {"bits": 4}
16
17autoround = AutoRound(model, tokenizer, iters=0, layer_config=layer_config, nsamples=512, dataset="github-code-clean")
18autoround.quantize_and_save("./Qwen3-Coder-480B-A35B-Instruct-q2ks", format="gguf:q2_k_s")
19