当前位置: 首页 > 工具软件 > paw-wars > 使用案例 >

space_wars

徐子石
2023-12-01

使用Python实现的太空​​战游戏

#gitignore

##Byte-compiled / optimized / DLL files
pycache/
*.py[cod]
*$py.class

##C extensions
*.so

Distribution / packaging

.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

PyInstaller

Usually these files are written by a python script from a template

before PyInstaller builds the exe, so as to inject date/other infos into it.

*.manifest
*.spec

Installer logs

pip-log.txt
pip-delete-this-directory.txt

Unit test / coverage reports

htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

Translations

*.mo
*.pot

Django stuff:

*.log
local_settings.py
db.sqlite3

Flask stuff:

instance/
.webassets-cache

Scrapy stuff:

.scrapy

Sphinx documentation

docs/_build/

PyBuilder

target/

Jupyter Notebook

.ipynb_checkpoints

pyenv

.python-version

celery beat schedule file

celerybeat-schedule

SageMath parsed files

*.sage.py

Environments

.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

Spyder project settings

.spyderproject
.spyproject

Rope project settings

.ropeproject

mkdocs documentation

/site

mypy

.mypy_cache/

#SpaceWars.py

import os
import random
import pygame
#Gave this program to that dickhead that goes by the name of Nandan Vaidya.
#Anyways dickhead wanted more responsive firing.
#So I guess here goes nothing…
#24th March - Made firing more responsive
#dickhead wants multiple enemies…
#27th March - Added multiple enemies.
#ata bhava la levels and sound effects have ahet. Ek kaam karna aaighalya swataha code shik ani lihi
#30th March - Added sound effects.

#Import the Turtle module
import turtle
turtle.fd(0)
#Set the animations speed to the maximum
turtle.speed(0)
#Change the background color
turtle.bgcolor(“black”)
#Hide the default turtle
turtle.ht()
#This saves memory
turtle.setundobuffer(1)
#This speeds up drawing
turtle.tracer(1)

class Sprite(turtle.Turtle):
def init(self, spriteshape, color, startx, starty):
turtle.Turtle.init(self, shape = spriteshape)
self.speed(0)
self.penup()
self.color(color)
self.fd(0)
self.goto(startx, starty)
self.speed = 1

def move(self):
	self.fd(self.speed)
	
	#Boundary detection
	if self.xcor() > 290:
		self.setx(290)
		self.rt(60)
	
	if self.xcor() < -290:
		self.setx(-290)
		self.rt(60)
	
	if self.ycor() > 290:
		self.sety(290)
		self.rt(60)
	
	if self.ycor() < -290:
		self.sety(-290)
		self.rt(60)
		
def is_collision(self, other):
	if (self.xcor() >= (other.xcor() - 20)) and \
	(self.xcor() <= (other.xcor() + 20)) and \
	(self.ycor() >= (other.ycor() - 20)) and \
	(self.ycor() <= (other.ycor() + 20)):
		return True
	else:
		return False

class Player(Sprite):
def init(self, spriteshape, color, startx, starty):
Sprite.init(self, spriteshape, color, startx, starty)
self.speed = 4
self.lives = 3

def turn_left(self):
	self.lt(45)
	
def turn_right(self):
	self.rt(45)

def accelerate(self):
	self.speed += 1
	
def decelerate(self):
	if self.speed > 4:
		self.speed -= 1
	else:
		self.speed = 4

class Enemy(Sprite):
def init(self, spriteshape, color, startx, starty):
Sprite.init(self, spriteshape, color, startx, starty)
self.speed = 6
self.setheading(random.randint(0,360))

class Missile(Sprite):
def init(self, spriteshape, color, startx, starty):
Sprite.init(self, spriteshape, color, startx, starty)
self.shapesize(stretch_wid=0.3, stretch_len=0.4, outline=None)
self.speed = 20
self.status = “ready”
self.goto(-1000, 1000)

def fire(self):
	if self.status == "ready":
		self.goto(player.xcor(), player.ycor())
		s = pygame.mixer.Sound('laser.mp3')
		s.play(loops=0, maxtime=0, fade_ms=0)
		self.setheading(player.heading())
		self.status = "firing"
		
def move(self):

	if self.status == "ready":
		self.goto(-1000, 1000)
	
	if self.status == "firing":
		self.fd(self.speed)	
		
	#Border check
	if self.xcor() < -290 or self.xcor() > 290 or \
		self.ycor()< -290 or self.ycor()> 290:
		self.goto(-1000,1000)
		self.status = "ready"

class Game():
def init(self):
self.level = 1
self.score = 0
self.state = “playing”
self.pen = turtle.Turtle()
self.lives = 3

def draw_border(self):
	#Draw border
	self.pen.speed(0)
	self.pen.color("white")
	self.pen.pensize(3)
	self.pen.penup()
	self.pen.goto(-300, 300)
	self.pen.pendown()
	for side in range(4):
		self.pen.fd(600)
		self.pen.rt(90)
	self.pen.penup()
	self.pen.ht()

def show_status(self):
	self.pen.undo()
	msg = "Score: %s" %(self.score)
	self.pen.penup()
	self.pen.goto(-300, 310)
	self.pen.write(msg, font=("Arial", 16, "normal"))

#Create game object
game = Game()

#Draw the game border
game.draw_border()

game.show_status()

pygame.mixer.pre_init(22050, -16, 2, 4096)
pygame.mixer.init()
pygame.init()

#Create my sprites
player = Player(“triangle”, “white”, 0, 0)
enemy = Enemy(“circle”, “red”, -100, 0)
missile = Missile(“triangle”, “yellow”, 0, 0)

#keyboard bindings
turtle.onkey(player.turn_left, “Left”)
turtle.onkey(player.turn_right, “Right”)
turtle.onkey(player.accelerate, “Up”)
turtle.onkey(player.decelerate, “Down”)
turtle.onkey(missile.fire, “space”)
turtle.listen()

#Main game loop
while True:
player.move()
enemy.move()
missile.move()

#Check for a collision with the player
if player.is_collision(enemy):
	x = random.randint(-250, 250)
	y = random.randint(-250, 250)
	s = pygame.mixer.Sound("explosion.mp3")
	s.play(loops=0, maxtime=0, fade_ms=0)
	enemy.goto(x, y)
	game.score -= 100
	game.show_status()

#Check for a collision between the missile and the enemy
if missile.is_collision(enemy):
	x = random.randint(-250, 250)
	y = random.randint(-250, 250)
	s = pygame.mixer.Sound("explosion.mp3")
	s.play(loops=0, maxtime=0, fade_ms=0)
	enemy.goto(x, y)
	missile.status = "ready"
	game.score += 100
	game.show_status()

delay = raw_input("Press enter to finish. > ")

 类似资料:

相关阅读

相关文章

相关问答