Coding a game /u/ArtistName Python Education
Coding a game /u/ArtistName Python Education
Me and my friend are making a little space shooter games for our end of semester school project. We are using codeHS. Although, our game ends after we score once. It just freezes.
This is our code: import tkinter as tk import random
Constants
WIDTH = 600 HEIGHT = 800 PLAYER_WIDTH = 50 PLAYER_HEIGHT = 50 ENEMY_WIDTH = 50 ENEMY_HEIGHT = 50 BULLET_WIDTH = 5 BULLET_HEIGHT = 10 PLAYER_SPEED = 20 BULLET_SPEED = 15 ENEMY_SPEED = 5 POWERUP_WIDTH = 20 POWERUP_HEIGHT = 20 POWERUP_SPEED = 3
class ShootingGame: def init(self, root): self.root = root self.root.resizable(False, False) # Added this line self.canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg=”black”) self.canvas.pack() self.powerups = [] self.power_active = False self.power_timer = 0 self.power_type = None
# Player setup with adjusted position self.player = self.canvas.create_rectangle( WIDTH // 2 - PLAYER_WIDTH // 2, # X start HEIGHT - PLAYER_HEIGHT - 20, # Y start - moved higher up WIDTH // 2 + PLAYER_WIDTH // 2, # X end HEIGHT - 20, # Y end fill="yellow", outline="white", width=3 ) # Debug print for player coordinates player_coords = self.canvas.coords(self.player) print(f"Player coordinates: {player_coords}") # List for bullets and enemies self.bullets = [] self.enemies = [] # Score self.score = 0 self.score_text = self.canvas.create_text( 10, 10, anchor="nw", text=f"Score: {self.score}", fill="white", font=("Arial", 16) ) # Controls self.root.bind("<Left>", self.move_left) self.root.bind("<Right>", self.move_right) self.root.bind("<space>", self.shoot_bullet) # Start game loop self.running = True self.update_game() def move_left(self, event): if self.canvas.coords(self.player)[0] > 0: self.canvas.move(self.player, -PLAYER_SPEED, 0) def move_right(self, event): if self.canvas.coords(self.player)[2] < WIDTH: self.canvas.move(self.player, PLAYER_SPEED, 0) def shoot_bullet(self, event): x1, y1, x2, y2 = self.canvas.coords(self.player) bullet = self.canvas.create_rectangle( (x1 + x2) // 2 - BULLET_WIDTH // 2, y1 - BULLET_HEIGHT, (x1 + x2) // 2 + BULLET_WIDTH // 2, y1, fill="white" ) self.bullets.append(bullet) def spawn_enemy(self): x = random.randint(0, WIDTH - ENEMY_WIDTH) enemy = self.canvas.create_rectangle( x, 0, x + ENEMY_WIDTH, ENEMY_HEIGHT, fill="red" ) self.enemies.append(enemy) def update_bullets(self): for bullet in self.bullets[:]: self.canvas.move(bullet, 0, -BULLET_SPEED) if self.canvas.coords(bullet)[1] < 0: self.canvas.delete(bullet) self.bullets.remove(bullet) def update_enemies(self): for enemy in self.enemies[:]: self.canvas.move(enemy, 0, ENEMY_SPEED) if self.canvas.coords(enemy)[3] >= HEIGHT: self.running = False self.canvas.create_text( WIDTH // 2, HEIGHT // 2, text="GAME OVER", fill="white", font=("Arial", 40) ) for bullet in self.bullets[:]: if self.check_collision(enemy, bullet): self.canvas.delete(enemy) self.canvas.delete(bullet) self.enemies.remove(enemy) self.bullets.remove(bullet) self.score += 1 self.canvas.itemconfig(self.score_text, text=f"Score: {self.score}") def check_collision(self, enemy, bullet): e_coords = self.canvas.coords(enemy) b_coords = self.canvas.coords(bullet) return not ( e_coords[2] < b_coords[0] or e_coords[0] > b_coords[2] or e_coords[3] < b_coords[1] or e_coords[1] > b_coords[3] ) def update_game(self): if not self.running: return if random.randint(1, 50) == 1: self.spawn_enemy() self.update_bullets() self.update_enemies() self.root.after(50, self.update_game) # Different power-up types and their colors self.powerup_types = { "double_shot": "blue", "speed_boost": "green", "shield": "purple" } def spawn_powerup(self): x = random.randint(0, WIDTH - POWERUP_WIDTH) power_type = random.choice(list(self.powerup_types.keys())) color = self.powerup_types[power_type] powerup = self.canvas.create_oval( x, 0, x + POWERUP_WIDTH, POWERUP_HEIGHT, fill=color, outline="white" ) self.powerups.append({"item": powerup, "type": power_type}) def update_powerups(self): for powerup in self.powerups[:]: self.canvas.move(powerup["item"], 0, POWERUP_SPEED) # Check if powerup is off screen if self.canvas.coords(powerup["item"])[3] >= HEIGHT: self.canvas.delete(powerup["item"]) self.powerups.remove(powerup) continue # Check collision with player if self.check_collision_with_player(powerup["item"]): self.activate_powerup(powerup["type"]) self.canvas.delete(powerup["item"]) self.powerups.remove(powerup) def check_collision_with_player(self, item): p_coords = self.canvas.coords(self.player) i_coords = self.canvas.coords(item) return not ( i_coords[2] < p_coords[0] or i_coords[0] > p_coords[2] or i_coords[3] < p_coords[1] or i_coords[1] > p_coords[3] ) def activate_powerup(self, power_type): self.power_active = True self.power_type = power_type self.power_timer = 100 # Power-up lasts for 100 game ticks if power_type == "speed_boost": global PLAYER_SPEED PLAYER_SPEED *= 2 def update_power_status(self): if self.power_active: self.power_timer -= 1 if self.power_timer <= 0: self.deactivate_powerup()
if name == “main“: root = tk.Tk() root.title(“Shooting Game”) game = ShootingGame(root) root.mainloop()
Any tips?
submitted by /u/ArtistName
[link] [comments]
r/learnpython Me and my friend are making a little space shooter games for our end of semester school project. We are using codeHS. Although, our game ends after we score once. It just freezes. This is our code: import tkinter as tk import random Constants WIDTH = 600 HEIGHT = 800 PLAYER_WIDTH = 50 PLAYER_HEIGHT = 50 ENEMY_WIDTH = 50 ENEMY_HEIGHT = 50 BULLET_WIDTH = 5 BULLET_HEIGHT = 10 PLAYER_SPEED = 20 BULLET_SPEED = 15 ENEMY_SPEED = 5 POWERUP_WIDTH = 20 POWERUP_HEIGHT = 20 POWERUP_SPEED = 3 class ShootingGame: def init(self, root): self.root = root self.root.resizable(False, False) # Added this line self.canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg=”black”) self.canvas.pack() self.powerups = [] self.power_active = False self.power_timer = 0 self.power_type = None # Player setup with adjusted position self.player = self.canvas.create_rectangle( WIDTH // 2 – PLAYER_WIDTH // 2, # X start HEIGHT – PLAYER_HEIGHT – 20, # Y start – moved higher up WIDTH // 2 + PLAYER_WIDTH // 2, # X end HEIGHT – 20, # Y end fill=”yellow”, outline=”white”, width=3 ) # Debug print for player coordinates player_coords = self.canvas.coords(self.player) print(f”Player coordinates: {player_coords}”) # List for bullets and enemies self.bullets = [] self.enemies = [] # Score self.score = 0 self.score_text = self.canvas.create_text( 10, 10, anchor=”nw”, text=f”Score: {self.score}”, fill=”white”, font=(“Arial”, 16) ) # Controls self.root.bind(“<Left>”, self.move_left) self.root.bind(“<Right>”, self.move_right) self.root.bind(“<space>”, self.shoot_bullet) # Start game loop self.running = True self.update_game() def move_left(self, event): if self.canvas.coords(self.player)[0] > 0: self.canvas.move(self.player, -PLAYER_SPEED, 0) def move_right(self, event): if self.canvas.coords(self.player)[2] < WIDTH: self.canvas.move(self.player, PLAYER_SPEED, 0) def shoot_bullet(self, event): x1, y1, x2, y2 = self.canvas.coords(self.player) bullet = self.canvas.create_rectangle( (x1 + x2) // 2 – BULLET_WIDTH // 2, y1 – BULLET_HEIGHT, (x1 + x2) // 2 + BULLET_WIDTH // 2, y1, fill=”white” ) self.bullets.append(bullet) def spawn_enemy(self): x = random.randint(0, WIDTH – ENEMY_WIDTH) enemy = self.canvas.create_rectangle( x, 0, x + ENEMY_WIDTH, ENEMY_HEIGHT, fill=”red” ) self.enemies.append(enemy) def update_bullets(self): for bullet in self.bullets[:]: self.canvas.move(bullet, 0, -BULLET_SPEED) if self.canvas.coords(bullet)[1] < 0: self.canvas.delete(bullet) self.bullets.remove(bullet) def update_enemies(self): for enemy in self.enemies[:]: self.canvas.move(enemy, 0, ENEMY_SPEED) if self.canvas.coords(enemy)[3] >= HEIGHT: self.running = False self.canvas.create_text( WIDTH // 2, HEIGHT // 2, text=”GAME OVER”, fill=”white”, font=(“Arial”, 40) ) for bullet in self.bullets[:]: if self.check_collision(enemy, bullet): self.canvas.delete(enemy) self.canvas.delete(bullet) self.enemies.remove(enemy) self.bullets.remove(bullet) self.score += 1 self.canvas.itemconfig(self.score_text, text=f”Score: {self.score}”) def check_collision(self, enemy, bullet): e_coords = self.canvas.coords(enemy) b_coords = self.canvas.coords(bullet) return not ( e_coords[2] < b_coords[0] or e_coords[0] > b_coords[2] or e_coords[3] < b_coords[1] or e_coords[1] > b_coords[3] ) def update_game(self): if not self.running: return if random.randint(1, 50) == 1: self.spawn_enemy() self.update_bullets() self.update_enemies() self.root.after(50, self.update_game) # Different power-up types and their colors self.powerup_types = { “double_shot”: “blue”, “speed_boost”: “green”, “shield”: “purple” } def spawn_powerup(self): x = random.randint(0, WIDTH – POWERUP_WIDTH) power_type = random.choice(list(self.powerup_types.keys())) color = self.powerup_types[power_type] powerup = self.canvas.create_oval( x, 0, x + POWERUP_WIDTH, POWERUP_HEIGHT, fill=color, outline=”white” ) self.powerups.append({“item”: powerup, “type”: power_type}) def update_powerups(self): for powerup in self.powerups[:]: self.canvas.move(powerup[“item”], 0, POWERUP_SPEED) # Check if powerup is off screen if self.canvas.coords(powerup[“item”])[3] >= HEIGHT: self.canvas.delete(powerup[“item”]) self.powerups.remove(powerup) continue # Check collision with player if self.check_collision_with_player(powerup[“item”]): self.activate_powerup(powerup[“type”]) self.canvas.delete(powerup[“item”]) self.powerups.remove(powerup) def check_collision_with_player(self, item): p_coords = self.canvas.coords(self.player) i_coords = self.canvas.coords(item) return not ( i_coords[2] < p_coords[0] or i_coords[0] > p_coords[2] or i_coords[3] < p_coords[1] or i_coords[1] > p_coords[3] ) def activate_powerup(self, power_type): self.power_active = True self.power_type = power_type self.power_timer = 100 # Power-up lasts for 100 game ticks if power_type == “speed_boost”: global PLAYER_SPEED PLAYER_SPEED *= 2 def update_power_status(self): if self.power_active: self.power_timer -= 1 if self.power_timer <= 0: self.deactivate_powerup() if name == “main”: root = tk.Tk() root.title(“Shooting Game”) game = ShootingGame(root) root.mainloop() Any tips? submitted by /u/ArtistName [link] [comments]
Me and my friend are making a little space shooter games for our end of semester school project. We are using codeHS. Although, our game ends after we score once. It just freezes.
This is our code: import tkinter as tk import random
Constants
WIDTH = 600 HEIGHT = 800 PLAYER_WIDTH = 50 PLAYER_HEIGHT = 50 ENEMY_WIDTH = 50 ENEMY_HEIGHT = 50 BULLET_WIDTH = 5 BULLET_HEIGHT = 10 PLAYER_SPEED = 20 BULLET_SPEED = 15 ENEMY_SPEED = 5 POWERUP_WIDTH = 20 POWERUP_HEIGHT = 20 POWERUP_SPEED = 3
class ShootingGame: def init(self, root): self.root = root self.root.resizable(False, False) # Added this line self.canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg=”black”) self.canvas.pack() self.powerups = [] self.power_active = False self.power_timer = 0 self.power_type = None
# Player setup with adjusted position self.player = self.canvas.create_rectangle( WIDTH // 2 - PLAYER_WIDTH // 2, # X start HEIGHT - PLAYER_HEIGHT - 20, # Y start - moved higher up WIDTH // 2 + PLAYER_WIDTH // 2, # X end HEIGHT - 20, # Y end fill="yellow", outline="white", width=3 ) # Debug print for player coordinates player_coords = self.canvas.coords(self.player) print(f"Player coordinates: {player_coords}") # List for bullets and enemies self.bullets = [] self.enemies = [] # Score self.score = 0 self.score_text = self.canvas.create_text( 10, 10, anchor="nw", text=f"Score: {self.score}", fill="white", font=("Arial", 16) ) # Controls self.root.bind("<Left>", self.move_left) self.root.bind("<Right>", self.move_right) self.root.bind("<space>", self.shoot_bullet) # Start game loop self.running = True self.update_game() def move_left(self, event): if self.canvas.coords(self.player)[0] > 0: self.canvas.move(self.player, -PLAYER_SPEED, 0) def move_right(self, event): if self.canvas.coords(self.player)[2] < WIDTH: self.canvas.move(self.player, PLAYER_SPEED, 0) def shoot_bullet(self, event): x1, y1, x2, y2 = self.canvas.coords(self.player) bullet = self.canvas.create_rectangle( (x1 + x2) // 2 - BULLET_WIDTH // 2, y1 - BULLET_HEIGHT, (x1 + x2) // 2 + BULLET_WIDTH // 2, y1, fill="white" ) self.bullets.append(bullet) def spawn_enemy(self): x = random.randint(0, WIDTH - ENEMY_WIDTH) enemy = self.canvas.create_rectangle( x, 0, x + ENEMY_WIDTH, ENEMY_HEIGHT, fill="red" ) self.enemies.append(enemy) def update_bullets(self): for bullet in self.bullets[:]: self.canvas.move(bullet, 0, -BULLET_SPEED) if self.canvas.coords(bullet)[1] < 0: self.canvas.delete(bullet) self.bullets.remove(bullet) def update_enemies(self): for enemy in self.enemies[:]: self.canvas.move(enemy, 0, ENEMY_SPEED) if self.canvas.coords(enemy)[3] >= HEIGHT: self.running = False self.canvas.create_text( WIDTH // 2, HEIGHT // 2, text="GAME OVER", fill="white", font=("Arial", 40) ) for bullet in self.bullets[:]: if self.check_collision(enemy, bullet): self.canvas.delete(enemy) self.canvas.delete(bullet) self.enemies.remove(enemy) self.bullets.remove(bullet) self.score += 1 self.canvas.itemconfig(self.score_text, text=f"Score: {self.score}") def check_collision(self, enemy, bullet): e_coords = self.canvas.coords(enemy) b_coords = self.canvas.coords(bullet) return not ( e_coords[2] < b_coords[0] or e_coords[0] > b_coords[2] or e_coords[3] < b_coords[1] or e_coords[1] > b_coords[3] ) def update_game(self): if not self.running: return if random.randint(1, 50) == 1: self.spawn_enemy() self.update_bullets() self.update_enemies() self.root.after(50, self.update_game) # Different power-up types and their colors self.powerup_types = { "double_shot": "blue", "speed_boost": "green", "shield": "purple" } def spawn_powerup(self): x = random.randint(0, WIDTH - POWERUP_WIDTH) power_type = random.choice(list(self.powerup_types.keys())) color = self.powerup_types[power_type] powerup = self.canvas.create_oval( x, 0, x + POWERUP_WIDTH, POWERUP_HEIGHT, fill=color, outline="white" ) self.powerups.append({"item": powerup, "type": power_type}) def update_powerups(self): for powerup in self.powerups[:]: self.canvas.move(powerup["item"], 0, POWERUP_SPEED) # Check if powerup is off screen if self.canvas.coords(powerup["item"])[3] >= HEIGHT: self.canvas.delete(powerup["item"]) self.powerups.remove(powerup) continue # Check collision with player if self.check_collision_with_player(powerup["item"]): self.activate_powerup(powerup["type"]) self.canvas.delete(powerup["item"]) self.powerups.remove(powerup) def check_collision_with_player(self, item): p_coords = self.canvas.coords(self.player) i_coords = self.canvas.coords(item) return not ( i_coords[2] < p_coords[0] or i_coords[0] > p_coords[2] or i_coords[3] < p_coords[1] or i_coords[1] > p_coords[3] ) def activate_powerup(self, power_type): self.power_active = True self.power_type = power_type self.power_timer = 100 # Power-up lasts for 100 game ticks if power_type == "speed_boost": global PLAYER_SPEED PLAYER_SPEED *= 2 def update_power_status(self): if self.power_active: self.power_timer -= 1 if self.power_timer <= 0: self.deactivate_powerup()
if name == “main“: root = tk.Tk() root.title(“Shooting Game”) game = ShootingGame(root) root.mainloop()
Any tips?
submitted by /u/ArtistName
[link] [comments] Me and my friend are making a little space shooter games for our end of semester school project. We are using codeHS. Although, our game ends after we score once. It just freezes. This is our code: import tkinter as tk import random Constants WIDTH = 600 HEIGHT = 800 PLAYER_WIDTH = 50 PLAYER_HEIGHT = 50 ENEMY_WIDTH = 50 ENEMY_HEIGHT = 50 BULLET_WIDTH = 5 BULLET_HEIGHT = 10 PLAYER_SPEED = 20 BULLET_SPEED = 15 ENEMY_SPEED = 5 POWERUP_WIDTH = 20 POWERUP_HEIGHT = 20 POWERUP_SPEED = 3 class ShootingGame: def init(self, root): self.root = root self.root.resizable(False, False) # Added this line self.canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg=”black”) self.canvas.pack() self.powerups = [] self.power_active = False self.power_timer = 0 self.power_type = None # Player setup with adjusted position self.player = self.canvas.create_rectangle( WIDTH // 2 – PLAYER_WIDTH // 2, # X start HEIGHT – PLAYER_HEIGHT – 20, # Y start – moved higher up WIDTH // 2 + PLAYER_WIDTH // 2, # X end HEIGHT – 20, # Y end fill=”yellow”, outline=”white”, width=3 ) # Debug print for player coordinates player_coords = self.canvas.coords(self.player) print(f”Player coordinates: {player_coords}”) # List for bullets and enemies self.bullets = [] self.enemies = [] # Score self.score = 0 self.score_text = self.canvas.create_text( 10, 10, anchor=”nw”, text=f”Score: {self.score}”, fill=”white”, font=(“Arial”, 16) ) # Controls self.root.bind(“<Left>”, self.move_left) self.root.bind(“<Right>”, self.move_right) self.root.bind(“<space>”, self.shoot_bullet) # Start game loop self.running = True self.update_game() def move_left(self, event): if self.canvas.coords(self.player)[0] > 0: self.canvas.move(self.player, -PLAYER_SPEED, 0) def move_right(self, event): if self.canvas.coords(self.player)[2] < WIDTH: self.canvas.move(self.player, PLAYER_SPEED, 0) def shoot_bullet(self, event): x1, y1, x2, y2 = self.canvas.coords(self.player) bullet = self.canvas.create_rectangle( (x1 + x2) // 2 – BULLET_WIDTH // 2, y1 – BULLET_HEIGHT, (x1 + x2) // 2 + BULLET_WIDTH // 2, y1, fill=”white” ) self.bullets.append(bullet) def spawn_enemy(self): x = random.randint(0, WIDTH – ENEMY_WIDTH) enemy = self.canvas.create_rectangle( x, 0, x + ENEMY_WIDTH, ENEMY_HEIGHT, fill=”red” ) self.enemies.append(enemy) def update_bullets(self): for bullet in self.bullets[:]: self.canvas.move(bullet, 0, -BULLET_SPEED) if self.canvas.coords(bullet)[1] < 0: self.canvas.delete(bullet) self.bullets.remove(bullet) def update_enemies(self): for enemy in self.enemies[:]: self.canvas.move(enemy, 0, ENEMY_SPEED) if self.canvas.coords(enemy)[3] >= HEIGHT: self.running = False self.canvas.create_text( WIDTH // 2, HEIGHT // 2, text=”GAME OVER”, fill=”white”, font=(“Arial”, 40) ) for bullet in self.bullets[:]: if self.check_collision(enemy, bullet): self.canvas.delete(enemy) self.canvas.delete(bullet) self.enemies.remove(enemy) self.bullets.remove(bullet) self.score += 1 self.canvas.itemconfig(self.score_text, text=f”Score: {self.score}”) def check_collision(self, enemy, bullet): e_coords = self.canvas.coords(enemy) b_coords = self.canvas.coords(bullet) return not ( e_coords[2] < b_coords[0] or e_coords[0] > b_coords[2] or e_coords[3] < b_coords[1] or e_coords[1] > b_coords[3] ) def update_game(self): if not self.running: return if random.randint(1, 50) == 1: self.spawn_enemy() self.update_bullets() self.update_enemies() self.root.after(50, self.update_game) # Different power-up types and their colors self.powerup_types = { “double_shot”: “blue”, “speed_boost”: “green”, “shield”: “purple” } def spawn_powerup(self): x = random.randint(0, WIDTH – POWERUP_WIDTH) power_type = random.choice(list(self.powerup_types.keys())) color = self.powerup_types[power_type] powerup = self.canvas.create_oval( x, 0, x + POWERUP_WIDTH, POWERUP_HEIGHT, fill=color, outline=”white” ) self.powerups.append({“item”: powerup, “type”: power_type}) def update_powerups(self): for powerup in self.powerups[:]: self.canvas.move(powerup[“item”], 0, POWERUP_SPEED) # Check if powerup is off screen if self.canvas.coords(powerup[“item”])[3] >= HEIGHT: self.canvas.delete(powerup[“item”]) self.powerups.remove(powerup) continue # Check collision with player if self.check_collision_with_player(powerup[“item”]): self.activate_powerup(powerup[“type”]) self.canvas.delete(powerup[“item”]) self.powerups.remove(powerup) def check_collision_with_player(self, item): p_coords = self.canvas.coords(self.player) i_coords = self.canvas.coords(item) return not ( i_coords[2] < p_coords[0] or i_coords[0] > p_coords[2] or i_coords[3] < p_coords[1] or i_coords[1] > p_coords[3] ) def activate_powerup(self, power_type): self.power_active = True self.power_type = power_type self.power_timer = 100 # Power-up lasts for 100 game ticks if power_type == “speed_boost”: global PLAYER_SPEED PLAYER_SPEED *= 2 def update_power_status(self): if self.power_active: self.power_timer -= 1 if self.power_timer <= 0: self.deactivate_powerup() if name == “main”: root = tk.Tk() root.title(“Shooting Game”) game = ShootingGame(root) root.mainloop() Any tips? submitted by /u/ArtistName [link] [comments]