Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tanks battle game #109

Open
wants to merge 69 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
885d815
tanks.py rysowanie mapy i czolgow
rzymski Nov 13, 2024
b32246b
tanks.py simple tank move
rzymski Nov 14, 2024
2b5b832
tanks.py title validation doesn't work
rzymski Nov 14, 2024
aea659c
tanks.py title validation something work
rzymski Nov 14, 2024
4ebd68f
tanks.py title validation something work with tankCentralization
rzymski Nov 14, 2024
e2fc54a
tanks.py title validation work when driving tank
rzymski Nov 14, 2024
6ea0ac6
tanks.py remove not needed function duplicate square
rzymski Nov 14, 2024
bf613d8
tanks.py code refactoring in drawing tank
rzymski Nov 14, 2024
183a5d3
tanks.py better driving tank
rzymski Nov 14, 2024
7a412e2
tanks.py better driving tank code refactoring
rzymski Nov 14, 2024
36f17b6
tanks.py fix bug with tanks stucked in place
rzymski Nov 14, 2024
3dc9df0
tanks.py driving in tank class
rzymski Nov 14, 2024
6bf269d
tanks.py tanks collision
rzymski Nov 14, 2024
ba402ca
tanks.py simple shooting
rzymski Nov 15, 2024
afb7e21
tanks.py simple shooting checking if hitted (need of correction, beca…
rzymski Nov 15, 2024
01c9d30
tanks.py checking if hit by bullet (fixed)
rzymski Nov 15, 2024
b379e48
tanks.py bullet hitting obstacles and mines
rzymski Nov 15, 2024
e8d4870
tanks.py ending game
rzymski Nov 16, 2024
85ed23f
tanks.py explosion when bullet hit tank
rzymski Nov 16, 2024
6a9a4a2
tanks.py tankDestroyed
rzymski Nov 16, 2024
809a051
tanks.py Tile Enum
rzymski Nov 16, 2024
d49b3d0
tanks.py fix bug with disappearing tank
rzymski Nov 17, 2024
c9ac1f1
tanks.py potential to do list
rzymski Nov 20, 2024
6f50ed2
add tanks Hp and end game modal
zeterPL Nov 20, 2024
e1e0f59
Merge branch 'master' of https://github.com/zeterPL/free-python-games
zeterPL Nov 20, 2024
3319090
tanks.py Fixing bugs and refactoring code
rzymski Nov 21, 2024
1571a95
tanks.py Main Game class
rzymski Nov 21, 2024
a25f8d8
tanks.py fix bugs
rzymski Nov 21, 2024
3c66b14
tanks.py hiding mines after some time
rzymski Nov 21, 2024
705f309
tanks.py change time where mines hide or end message display
Nov 21, 2024
3f26562
basic load map from txt file
zeterPL Nov 21, 2024
2e82c38
Merge branch 'master' of https://github.com/zeterPL/free-python-games
zeterPL Nov 21, 2024
c96889e
pause game
zeterPL Nov 21, 2024
628707f
parameterization function DrawModalMessage
zeterPL Nov 21, 2024
a807582
game instruction modal
zeterPL Nov 21, 2024
375b93f
init game with start menu
zeterPL Nov 21, 2024
1f8f5d2
tanks.py refactoring code
rzymski Nov 22, 2024
f55b7c0
tanks.py reading from ini file instead of normal txt
rzymski Nov 22, 2024
e15f77c
tanks.py reading from ini file different tileSize implementation
rzymski Nov 22, 2024
5b50f55
tanks.py reading from ini file tanks started positions
rzymski Nov 22, 2024
a5977ef
tanks.py
rzymski Nov 22, 2024
8ead5a9
tanks.py -reloading
rzymski Nov 22, 2024
6987e30
computer controlled tank
zeterPL Nov 22, 2024
cfc8ac9
Merge pull request #1 from zeterPL/computer-controlled-tank
zeterPL Nov 22, 2024
d65049f
tanks.py change folder name
rzymski Nov 22, 2024
42f972b
tanks.py refactor code
rzymski Nov 22, 2024
fca1adb
tanks.py sound effects
rzymski Nov 23, 2024
0d9ff73
tanks.py wrapping map (wrap in axis x require one below tile to be av…
rzymski Nov 23, 2024
9fc0e5f
tanks.py enemies from file
rzymski Nov 23, 2024
cf5b501
tanks.py fix that enemies don't shot itself (friendly fire still on, …
rzymski Nov 23, 2024
b14fada
tanks.py enemies shouldn't go on mines and collide which each other (…
rzymski Nov 23, 2024
6fa8bfc
opponent's move via A* algorithm
zeterPL Nov 23, 2024
561f521
Merge branch 'master' into computer-controlled-tank
zeterPL Nov 23, 2024
6a7395a
tanks.py clear tanks in drawTank
rzymski Nov 23, 2024
7adf387
fix
zeterPL Nov 23, 2024
a578b6e
tanks.py fix hiding in forest
rzymski Nov 23, 2024
ee5d71f
Merge branch 'master' into computer-controlled-tank
zeterPL Nov 23, 2024
4c18e0d
Merge pull request #2 from zeterPL/computer-controlled-tank
zeterPL Nov 23, 2024
da0e859
fix ai tank movement
zeterPL Nov 23, 2024
8cc372e
Merge pull request #3 from zeterPL/computer-controlled-tank
zeterPL Nov 23, 2024
42c612c
tanks.py AI terminators: Rise of Machine
rzymski Nov 25, 2024
2713db9
tanks.py second tank optional fix
rzymski Nov 25, 2024
ba342f7
tanks clean version
zeterPL Dec 11, 2024
1c05db6
delete txt file
zeterPL Dec 11, 2024
0818655
tanks - add exercises
zeterPL Dec 11, 2024
c6033e5
change exercises
zeterPL Dec 11, 2024
75f210b
update gitignore
zeterPL Dec 11, 2024
5c586f1
fix requirements.txt
zeterPL Dec 11, 2024
249a427
fix comments
zeterPL Dec 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# virutalenv directories
/env*/
/venv*/

# coverage files
.coverage
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
-e .
-e .
143 changes: 143 additions & 0 deletions src/freegames/tanks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@

"""
Tank Battle Game

Exercises:

1. Change the colors of the tanks and bullets.
2. Adjust the frame rate to make the game faster or slower.
3. Modify the speed of the bullets.
4. Add more obstacles or rearrange their positions.
5. Increase or decrease the tank's movement speed.
6. Implement a power-up system (e.g., extra bullets or health).
7. Create a scoring system to track wins.
8. Allow multiple players (up to 4 tanks).
9. Add a second bullet type with different behaviors.
"""

from turtle import *
from freegames import vector
from math import cos, sin, radians

class Tank:
def __init__(self, x, y, color, controls):
self.position = vector(x, y)
self.speed = vector(0, 0)
self.color = color
self.controls = controls
self.direction = 90 # Start direction (up)
self.hp = 3
self.bullets = []

self.body = Turtle()
self.body.shape("square")
self.body.color(color)
self.body.shapesize(1.5, 1)
self.body.penup()
self.body.goto(x, y)

self.gun = Turtle()
self.gun.shape("square")
self.gun.color("black")
self.gun.shapesize(0.5, 0.2)
self.gun.penup()
self.update_gun()

def update_gun(self):
self.gun.goto(self.position.x + 15 * cos(radians(self.direction)),
self.position.y + 15 * sin(radians(self.direction)))
self.gun.setheading(self.direction)

def move(self):
new_pos = self.position + self.speed
if -200 < new_pos.x < 200 and -200 < new_pos.y < 200 and self.valid_position(new_pos):
self.position = new_pos
self.body.goto(self.position.x, self.position.y)
self.update_gun()

def change_speed(self, dx, dy, angle=None):
self.speed.x, self.speed.y = dx, dy
if angle is not None:
self.direction = angle

def shoot(self):
bullet = vector(self.position.x + 15 * cos(radians(self.direction)),
self.position.y + 15 * sin(radians(self.direction)))
direction = vector(cos(radians(self.direction)), sin(radians(self.direction))) * 15 # Increased bullet speed
bullet_turtle = Turtle()
bullet_turtle.shape("circle")
bullet_turtle.color(self.color)
bullet_turtle.shapesize(0.2, 0.2)
bullet_turtle.penup()
bullet_turtle.goto(bullet.x, bullet.y)
self.bullets.append((bullet, direction, bullet_turtle))

def valid_position(self, pos):
for obstacle in obstacles:
if obstacle.distance(pos.x, pos.y) < 20:
return False
return True

def hit(self):
self.hp -= 1
print(f"{self.color} tank HP: {self.hp}")
if self.hp <= 0:
self.body.hideturtle()
self.gun.hideturtle()
return True
return False

screen = Screen()
screen.setup(420, 420)
screen.tracer(False)

# Create map
obstacles = []
for x, y in [(-100, 50), (50, -50), (0, 100)]:
obs = Turtle()
obs.shape("square")
obs.color("gray")
obs.penup()
obs.goto(x, y)
obstacles.append(obs)

tank1 = Tank(-150, 0, 'blue', {'up': 'w', 'down': 's', 'left': 'a', 'right': 'd', 'shoot': 'e'})
tank2 = Tank(150, 0, 'red', {'up': 'Up', 'down': 'Down', 'left': 'Left', 'right': 'Right', 'shoot': 'Return'})

def setup_controls(tank, controls):
onkey(lambda: tank.change_speed(0, 10, 90), controls["up"])
onkey(lambda: tank.change_speed(0, -10, 270), controls["down"])
onkey(lambda: tank.change_speed(-10, 0, 180), controls["left"])
onkey(lambda: tank.change_speed(10, 0, 0), controls["right"])
onkey(tank.shoot, controls["shoot"])

setup_controls(tank1, tank1.controls)
setup_controls(tank2, tank2.controls)

def update():
tank1.move()
tank2.move()

for tank in (tank1, tank2):
new_bullets = []
for bullet, direction, bullet_turtle in tank.bullets:
bullet.move(direction)
if -200 < bullet.x < 200 and -200 < bullet.y < 200:
bullet_turtle.goto(bullet.x, bullet.y)
new_bullets.append((bullet, direction, bullet_turtle))

target = tank2 if tank == tank1 else tank1
if abs(bullet - target.position) < 10 and target.hit():
print(f"{target.color} tank destroyed!")
bullet_turtle.hideturtle()
return
else:
bullet_turtle.hideturtle()
tank.bullets = new_bullets

screen.update()
ontimer(update, 50)

listen()
update()
done()