diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 00000000..0029c264 Binary files /dev/null and b/.DS_Store differ diff --git a/BlueTank.png b/BlueTank.png new file mode 100644 index 00000000..43d6cb97 Binary files /dev/null and b/BlueTank.png differ diff --git a/NewBackground.jpg b/NewBackground.jpg new file mode 100644 index 00000000..7e8e51bc Binary files /dev/null and b/NewBackground.jpg differ diff --git a/ProjectProposal.txt b/ProjectProposal.txt new file mode 100644 index 00000000..3c044037 --- /dev/null +++ b/ProjectProposal.txt @@ -0,0 +1,17 @@ +What is the main idea of your project? What topics will you explore and what will you generate? What is your minimum viable product? What is a stretch goal? + - We will make a tank game based on wii tanks. Our MVP will be a 2 player PVP. A stretch goal is to add an AI. +What are your learning goals for this project (for each member)? + + - Alex: getting better at using class and object, also make readable code + + - Ricky: Get better at debugging, making more readable and modular code, and getting better with classes and objects. + +What libraries are you planning to use? (if you don’t know enough yet, please outline how you will decide this question during the beginning phase of the project). + - Pygame and whatever else we need. +What do you plan to accomplish by the mid-project check-in? (See below for some generic goals; edit to suit your particular project) + + - We plan to have a tank that can move (stretch: shooting projectiles?) + +What do you view as the biggest risks to you being successful on this project? + + - Ususlly time is the biggest factor in completing projects like this. \ No newline at end of file diff --git a/RedTank.png b/RedTank.png new file mode 100644 index 00000000..c6fb9613 Binary files /dev/null and b/RedTank.png differ diff --git a/SoftdesMP4WriteUp.pdf b/SoftdesMP4WriteUp.pdf new file mode 100644 index 00000000..43a6b7d9 Binary files /dev/null and b/SoftdesMP4WriteUp.pdf differ diff --git a/SoftdesMP4WriteUptxt.txt b/SoftdesMP4WriteUptxt.txt new file mode 100644 index 00000000..e3043058 --- /dev/null +++ b/SoftdesMP4WriteUptxt.txt @@ -0,0 +1,13 @@ +Abstract: + Alex and I made a collaborative tank game in which two players work to clear a map of blue squares. We got our idea from a mini-game within Wii Play called “tanks” and many other areal view tank games. +Results: + We mad the game as modular as possible so we could go from our “MVP” to as advanced as we want. Our “MVP” was one tank that was able to move around a screen. We then made a larger background and a second tank and also added rotation. After this, we added the capability for the tanks to fire. Finally we added randomized dots that the tanks were able to clear from the map. This is far from our MVP, but still short of our stretch goal, as the program is not always able to handle two people pressing keys at once. +Implementation: + We began by creating a window and bliting an image of equal size on to it. We next made a “Player” class that creates a sprite from an image and blits it on to the background. Within the player function is an update function that updates the position and rotation of the tank/sprite. + Our next class is “Bullet”, which creates another sprite (this time with a small rectangle), references the player’s position and current angle of rotation and sends the sprite along that angle from that position. + Next, we created a class called “Block” which generates squares at random locations around the field. We later reference this in a separate definition to create sprites that are deleted when they come into contact with our “Bullets” + Finally, we have a class called “Controller” which adds values to the player class, thus moving the player. + +Reflection: + In terms of process, we did a good job at organizing the project from the very beginning. We created a Trello and distributed tasks to ourselves. We used a Scrum-like system to show to-do, doing, and done tasks, as well as a backlog for ideas that we could ad if we had time. + Overall this worked well and we scoped our project well. it was very doable and had a lot of room to scale upward, which we did to some degree. Alex and I had some time to pair program but not as much as we anticipated. We also had a lot of work outside of class with QEA and P&M. This made it difficult to balance, especially when some assignments (usually QEA) took longer than anticipated. This gave us less time to work on the project, thus we were not able to implement all of the features that we wanted to. \ No newline at end of file diff --git a/Tank_Cop.py b/Tank_Cop.py new file mode 100644 index 00000000..918fb619 --- /dev/null +++ b/Tank_Cop.py @@ -0,0 +1,224 @@ +import pygame +from pygame.locals import * +from math import * +import time +import random + +# Define some colors +WHITE = (255, 255, 255) # have no idea what it does +BLACK = (0, 0, 0) # need to keep +BLUE = (0, 0, 255) +# Call this function so the Pygame library can initialize itself +pygame.init() + +# Create an 800x600 sized screen +screen = pygame.display.set_mode([1024,768]) + +clock = pygame.time.Clock() # I sure this controls the time the game (how faster the puase is at the bottom) + +class Player(pygame.sprite.Sprite): # player object right now is confusing do it postion here but not being used + """ Encodes the state of the paddle in the game """ + def __init__(self,filename, a, b): + """ Initialize the player and position (x,y) """ + super().__init__() + + + self.angle = 0 + self.image = pygame.image.load(filename).convert() #image goes here + self.image.set_colorkey(WHITE) + + self.rect = self.image.get_rect() + self.rect.x = a; + self.rect.y = b; + + def update(self):# this may work idk yet + """ update the state of the paddle """ + + self.image_dom = rot_center(self.image, self.angle) + + def __str__(self): + return "Player, x=%f, y=%f" % (self.x, self.y) + +class Bullet(pygame.sprite.Sprite): + """ This class represents the bullet . """ + def __init__(self, angle, x, y): + # Call the parent class (Sprite) constructor + super().__init__() + + self.image = pygame.Surface([4, 10]) + self.image.fill(WHITE) + + + + self.speed = 5 + + self.angle = angle + + self.rect = self.image.get_rect() + + self.rect.x = x + self.rect.y = y + + def update(self): + """ Move the bullet. """ + self.rect.x += self.speed * cos(radians(-self.angle+3)) + self.rect.y += self.speed * sin(radians(-self.angle+3)) + +class Block(pygame.sprite.Sprite): + """ This class represents the block. """ + def __init__(self, color): + # Call the parent class (Sprite) constructor + super().__init__() + + self.image = pygame.Surface([20, 15]) + self.image.fill(color) + + self.rect = self.image.get_rect() + +class PyGameKeyboardController(object): #works well a little jumpy maybe use vx + """ Handles keyboard input for brick breaker """ + def __init__(self): + + self.x = 0 + self.y = 0 + self.j =0 + self.i =0 + def handle_event1(self, event, Player): + """ Left and right presses modify the x velocity of the paddle """ + #link for event.key https://www.pygame.org/docs/ref/key.html + if event.type != KEYDOWN: + return + if event.key == pygame.K_a: + Player.rect.x += -2 + if event.key == pygame.K_d: + Player.rect.x += 2 + if event.key == pygame.K_w: + Player.rect.y += -2 + if event.key == pygame.K_s: + Player.rect.y += 2 + if event.key == pygame.K_q: + Player.angle += 1 %360 + if event.key == pygame.K_e: + Player.angle += -1 %360 + if event.key == pygame.K_2: + self.i +=1 + + def handle_event2(self, event, Player): + """ Left and right presses modify the x velocity of the paddle """ + #link for event.key https://www.pygame.org/docs/ref/key.html + if event.type != KEYDOWN: + return + if event.key == pygame.K_j: + Player.rect.x += -2 + if event.key == pygame.K_l: + Player.rect.x += 2 + if event.key == pygame.K_i: + Player.rect.y += -2 + if event.key == pygame.K_k: + Player.rect.y += 2 + if event.key == pygame.K_u: + Player.angle += 1 %360 + if event.key == pygame.K_o: + Player.angle += -1 %360 + if event.key == pygame.K_8: + self.j +=1 + + + +# Fuction that rotate image on its center +def rot_center(image, angle): + """rotate an image while keeping its center and size""" + orig_rect = image.get_rect() + rot_image = pygame.transform.rotate(image, angle) + rot_rect = orig_rect.copy() + rot_rect.center = rot_image.get_rect().center + rot_image = rot_image.subsurface(rot_rect).copy() + return rot_image + + + +all_sprites_list = pygame.sprite.Group() +block_list = pygame.sprite.Group() +bullet_list = pygame.sprite.Group() +# Set positions of graphics +background_position = [0, 0] + +# Load and set up graphics only back grond +background_image = pygame.image.load("NewBackground.jpg").convert() + +for i in range(100): + # This represents a block + block = Block(BLUE) + + # Set a random location for the block 1024,768 + block.rect.x = random.randrange(1024) + block.rect.y = random.randrange(768) + + # Add the block to the list of objects + block_list.add(block) + all_sprites_list.add(block) + + + +clock = pygame.time.Clock() + +score = 0 +player1 = Player("BlueTank.png", 0, 0) +player2 = Player("RedTank.png", 100, 100) +#all_sprites_list.add(player) +controller = PyGameKeyboardController() +done = False + +while not done: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + done = True + + + # Copy image to screen: + screen.blit(background_image, background_position) + + + controller.handle_event1(event,player1) + controller.handle_event2(event,player2) + player1.update() + player2.update() + all_sprites_list.update() + + if controller.i > 0: + bullet = Bullet(player1.angle,player1.rect.center[0],player1.rect.center[1]) + all_sprites_list.add(bullet) + bullet_list.add(bullet) + controller.i += -1 + if controller.j > 0: + bullet = Bullet(player2.angle,player2.rect.center[0],player2.rect.center[1]) + all_sprites_list.add(bullet) + bullet_list.add(bullet) + controller.j += -1 + + + screen.blit(player1.image_dom, [player1.rect.x, player1.rect.y]) + screen.blit(player2.image_dom, [player2.rect.x, player2.rect.y]) + for bullet in bullet_list: + + # See if it hit a block + block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True) + + # For each block hit, remove the bullet and add to the score + for block in block_hit_list: + bullet_list.remove(bullet) + all_sprites_list.remove(bullet) + score += 1 + print(score) + + # Remove the bullet if it flies up off the screen + if bullet.rect.y < -10: + bullet_list.remove(bullet) + all_sprites_list.remove(bullet) + + all_sprites_list.draw(screen) + pygame.display.flip() + + clock.tick(60) + +pygame.quit() diff --git a/test/BasicsOfPythonGame.py b/test/BasicsOfPythonGame.py new file mode 100644 index 00000000..b4120964 --- /dev/null +++ b/test/BasicsOfPythonGame.py @@ -0,0 +1,12 @@ +import pygame +from pygame.locals import * +pygame.init() +SCREENRECT = Rect(0, 0, 640, 480) +background = [pygame.image.load('GrassBackground.jpg'),pygame.image.load('GrassBackground.jpg')] +screen_width=700 +screen_height=400 +screen=pygame.display.set_mode([screen_width,screen_height]) +for i in range(6): + screen.blit(background[i], (i*1, 0)) +playerpos = 3 +screen.blit(playerimage, (playerpos*10, 0)) \ No newline at end of file diff --git a/test/GrassBackground.jpg b/test/GrassBackground.jpg new file mode 100644 index 00000000..3b5e8a54 Binary files /dev/null and b/test/GrassBackground.jpg differ diff --git a/test/MiniProject4.py b/test/MiniProject4.py new file mode 100644 index 00000000..5a0dee06 --- /dev/null +++ b/test/MiniProject4.py @@ -0,0 +1,16 @@ +import pygame +#from pygame.locals import * +pygame.init() +screen = create_screen() +player = load_player_image() +background = load_background_image() +screen.blit(background, (0, 0)) #draw the background +position = player.get_rect() +screen.blit(player, position) #draw the player +pygame.display.update() #and show it all +for x in range(100): #animate 100 frames + screen.blit(background, position, position) #erase + position = position.move(2, 0) #move player + screen.blit(player, position) #draw new player + pygame.display.update() #and show it all + pygame.time.delay(100) #stop the program for 1/10 second diff --git a/test/Move.py b/test/Move.py new file mode 100644 index 00000000..651ce76b --- /dev/null +++ b/test/Move.py @@ -0,0 +1,121 @@ +import pygame +from pygame.locals import * +import time + +class PyGameWindowView(object): + """ A view of brick breaker rendered in a Pygame window """ + def __init__(self, model, size): + """ Initialize the view with a reference to the model and the + specified game screen dimensions (represented as a tuple + containing the width and height """ + self.model = model + self.screen = pygame.display.set_mode(size) + + def draw(self): + """ Draw the current game state to the screen """ + self.screen.fill(pygame.Color(0,0,0)) + # for brick in self.model.bricks: + # pygame.draw.rect(self.screen, + # pygame.Color(255, 255, 255), + # pygame.Rect(brick.x, + # brick.y, + # brick.width, + # brick.height)) + pygame.draw.rect(self.screen, + pygame.Color(255, 0, 0), + pygame.Rect(self.model.paddle.x, + self.model.paddle.y, + self.model.paddle.width, + self.model.paddle.height)) + pygame.display.update() + +class BrickBreakerModel(object): + """ Encodes a model of the game state """ + def __init__(self, size): + self.bricks = [] + self.width = size[0] + self.height = size[1] + self.brick_width = 100 + self.brick_height = 40 + self.brick_space = 10 + self.player_image = pygame.image.load("").convert() + self.paddle = Paddle(20, 100, 500, self.height - 500) + + def update(self): + """ Update the game state (currently only tracking the paddle) """ + self.paddle.update() + + def __str__(self): + output_lines = [] + # convert each brick to a string for outputting + for brick in self.bricks: + output_lines.append(str(brick)) + output_lines.append(str(self.paddle)) + # print one item per line + return "\n".join(output_lines) + + +class Paddle(object): + """ Encodes the state of the paddle in the game """ + def __init__(self, height, width, x, y): + """ Initialize a paddle with the specified height, width, + and position (x,y) """ + self.height = height + self.width = width + self.x = x + self.y = y + self.vx = 0.0 + self.vy = 0.0 + + self.player_image = pygame.image.load("top_down_tank-8hkMRt.png").convert() + def update(self): + """ update the state of the paddle """ + self.x += self.vx + self.y += self.vy + def __str__(self): + return "Paddle height=%f, width=%f, x=%f, y=%f" % (self.height, + self.width, + self.x, + self.y) +class PyGameKeyboardController(object): + """ Handles keyboard input for brick breaker """ + def __init__(self,model): + self.model = model + + def handle_event(self,event): + """ Left and right presses modify the x velocity of the paddle """ + #link for event.key https://www.pygame.org/docs/ref/key.html + if event.type != KEYDOWN: + return + if event.key == pygame.K_LEFT: + self.model.paddle.vx += -1 + if event.key == pygame.K_RIGHT: + self.model.paddle.vx += 1 + if event.key == pygame.K_UP: + self.model.paddle.vy += -1 + if event.key == pygame.K_DOWN: + self.model.paddle.vy += 1 + +if __name__ == '__main__': + pygame.init() + + size = (1000, 1000) + + model = BrickBreakerModel(size) + print(model) + view = PyGameWindowView(model, size) + #controller = PyGameKeyboardController(model) + controller = PyGameKeyboardController(model) + + running = True + while running: + for event in pygame.event.get(): + if event.type == QUIT: + running = False + controller.handle_event(event) + model.update() + view.draw() + screen.blit(Paddle.player_image, [Paddle.x,Paddle.y]) + time.sleep(.001) + + pygame.quit() diff --git a/test/MoveWBackground.py b/test/MoveWBackground.py new file mode 100644 index 00000000..11b3f10d --- /dev/null +++ b/test/MoveWBackground.py @@ -0,0 +1,124 @@ +import pygame +from pygame.locals import * +import time +pygame.init() +#display.set_mode(resolution=(0,0), flags=0, depth=0) +background_image = pygame.image.load("GrassBackground.jpg").convert() +class PyGameWindowView(object): + """ A view of brick breaker rendered in a Pygame window """ + def __init__(self, model, size=(800, 600)): + """ Initialize the view with a reference to the model and the + specified game screen dimensions (represented as a tuple + containing the width and height """ + self.model = model + self.screen = pygame.display.set_mode(resolution=(800,600)) + self.screen.blit(background_image, [0, 0]) + + def draw(self): + """ Draw the current game state to the screen """ + self.screen.fill(pygame.Color(0,0,0)) + # for brick in self.model.bricks: + # pygame.draw.rect(self.screen, + # pygame.Color(255, 255, 255), + # pygame.Rect(brick.x, + # brick.y, + # brick.width, + # brick.height)) + pygame.draw.rect(self.screen, + pygame.Color(255, 0, 0), + pygame.Rect(self.model.paddle.x, + self.model.paddle.y, + self.model.paddle.width, + self.model.paddle.height)) + pygame.display.update() + + + +class BrickBreakerModel(object): + """ Encodes a model of the game state """ + def __init__(self, size): + self.bricks = [] + self.width = size[0] + self.height = size[1] + self.brick_width = 100 + self.brick_height = 40 + self.brick_space = 10 + + self.paddle = Paddle(20, 100, 500, self.height - 500) + + def update(self): + """ Update the game state (currently only tracking the paddle) """ + self.paddle.update() + + def __str__(self): + output_lines = [] + # convert each brick to a string for outputting + for brick in self.bricks: + output_lines.append(str(brick)) + output_lines.append(str(self.paddle)) + # print one item per line + return "\n".join(output_lines) + + +class Paddle(object): + """ Encodes the state of the paddle in the game """ + def __init__(self, height, width, x, y): + """ Initialize a paddle with the specified height, width, + and position (x,y) """ + self.height = height + self.width = width + self.x = x + self.y = y + self.vx = 0.0 + self.vy = 0.0 + + def update(self): + """ update the state of the paddle """ + self.x += self.vx + self.y += self.vy + def __str__(self): + return "Paddle height=%f, width=%f, x=%f, y=%f" % (self.height, + self.width, + self.x, + self.y) +class PyGameKeyboardController(object): + """ Handles keyboard input for brick breaker """ + def __init__(self,model): + self.model = model + + def handle_event(self,event): + """ Left and right presses modify the x velocity of the paddle """ + #link for event.key https://www.pygame.org/docs/ref/key.html + if event.type != KEYDOWN: + return + if event.key == pygame.K_LEFT: + self.model.paddle.x += -1 + if event.key == pygame.K_RIGHT: + self.model.paddle.x += 1 + if event.key == pygame.K_UP: + self.model.paddle.y += -1 + if event.key == pygame.K_DOWN: + self.model.paddle.y += 1 + +if __name__ == '__main__': + pygame.init() + + size = (1000, 1000) + + model = BrickBreakerModel(size) + print(model) + view = PyGameWindowView(model, size) + #controller = PyGameKeyboardController(model) + controller = PyGameKeyboardController(model) + + running = True + while running: + for event in pygame.event.get(): + if event.type == QUIT: + running = False + controller.handle_event(event) + model.update() + view.draw() + time.sleep(.001) + + pygame.quit() diff --git a/test/PhotoTest.py b/test/PhotoTest.py new file mode 100644 index 00000000..ba797483 --- /dev/null +++ b/test/PhotoTest.py @@ -0,0 +1,18 @@ +import pygame +from pygame.locals import * +import time +pygame.init() +screen = pygame.display.set_mode([800, 600]) +clock = pygame.time.Clock() +background_image = pygame.image.load("GrassBackground.jpg").convert() +done = False +while not done: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + done = True + elif event.type == pygame.KEYDOWN: + done = True + screen.blit(background_image,[0,0]) + pygame.display.flip() +clock.tick(60) +pygame.quit() \ No newline at end of file diff --git a/test/Rote_test.py b/test/Rote_test.py new file mode 100644 index 00000000..918fb619 --- /dev/null +++ b/test/Rote_test.py @@ -0,0 +1,224 @@ +import pygame +from pygame.locals import * +from math import * +import time +import random + +# Define some colors +WHITE = (255, 255, 255) # have no idea what it does +BLACK = (0, 0, 0) # need to keep +BLUE = (0, 0, 255) +# Call this function so the Pygame library can initialize itself +pygame.init() + +# Create an 800x600 sized screen +screen = pygame.display.set_mode([1024,768]) + +clock = pygame.time.Clock() # I sure this controls the time the game (how faster the puase is at the bottom) + +class Player(pygame.sprite.Sprite): # player object right now is confusing do it postion here but not being used + """ Encodes the state of the paddle in the game """ + def __init__(self,filename, a, b): + """ Initialize the player and position (x,y) """ + super().__init__() + + + self.angle = 0 + self.image = pygame.image.load(filename).convert() #image goes here + self.image.set_colorkey(WHITE) + + self.rect = self.image.get_rect() + self.rect.x = a; + self.rect.y = b; + + def update(self):# this may work idk yet + """ update the state of the paddle """ + + self.image_dom = rot_center(self.image, self.angle) + + def __str__(self): + return "Player, x=%f, y=%f" % (self.x, self.y) + +class Bullet(pygame.sprite.Sprite): + """ This class represents the bullet . """ + def __init__(self, angle, x, y): + # Call the parent class (Sprite) constructor + super().__init__() + + self.image = pygame.Surface([4, 10]) + self.image.fill(WHITE) + + + + self.speed = 5 + + self.angle = angle + + self.rect = self.image.get_rect() + + self.rect.x = x + self.rect.y = y + + def update(self): + """ Move the bullet. """ + self.rect.x += self.speed * cos(radians(-self.angle+3)) + self.rect.y += self.speed * sin(radians(-self.angle+3)) + +class Block(pygame.sprite.Sprite): + """ This class represents the block. """ + def __init__(self, color): + # Call the parent class (Sprite) constructor + super().__init__() + + self.image = pygame.Surface([20, 15]) + self.image.fill(color) + + self.rect = self.image.get_rect() + +class PyGameKeyboardController(object): #works well a little jumpy maybe use vx + """ Handles keyboard input for brick breaker """ + def __init__(self): + + self.x = 0 + self.y = 0 + self.j =0 + self.i =0 + def handle_event1(self, event, Player): + """ Left and right presses modify the x velocity of the paddle """ + #link for event.key https://www.pygame.org/docs/ref/key.html + if event.type != KEYDOWN: + return + if event.key == pygame.K_a: + Player.rect.x += -2 + if event.key == pygame.K_d: + Player.rect.x += 2 + if event.key == pygame.K_w: + Player.rect.y += -2 + if event.key == pygame.K_s: + Player.rect.y += 2 + if event.key == pygame.K_q: + Player.angle += 1 %360 + if event.key == pygame.K_e: + Player.angle += -1 %360 + if event.key == pygame.K_2: + self.i +=1 + + def handle_event2(self, event, Player): + """ Left and right presses modify the x velocity of the paddle """ + #link for event.key https://www.pygame.org/docs/ref/key.html + if event.type != KEYDOWN: + return + if event.key == pygame.K_j: + Player.rect.x += -2 + if event.key == pygame.K_l: + Player.rect.x += 2 + if event.key == pygame.K_i: + Player.rect.y += -2 + if event.key == pygame.K_k: + Player.rect.y += 2 + if event.key == pygame.K_u: + Player.angle += 1 %360 + if event.key == pygame.K_o: + Player.angle += -1 %360 + if event.key == pygame.K_8: + self.j +=1 + + + +# Fuction that rotate image on its center +def rot_center(image, angle): + """rotate an image while keeping its center and size""" + orig_rect = image.get_rect() + rot_image = pygame.transform.rotate(image, angle) + rot_rect = orig_rect.copy() + rot_rect.center = rot_image.get_rect().center + rot_image = rot_image.subsurface(rot_rect).copy() + return rot_image + + + +all_sprites_list = pygame.sprite.Group() +block_list = pygame.sprite.Group() +bullet_list = pygame.sprite.Group() +# Set positions of graphics +background_position = [0, 0] + +# Load and set up graphics only back grond +background_image = pygame.image.load("NewBackground.jpg").convert() + +for i in range(100): + # This represents a block + block = Block(BLUE) + + # Set a random location for the block 1024,768 + block.rect.x = random.randrange(1024) + block.rect.y = random.randrange(768) + + # Add the block to the list of objects + block_list.add(block) + all_sprites_list.add(block) + + + +clock = pygame.time.Clock() + +score = 0 +player1 = Player("BlueTank.png", 0, 0) +player2 = Player("RedTank.png", 100, 100) +#all_sprites_list.add(player) +controller = PyGameKeyboardController() +done = False + +while not done: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + done = True + + + # Copy image to screen: + screen.blit(background_image, background_position) + + + controller.handle_event1(event,player1) + controller.handle_event2(event,player2) + player1.update() + player2.update() + all_sprites_list.update() + + if controller.i > 0: + bullet = Bullet(player1.angle,player1.rect.center[0],player1.rect.center[1]) + all_sprites_list.add(bullet) + bullet_list.add(bullet) + controller.i += -1 + if controller.j > 0: + bullet = Bullet(player2.angle,player2.rect.center[0],player2.rect.center[1]) + all_sprites_list.add(bullet) + bullet_list.add(bullet) + controller.j += -1 + + + screen.blit(player1.image_dom, [player1.rect.x, player1.rect.y]) + screen.blit(player2.image_dom, [player2.rect.x, player2.rect.y]) + for bullet in bullet_list: + + # See if it hit a block + block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True) + + # For each block hit, remove the bullet and add to the score + for block in block_hit_list: + bullet_list.remove(bullet) + all_sprites_list.remove(bullet) + score += 1 + print(score) + + # Remove the bullet if it flies up off the screen + if bullet.rect.y < -10: + bullet_list.remove(bullet) + all_sprites_list.remove(bullet) + + all_sprites_list.draw(screen) + pygame.display.flip() + + clock.tick(60) + +pygame.quit() diff --git a/test/Test.py b/test/Test.py new file mode 100644 index 00000000..a7d70a40 --- /dev/null +++ b/test/Test.py @@ -0,0 +1,117 @@ +""" + Sample Python/Pygame Programs + Simpson College Computer Science + http://programarcadegames.com/ + http://simpson.edu/computer-science/ + + Explanation video: http://youtu.be/4YqIKncMJNs + Explanation video: http://youtu.be/ONAK8VZIcI4 + Explanation video: http://youtu.be/_6c4o41BIms +""" + +import pygame +from pygame.locals import * +import time + +# Define some colors +#WHITE = (255, 255, 255) +BLACK = (0, 0, 0) + +# Call this function so the Pygame library can initialize itself +pygame.init() + +# Create an 1024x768 sized screen +screen = pygame.display.set_mode([1024, 768]) + +# This sets the name of the window +# pygame.display.set_caption('CMSC 150 is cool') + +clock = pygame.time.Clock() + +class Player(object): + """ Encodes the state of the paddle in the game """ + def __init__(self, filename, x, y): + """ Initialize the player and position (x,y) """ + + self.x = x + self.y = y + self.player_image = pygame.image.load(filename).convert() + self.player_image.set_colorkey(BLACK) + def update(self): + """ update the state of the paddle """ + self.x += self.x + self.y += self.y + + def __str__(self): + return "Player, x=%f, y=%f" % (self.x, self.y) + +class PyGameKeyboardController(object): + """ Handles keyboard input tanks """ + def __init__(self, u, d, l, r): + #self.model = model + self.x = 0 + self.y = 0 + self.l = self.l + self.d = self.d + self.u = self.u + self.r = self.r + def handle_event(self,event): + """ Left and right presses modify the x velocity of the tank """ + #link for event.key https://www.pygame.org/docs/ref/key.html + + if event.type != KEYDOWN: + return + if event.key == self.l: + self.x += -10 + if event.key == self.r: + self.x += 10 + if event.key == self.u: + self.y += -10 + if event.key == self.d: + self.y += 10 +# Before the loop, load the sounds: +#click_sound = pygame.mixer.Sound("laser5.ogg") + +# Set positions of graphics +background_position = [0, 0] + +# Load and set up graphics. +background_image = pygame.image.load("NewBackground.jpg").convert() +# player_image = pygame.image.load("top_down_tank-8hkMRt.png").convert() +# player_image.set_colorkey(BLACK) + +clock = pygame.time.Clock() +controller1 = PyGameKeyboardController(pygame.K_UP, pygame.K_DOWN, pygame.K_LEFT, pygame.K_RIGHT) +controller2 = PyGameKeyboardController('K_w', 'K_s', 'K_a', 'K_d') + +player1 = Player("BlueTank.png", 0, 0) +player2 = Player("RedTank.png", 100, 100) +done = False +while not done: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + done = True + # elif event.type == pygame.MOUSEBUTTONDOWN: + # click_sound.play() + #if event.type != KEYDOWN: + + # Copy image to screen: + screen.blit(background_image, background_position) + + + controller1.handle_event(event) + controller2.handle_event(event) + player1.update() + player2.update() + + + + # Copy image to screen: + screen.blit(player1.player_image, [controller1.x, controller1.y]) + screen.blit(player2.player_image, [controller2.x, controller2.y]) + + pygame.display.flip() + + clock.tick(60) + +pygame.quit() diff --git a/test/TestGame.py b/test/TestGame.py new file mode 100644 index 00000000..19bc423e --- /dev/null +++ b/test/TestGame.py @@ -0,0 +1,55 @@ +import pygame + +# Define some colors +WHITE = (255, 255, 255) +BLACK = (0, 0, 0) + +# Call this function so the Pygame library can initialize itself +pygame.init() + +# Create an 800x600 sized screen +screen = pygame.display.set_mode([600, 361]) + +# This sets the name of the window +pygame.display.set_caption('TestGame') + +clock = pygame.time.Clock() + +# Before the loop, load the sounds: +#click_sound = pygame.mixer.Sound("laser5.ogg") + +# Set positions of graphics +background_position = [0, 0] + +# Load and set up graphics. +background_image = pygame.image.load("GrassBackground.jpg").convert() +player_image = pygame.image.load("top_down_tank-8hkMRt.png").convert() +player_image.set_colorkey(BLACK) + +done = False + +while not done: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + done = True + elif event.type == pygame.MOUSEBUTTONDOWN: + done = True + #click_sound.play() + + # Copy image to screen: + screen.blit(background_image, background_position) + + # Get the current mouse position. This returns the position + # as a list of two numbers. + player_position = pygame.mouse.get_pos() + x = player_position[0] + y = player_position[1] + + # Copy image to screen: + screen.blit(player_image, [x, y]) + + pygame.display.flip() + + clock.tick(60) + +pygame.quit() \ No newline at end of file diff --git a/test/bullet.py b/test/bullet.py new file mode 100644 index 00000000..e9b3b0ac --- /dev/null +++ b/test/bullet.py @@ -0,0 +1,176 @@ +""" + Show how to fire bullets. + + Sample Python/Pygame Programs + Simpson College Computer Science + http://programarcadegames.com/ + http://simpson.edu/computer-science/ + + Explanation video: http://youtu.be/PpdJjaiLX6A +""" +import pygame +import random + +# Define some colors +BLACK = (0, 0, 0) +WHITE = (255, 255, 255) +RED = (255, 0, 0) +BLUE = (0, 0, 255) + +# --- Classes + + +class Block(pygame.sprite.Sprite): + """ This class represents the block. """ + def __init__(self, color): + # Call the parent class (Sprite) constructor + super().__init__() + + self.image = pygame.Surface([20, 15]) + self.image.fill(color) + + self.rect = self.image.get_rect() + + +class Player(pygame.sprite.Sprite): + """ This class represents the Player. """ + + def __init__(self): + """ Set up the player on creation. """ + # Call the parent class (Sprite) constructor + super().__init__() + + self.image = pygame.Surface([20, 20]) + self.image.fill(RED) + + self.rect = self.image.get_rect() + + def update(self): + """ Update the player's position. """ + # Get the current mouse position. This returns the position + # as a list of two numbers. + pos = pygame.mouse.get_pos() + + # Set the player x position to the mouse x position + self.rect.x = pos[0] + + +class Bullet(pygame.sprite.Sprite): + """ This class represents the bullet . """ + def __init__(self): + # Call the parent class (Sprite) constructor + super().__init__() + + self.image = pygame.Surface([4, 10]) + self.image.fill(BLACK) + + self.rect = self.image.get_rect() + + def update(self): + """ Move the bullet. """ + self.rect.y -= 3 + + +# --- Create the window + +# Initialize Pygame +pygame.init() + +# Set the height and width of the screen +screen_width = 700 +screen_height = 400 +screen = pygame.display.set_mode([screen_width, screen_height]) + +# --- Sprite lists + +# This is a list of every sprite. All blocks and the player block as well. +all_sprites_list = pygame.sprite.Group() + +# List of each block in the game +block_list = pygame.sprite.Group() + +# List of each bullet +bullet_list = pygame.sprite.Group() + +# --- Create the sprites + +for i in range(50): + # This represents a block + block = Block(BLUE) + + # Set a random location for the block + block.rect.x = random.randrange(screen_width) + block.rect.y = random.randrange(350) + + # Add the block to the list of objects + block_list.add(block) + all_sprites_list.add(block) + +# Create a red player block +player = Player() +all_sprites_list.add(player) + +# Loop until the user clicks the close button. +done = False + +# Used to manage how fast the screen updates +clock = pygame.time.Clock() + +score = 0 +player.rect.y = 370 + +# -------- Main Program Loop ----------- +while not done: + # --- Event Processing + for event in pygame.event.get(): + if event.type == pygame.QUIT: + done = True + + elif event.type == pygame.MOUSEBUTTONDOWN: + # Fire a bullet if the user clicks the mouse button + bullet = Bullet() + # Set the bullet so it is where the player is + bullet.rect.x = player.rect.x + bullet.rect.y = player.rect.y + # Add the bullet to the lists + all_sprites_list.add(bullet) + bullet_list.add(bullet) + + # --- Game logic + + # Call the update() method on all the sprites + all_sprites_list.update() + + # Calculate mechanics for each bullet + for bullet in bullet_list: + + # See if it hit a block + block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True) + + # For each block hit, remove the bullet and add to the score + for block in block_hit_list: + bullet_list.remove(bullet) + all_sprites_list.remove(bullet) + score += 1 + print(score) + + # Remove the bullet if it flies up off the screen + if bullet.rect.y < -10: + bullet_list.remove(bullet) + all_sprites_list.remove(bullet) + + # --- Draw a frame + + # Clear the screen + screen.fill(WHITE) + + # Draw all the spites + all_sprites_list.draw(screen) + + # Go ahead and update the screen with what we've drawn. + pygame.display.flip() + + # --- Limit to 20 frames per second + clock.tick(60) + +pygame.quit() diff --git a/test/master.py b/test/master.py new file mode 100644 index 00000000..1dd271fb --- /dev/null +++ b/test/master.py @@ -0,0 +1,92 @@ +import pygame +from pygame.locals import * +import time + +# Define some colors +WHITE = (255, 255, 255) # have no idea what it does +BLACK = (0, 0, 0) # need to keep + +# Call this function so the Pygame library can initialize itself +pygame.init() + +# Create an 800x600 sized screen +screen = pygame.display.set_mode([600, 361]) + +clock = pygame.time.Clock() # I sure this controls the time the game (how faster the puase is at the bottom) + +class Player(object): # player object right now is confusing do it postion here but not being used + """ Encodes the state of the paddle in the game """ + def __init__(self): + """ Initialize the player and position (x,y) """ + + self.x = 0 + self.y = 0 + self.player_image = pygame.image.load("top_down_tank-8hkMRt.png").convert() #image goes here + self.player_image.set_colorkey(BLACK) + + def update(self):# this may work idk yet + """ update the state of the paddle """ + self.x += self.x + self.y += self.y + + def __str__(self): + return "Player, x=%f, y=%f" % (self.x, self.y) + +class PyGameKeyboardController(object): #works well a little jumpy maybe use vx + """ Handles keyboard input for brick breaker """ + def __init__(self): + #self.model = model + self.x = 0 + self.y = 0 + + def handle_event(self,event): + """ Left and right presses modify the x velocity of the paddle """ + #link for event.key https://www.pygame.org/docs/ref/key.html + + if event.type != KEYDOWN: + return + if event.key == pygame.K_LEFT: + self.x += -10 + if event.key == pygame.K_RIGHT: + self.x += 10 + if event.key == pygame.K_UP: + self.y += -10 + if event.key == pygame.K_DOWN: + self.y += 10 +# Before the loop, load the sounds: +#click_sound = pygame.mixer.Sound("laser5.ogg") + +# Set positions of graphics +background_position = [0, 0] + +# Load and set up graphics only back grond +background_image = pygame.image.load("GrassBackground.jpg").convert() + + +clock = pygame.time.Clock() +controller = PyGameKeyboardController() +player = Player() +done = False +while not done: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + done = True + + + # Copy image to screen: + screen.blit(background_image, background_position) + + + controller.handle_event(event) + player.update() + + + + # Copy image to screen: + screen.blit(player.player_image, [controller.x, controller.y]) + + pygame.display.flip() + + clock.tick(60) + +pygame.quit() diff --git a/test/tank_move.py b/test/tank_move.py new file mode 100644 index 00000000..4c4f03d6 --- /dev/null +++ b/test/tank_move.py @@ -0,0 +1,73 @@ +import pygame +from pygame.locals import * +import time + +# Define some colors +WHITE = (255, 255, 255) +BLACK = (0, 0, 0) + +# Call this function so the Pygame library can initialize itself +pygame.init() + +# Create an 800x600 sized screen +screen = pygame.display.set_mode([800, 600]) + +# This sets the name of the window +pygame.display.set_caption('CMSC 150 is cool') + +clock = pygame.time.Clock() + +# Before the loop, load the sounds: +#click_sound = pygame.mixer.Sound("laser5.ogg") + +# Set positions of graphics +background_position = [0, 0] + +# Load and set up graphics. +background_image = pygame.image.load("GrassBackground.jpg").convert() +player = pygame.image.load("top_down_tank-8hkMRt.png").convert() +player.set_colorkey(BLACK) +done = False +class PyGameKeyboardController(object): + """ Handles keyboard input for brick breaker """ + def __init__(self,model): + self.model = model + + def handle_event(self,event): + """ Left and right presses modify the x velocity of the paddle """ + #link for event.key https://www.pygame.org/docs/ref/key.html + if event.type != KEYDOWN: + return + if event.key == pygame.K_LEFT: + self.player.position.x += -1 + if event.key == pygame.K_RIGHT: + self.player.position.x += 1 + if event.key == pygame.K_UP: + self.player.position.y += -1 + if event.key == pygame.K_DOWN: + self.player.position.y += 1 +controller = PyGameKeyboardController(player) +while not done: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + done = True + # elif event.type == pygame.MOUSEBUTTONDOWN: + # click_sound.play() + + # Copy image to screen: + screen.blit(background_image, background_position) + + # Get the current mouse position. This returns the position + # as a list of two numbers. + controller.handle_event(event) + x = player.position.x + y = player.position.y + + # Copy image to screen: + screen.blit(player, [x, y]) + + pygame.display.flip() + + clock.tick(60) + +pygame.quit() diff --git a/test/top_down_tank-8hkMRt.png b/test/top_down_tank-8hkMRt.png new file mode 100644 index 00000000..15751706 Binary files /dev/null and b/test/top_down_tank-8hkMRt.png differ diff --git a/test/top_down_tank-8hkMRt_fix.png b/test/top_down_tank-8hkMRt_fix.png new file mode 100644 index 00000000..601a7bfb Binary files /dev/null and b/test/top_down_tank-8hkMRt_fix.png differ