forked from alexbahner1/InteractiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.py
More file actions
108 lines (83 loc) · 2.8 KB
/
Test.py
File metadata and controls
108 lines (83 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""
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 800x600 sized screen
screen = pygame.display.set_mode([600, 361])
# 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):
""" 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()
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 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.
background_image = pygame.image.load("GrassBackground.jpg").convert()
# player_image = pygame.image.load("top_down_tank-8hkMRt.png").convert()
# player_image.set_colorkey(BLACK)
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
# elif event.type == pygame.MOUSEBUTTONDOWN:
# click_sound.play()
#if event.type != KEYDOWN:
# 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()